WooCommerce: Define Product Settings Programmatically e.g. Enable Reviews, Sold Individually

In WooCommerce, everything is easy until you have a dozen products to manage. But once you start scaling, and maybe need to import hundreds of items, right then is when you go looking for shortcuts.

Problem is – there are single product settings that you must enter manually for each product (or do it in bulk anyway) such as tax status, tax class, shipping class, sold individually, enable reviews and more; you could keep repeating your manual setup operations or fine-tune your bulk editor system to get that done, but what if there were a few lines of code that would simply “set” whatever option you need for ALL products, without worrying whether that specific option is set or not set in the single product edit page?

For example, it happened to a client of mine that we forgot to “enable reviews” on 10,000 imported products so we were left with two choices: re-run the product import, or find something smarter. And the latter is what we’ll cover today.

So, how do you “override” or “force” a specific WooCommerce single product setting without worrying about its actual per-product value, so that products do behave all the same? Here are a couple of ideas. Enjoy!

PHP Snippet 1: Force “Enable Reviews” to All Products

In this case it gets a little tricky, but once you study how reviews are triggered on the single product page, the result is just 7 lines of PHP. Here’s the solution:

/**
 * @snippet       Override & Force Enable Reviews @ WooCommerce Products
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'comments_open', 'bbloomer_force_enable_reviews', 9999, 2 );

function bbloomer_force_enable_reviews( $enable, $post_id ) {
	if ( 'product' === get_post_type( $post_id ) ) {
		$enable = true;
	}
	return $enable;
}

Here’s the before:

And here’s the after – despite “Enable reviews” is disabled, our snippet overrides that and forces the product to display the reviews tab:

PHP Snippet 2: Force “Sold individually” to All Products

Here things get much easier as WooCommerce gives us a filter hook called “woocommerce_is_sold_individually” (we already covered some case scenarios for that in this post). We can simply override the per-product settings, store-wide, with a 1 liner!

/**
 * @snippet       Override & Force Sold Individually @ WooCommerce Products
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_is_sold_individually', '__return_true' );

Here’s the setting screenshot:

And here’s the result on the frontend once the snippet is active:

PHP Snippet 3: Force Any Product Setting

Meet the ‘woocommerce_product_get_WHATEVER‘ filter, that you can use to alter whatever setting was enabled/assigned to any product field.

Simply change “WHATEVER” into any of the following keys (beside each key you find their default value):

$data = array(
		'name'               => '',
		'slug'               => '',
		'date_created'       => null,
		'date_modified'      => null,
		'status'             => false,
		'featured'           => false,
		'catalog_visibility' => 'visible',
		'description'        => '',
		'short_description'  => '',
		'sku'                => '',
		'price'              => '',
		'regular_price'      => '',
		'sale_price'         => '',
		'date_on_sale_from'  => null,
		'date_on_sale_to'    => null,
		'total_sales'        => '0',
		'tax_status'         => 'taxable',
		'tax_class'          => '',
		'manage_stock'       => false,
		'stock_quantity'     => null,
		'stock_status'       => 'instock',
		'backorders'         => 'no',
		'low_stock_amount'   => '',
		'sold_individually'  => false,
		'weight'             => '',
		'length'             => '',
		'width'              => '',
		'height'             => '',
		'upsell_ids'         => array(),
		'cross_sell_ids'     => array(),
		'parent_id'          => 0,
		'reviews_allowed'    => true,
		'purchase_note'      => '',
		'attributes'         => array(),
		'default_attributes' => array(),
		'menu_order'         => 0,
		'post_password'      => '',
		'virtual'            => false,
		'downloadable'       => false,
		'category_ids'       => array(),
		'tag_ids'            => array(),
		'shipping_class_id'  => 0,
		'downloads'          => array(),
		'image_id'           => '',
		'gallery_image_ids'  => array(),
		'download_limit'     => -1,
		'download_expiry'    => -1,
		'rating_counts'      => array(),
		'average_rating'     => 0,
		'review_count'       => 0,
);

Which means, if you wish to override “manage_stock”, you can write the following:

/**
 * @snippet       Override a Setting @ WooCommerce Products
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_product_get_manage_stock', 'bbloomer_override_product_setting', 9999, 2 );

function bbloomer_override_product_setting( $value, $product ) {
   $value = 'yes';
   return $value;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Define Product Settings Programmatically e.g. Enable Reviews, Sold Individually

  1. Hi there!

    On “PHP Snippet 3: Force Any Product Setting” – does this change the actual setting? For example, I have a client that has thousands of products that have (wrongly) been input as Tax Status: None when they should be taxable. If I run the relevant filter to change tax status to ‘taxable’ and then remove it, will products remain taxable? Or will they revert to None?

    Create content as always by the way.

    1. This filter does not “save” the setting, it filters it. So as long as the filter is active, the Tax Status will be “taxable”. As soon as you remove the code, the Tax Status will read whatever is saved/visible in the product settings. Does this help?

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *