在Woocommerce中,当两个类别的产品都在购物车中时,我试图使该功能在购物车页面上方显示一条消息.
我正在尝试修改在这里找到的代码,但是当添加其中一个类别而不是两个类别时,它已经显示了该消息:
add_action( 'woocommerce_before_cart', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
$special_cat2 = 'test-cat2';
$special_cat3 = 'test-cat3';
$bool = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $cart_item['data'];
if ( has_term( $special_cat2 && $special_cat3, 'product_cat', $item->id ) )
$bool = true;
}
if ($bool)
echo '<div class="cartmessage">Warning! Beware of combining these materials!</div>';
}
解决方法:
您的代码中存在一些错误,例如has_term()不支持2个用&&并从购物车中获取用于定制分类法的产品ID作为产品类别,您需要$cart_item [‘product_id’]来代替,这也适用于产品变体……
为了使这两个产品类别都在购物车中时起作用,请改用以下方法:
add_action( 'woocommerce_before_cart', 'allclean_add_checkout_content', 12 );
function allclean_add_checkout_content() {
$product_cats = array('test-cat2','test-cat3');
$found1 = $found2 = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
// The needed working product ID is $cart_item['product_id'] <====
if ( has_term( $product_cats[0], 'product_cat', $cart_item['product_id'] ) )
$found1 = true;
elseif ( has_term( $product_cats[1], 'product_cat', $cart_item['product_id'] ) )
$found2 = true;
}
if ( $found1 && $found2 )
echo '<div class="cartmessage">Warning! Beware of combining these materials!</div>';
}
代码进入您的活动子主题(或活动主题)的function.PHP文件中.经过测试和工作.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。