【发布时间】:2021-11-27 09:06:28
【问题描述】:
我正在尝试根据Set minimum Order amount for specific Products or Categories in WooCommerce 答案代码,从购物车中总共 200 件以下的购物车中删除 2 件会员产品,这是脚本:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$minimum_amount = 200;
$category_ids = array( 83 );
$product_ids = array( 1111 , 1112 );
if( WC()->cart->is_empty() || ! ( is_cart() || is_checkout() ) )
return;
$total_amount = WC()->cart->subtotal; // Items subtotal including taxes
if ( $total_amount < $minimum_amount ) {
$needs_minimum_amount = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
if( sizeof($category_ids) > 0 ) {
$taxonomy = 'product_cat';
if ( has_term( $category_ids, $taxonomy, $product_id ) ) {
$needs_minimum_amount = true;
break; // Stop the loop
}
}
if( sizeof($product_ids) > 0 ) {
if ( array_intersect( $product_ids, array($product_id, $variation_id) ) ) {
$needs_minimum_amount = true;
break;
}
}
}
if( $needs_minimum_amount ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['product_id'] == 1111 || $cart_item['product_id'] == 1112 ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
}
wc_print_notice( sprintf(
__("רכישת חברת מועדון מעל %s. כרגע סך העגלה שלך עומד על %s."),
wc_price( $minimum_amount ),
wc_price( $total_amount )
), 'error' );
}
}
}
由于某种原因,产品没有从购物车中删除,但通知有效,有人知道我错过了什么吗?
查看网站-> https://woocommerce-620267-2036098.cloudwaysapps.com/
【问题讨论】:
-
您查看过您的日志文件了吗?这些是否包含错误消息?你提到删除产品。但是,也有一部分考虑了类别。这也适用吗?
-
从我在网上看到的 remove_cart_item() 不需要分类
-
那为什么在你的代码中使用它呢?如果不需要?
-
它需要确定产品是否需要最低金额
-
我只是不明白为什么 remove_cart_item() 不起作用:(
标签: php wordpress woocommerce cart