【发布时间】:2022-01-10 21:02:01
【问题描述】:
我正在为客户创建一个登录页面,其中包含类别为landing-page 的特定产品。
我希望在购物车中出现 landing-page 类别时删除当前在购物车页面上的其他产品。
这里是sn-p。现在,由于$woocommerce->cart->empty_cart(),它会删除其中的所有产品。
add_action('woocommerce_checkout_before_customer_details', 'check_if_landing_page_category_is_on_cart');
function check_if_landing_page_category_is_on_cart() {
global $woocommerce;
$categories = array('landing-page');
$has_category = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product categories
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$woocommerce->cart->empty_cart();
$has_category = true;
break;
}
}
if ( $has_category ) {
?>
<style>
.coupon-form {
display: none;
}
</style>
<?php
}
}
有什么建议吗?
【问题讨论】:
标签: wordpress woocommerce product hook-woocommerce checkout