I'm trying to remove 2 membership products from cart under 200 in cart total, based on Set minimum Order amount for specific Products or Categories in WooCommerce answer code, this is the script:
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' );
}
}
}
For some reason the products aren't removed from the cart but the notice is working, anyone has a clue what am I missing?
See website-> https://woocommerce-620267-2036098.cloudwaysapps.com/
CodePudding user response:
There were a few bugs and a wrong hook as well, also I've simplified it for you given you only need to check against product IDs:
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' );
}
}
}