php - Woocommerce:如果购物车包含此类别,则更改购物车总数
问题描述
我一直在尝试解决这个问题一段时间,但似乎无法弄清楚为什么这只能以一种方式工作。
目标:当“服务”类别在购物车中时,默认购物车总额为 50 美元的押金,如果“服务”类别的产品不在购物车中,则正常结帐。
问题:woocommerce 通知运行良好,但购物车没有改变。我尝试从 if 语句而不是 woocommerce 通知中调用change_calculcated_total函数,但是无论购物车中的类别是什么,该函数都始终运行...
是结构错误吗?
add_action( 'woocommerce_before_cart', 'bbloomer_check_category_in_cart' );
$cat_in_cart = false; // Set $cat_in_cart to false
function bbloomer_check_category_in_cart() {
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// If Cart has category "services", set $cat_in_cart to true
if ( has_term( 'services', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}
// Do something if category "services" is in the Cart
if ( $cat_in_cart ) {
// For example, print a notice
wc_print_notice( 'Services selected, Only $50 deposit is required.', 'notice' );
}
}
//change cart total.
function change_calculated_total( $total, $cart ) {
if ( $cat_in_cart ){
return 50;
}else{
return $total;
}
}
add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
解决方案
推荐阅读
- javascript - 传递的 id 未定义,错误 400(错误请求)
- python - 基于数组字段的MongoDB查找查询
- docker - “docker run --rm -v”从守护进程获取错误响应:状态码不正常,但 500
- c# - 升级我的 Chrome 后,除了 Bootstrap Datepicker 之外还有两个黑点
- c++ - 如何使用 PJSIP 更改 INVITE SDP 中的 RTP 媒体流地址?
- html - 两个并排可滚动的 div,水平滚动始终可见
- plugins - Gatsby createSchemaCustomization 向每个节点添加自定义字段
- powershell - Get-ADUser -LDAPFilter 使用 AND 和 OR
- r - 从依赖于其他变量中的 NA 的变量中删除单个值
- javascript - 使用循环的 Firestore 批量写入