【发布时间】:2021-02-10 17:11:04
【问题描述】:
我有 3 个类别“其他”、“D 类”和“T 类”:
• 如果用户的购物篮中有属于 3 个类别的产品,则接受付款。
• 如果用户的购物篮中有属于“D 类”和“其他”的产品,则接受付款。
• 如果用户只有一个类别的产品,则接受付款
• 如果用户的购物篮中有属于“T 类”和“D 类”的产品,付款将被拒绝。
• 如果篮子是空的,则不会出现错误消息。
总而言之,只有当用户的购物篮中有“D 类”和“T 类”产品时,才应拒绝付款。
这是我的代码:
function verifierproduitpanier(){
$cat_is_catD = false;
foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item){
$product = $cart_item['data'];
if (has_term('categorieD', 'product_cat',$product->id)) {
$cat_is_catD = true;
}
$cat_is_catT = false;
if (has_term('categorieT', 'product_cat',$product->id)) {
$cat_is_catT = true;
}
$cat_is_other = false;
if (has_term('autre', 'product_cat',$product->id)) {
$cat_is_other = true;
}
}
if ($cat_is_catD && $cat_is_catT && !$cart_is_other){
wc_add_notice(sprintf('<p>Les produits sélectionnés ne sont pas disponibles</p>'), 'error');
}
}
add_action( 'woocommerce_check_cart_items', 'verifierproduitpanier' );
当我有属于“D 类”和“T 类”的商品时,我会弹出一条错误消息,但问题是当我从“D 类”中删除产品并单击“取消”按钮时错误消息不再出现,付款被接受。
有什么帮助吗?
【问题讨论】:
标签: php wordpress woocommerce cart taxonomy-terms