首页 > 解决方案 > Woocommerce - 根据产品类别停止运行代码

问题描述

我在我的functions.php 中使用了一个代码,它根据用户是否登录的天气在我的商店中显示消息。该代码显示一条消息,告诉客户注册为会员以在全站范围内获得 10% 的折扣。

当用户登录时,显示的消息建议在结帐时自动应用折扣。

该代码效果很好,但是,折扣不适用于一种特定的产品类别。我不希望在该特定类别上执行此代码。

有人可以帮忙吗?

这是代码片段。

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>';

}

标签: phpwordpressfunctionwoocommercewordpress-theming

解决方案


我认为您只需要像这样更改第一行:

 if (!is_user_logged_in() && !is_category( '9' );) //change 9 to your category ID
    
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>';

}

推荐阅读