How to Exclude a Product from Discount Coupons in WooCommerce with PHP Snippet


Discover how to exclude specific products from discount coupons in WooCommerce using a PHP snippet. By following these steps, you’ll have more control over your coupon promotions and ensure a seamless shopping experience for your customers.

Step 1: Locate the PHP File Open your theme’s functions.php file or create a custom plugin for code snippets.

Step 2: Implement the PHP Snippet Copy and paste the PHP code provided below into the functions.php file or your custom plugin.

function exclude_product_from_coupons( $excluded, $product ) {
    $excluded_products = array( 123 ); // Replace 123 with the product ID you want to exclude

    if ( in_array( $product->get_id(), $excluded_products ) ) {
        $excluded = true;
    }

    return $excluded;
}
add_filter( 'woocommerce_coupon_is_valid_for_product', 'exclude_product_from_coupons', 10, 2 );

Step 3: Define the Product to Exclude In the PHP snippet, replace 123 with the actual product ID you want to exclude from discount coupons. Find the product ID by editing the product in the WooCommerce admin area and checking the URL.

Step 4: Save and Activate Save the changes to the functions.php file or your custom plugin. The exclusion functionality will now be active.


Leave a Reply

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