【问题标题】:Woocommerce - Stop code from running based on product categoryWoocommerce - 根据产品类别停止运行代码
【发布时间】:2021-10-06 23:48:47
【问题描述】:

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

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

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

有人可以帮忙吗?

这是一个sn-p的代码。

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

}

【问题讨论】:

    标签: php wordpress function woocommerce wordpress-theming


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 2021-10-27
      • 2020-08-25
      相关资源
      最近更新 更多