php - 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>';
}
解决方案
我认为您只需要像这样更改第一行:
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>';
}
推荐阅读
- python - Python:使用另一个数据帧替换一个数据帧中的数据
- javascript - 使用 if 语句来回移动循环
- c++ - 如何使用Repeater和GridView将二维数组的值从C++显示到QML
- c++ - 使用 oop 在单链表中添加节点
- c++ - 在评估内部等待表达式期间可以销毁协程吗?
- jquery - 获取错误的值复选框jQuery
- python - REST API in Python with FastAPI and pydantic: read-only property in model
- python - SQLAlchemy/Flask 未在表中显示记录
- c++ - char8_t / UTF8 chars pre-C++17 和穷人的情况呢?
- mysql - Generate a triangle of stars using mySQL recursive CTE