从 WooCommerce 购物车中删除特定数量的特定产品
Remove specific products from WooCommerce cart under certain amount
我正在尝试根据 答案代码从购物车中移除 2 个会员产品,总数量低于 200,这是脚本:
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' );
}
}
}
出于某种原因,产品没有从购物车中移除,但通知有效,有人知道我错过了什么吗?
还有一些错误和一个错误的钩子,我还为您简化了它,因为您只需要检查产品 ID:
add_action( 'woocommerce_before_checkout', 'bbloomer_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'bbloomer_minimum_order_amount' );
function bbloomer_minimum_order_amount() {
$minimum_amount = 200;
$product_ids = array( 186181, 186185 );
if ( WC()->cart->is_empty() ) return;
$total_amount = WC()->cart->subtotal; // Items subtotal including taxes
if ( $total_amount < $minimum_amount ) {
$needs_minimum_amount = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
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 ( array_intersect( $product_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
}
wc_print_notice( sprintf(
__("רכישת חברת מועדון מעל %s. כרגע סך העגלה שלך עומד על %s."),
wc_price( $minimum_amount ),
wc_price( $total_amount )
), 'error' );
}
}
}
我正在尝试根据
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' );
}
}
}
出于某种原因,产品没有从购物车中移除,但通知有效,有人知道我错过了什么吗?
还有一些错误和一个错误的钩子,我还为您简化了它,因为您只需要检查产品 ID:
add_action( 'woocommerce_before_checkout', 'bbloomer_minimum_order_amount' );
add_action( 'woocommerce_before_cart', 'bbloomer_minimum_order_amount' );
function bbloomer_minimum_order_amount() {
$minimum_amount = 200;
$product_ids = array( 186181, 186185 );
if ( WC()->cart->is_empty() ) return;
$total_amount = WC()->cart->subtotal; // Items subtotal including taxes
if ( $total_amount < $minimum_amount ) {
$needs_minimum_amount = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
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 ( array_intersect( $product_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
}
wc_print_notice( sprintf(
__("רכישת חברת מועדון מעל %s. כרגע סך העגלה שלך עומד על %s."),
wc_price( $minimum_amount ),
wc_price( $total_amount )
), 'error' );
}
}
}