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 tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 5
* @donate $9 https://tutoraspire.com
*/
add_filter( 'comments_open', 'tutoraspire_force_enable_reviews', 9999, 2 );
function tutoraspire_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 tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 5
* @donate $9 https://tutoraspire.com
*/
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 tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 5
* @donate $9 https://tutoraspire.com
*/
add_filter( 'woocommerce_product_get_manage_stock', 'tutoraspire_override_product_setting', 9999, 2 );
function tutoraspire_override_product_setting( $value, $product ) {
$value = 'yes';
return $value;
}