How to exclude certain products from Price adjustments on your WooCommerce store? (Code Snippet)

Bruce has a WooCommerce store in which he wants to set up price adjustments for his products. He uses our ELEX WooCommerce Catalog Mode, Wholesale and Role Based Pricing plugin for achieving this goal. The price adjustment feature in the plugin is used to configure discount or markup value for all products, based on user roles.

But Bruce wants to exclude some product from the price adjustment, to sell at its original price.

Although we have seen a simple demonstration of price adjustment in this part of product setting up article, in this article, we will see how to exclude certain product(s) from price adjustment on your WooCommerce store.

The Initial setup

The initial price setup in Bruce’s store is as shown in the screenshot below. Notice the price of each product.

WooCommerce Price Adjustment | Initial product prices

Initial product prices

Let us now add a discount of 10% to all Administrators of the store.

Following screenshot shows how price adjustment is configured in the Role Based Pricing settings of the plugin.

WooCommerce Price Adjustment | Discount for Administrators

10% Discount for Administrators

Once the price adjustment is applied, all the products in the store will be affected as shown in the screenshot below.

WooCommerce Price Adjustment | Discount applied to all products

Discount applied to all products

Excluding a product from Price adjustment

Bruce wants to exclude the product – Woo Mug from price adjustment. To exclude a product, we need the product ID of that specific product. The product ID can be found in the Products > All Products section, as shown in the screenshot below.

WooCommerce Price Adjustment | Finding Product ID

Finding Product ID

Next, let us paste the following code snippet in the functions.php file of our activated website theme.

Note: To use this code snippet, replace the value 17 in the array(17) with the ID of the product to be excluded from the price adjustments. For multiple products, separate each product ID with a comma.

Product excluded from Price adjustment

Following screenshot shows how the discount of 10% is applied to other products except for Woo Mug.

WooCommerce Price Adjustment | Product excluded from Price adjustment

Product excluded from Price adjustment

In this way, you can exclude certain products from Price adjustment on your WooCommerce store.

Code Snippet to exclude products from discounts if they are already on Sale

If you wish to exclude all products from discounts that are already on sale, you can add the following code snippet at the end of the functions.php file of your activated website theme.

add_filter('xa_pbu_skip_product_on_sale','skip_product_on_sale_from_discount',1,2);
function skip_product_on_sale_from_discount($return_val,$pid) { 
$sale_price = get_post_meta( $pid,'_sale_price',true ); 
if( !empty($sale_price) ) {
return true; // true to skip this product
}
return $return_val;
}

Explore more details about the plugins, go check out ELEX WooCommerce Catalog Mode, Wholesale & Role Based Pricing plugin.

Read the product setting up article to understand the plugin, in detail. Or check out the product documentation section for more related articles.

You can also check out other WooCommerce and WordPress plugins in ELEX.

    Leave a Reply