Woocommerce – Stop code from running based on product category

I am using a code in my functions.php that displays messages on my store based on weather the user is logged in or not. The code displays a message telling customers to sign up as a member to receive a 10% discount sitewide.

When as user is logged in the message displayed advises that discount is automatically applied at checkout.

The code works great, however, there is one particular product category that the discount is not valid for. I don’t want this code to be executed on that particular category.

Can anyone assist with this?

This is a snippet of the code.

if (!is_user_logged_in()) 
    
add_filter( 'woocommerce_get_price_html', 'PW_text_after_price');

function PW_text_after_price($price){

    $text_to_add_after_price  = '<span style="font-style:italic;font-weight:bold;color: #A99055;font-size:14px!important;">' . _('<p><p>Sign-up for 10% members discount!</p>').'</span>'; //change text in bracket to your preferred text 
          
    return $price .   $text_to_add_after_price;
          
}       

if ( is_user_logged_in() ) 
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );

function add_content_after_addtocart_button_func() {

// Echo content.

echo '<span style="font-style:italic;font-weight:bold;color: #A99055;font-size:14px!important;">' . _('Members discount automatically applied at checkout!').'</span>';

}

Source: Ask PHP

LEAVE A COMMENT