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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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 this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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