问题
I'm using the following code "Minimum cart amount except for a specific product in Woocommerce" in my php that allows the override of the Woocommerce minimum cart value of $130.
Works fine but only for one defined product. In this case product_id 2649
I am trying to further modify this code by adding lines of except_product_id...
but that does not help.
How can I modify to make a list of exceptions by product id
?
Any help appreciated
add_action( 'woocommerce_check_cart_items', 'min_cart_amount' );
function min_cart_amount() {
## ----- EXCLUDES A PRODUCT FROM MINIMUM ORDER DOLLAR VALUE Your Settings below ----- ##
$min_cart_amount = 130; // Minimum cart amount
$except_product_id = 2649; // Except for this product ID
$except_product_id = 2659; // Except for this product ID
$except_product_id = 1747; // Except for this product ID
// Loop though cart items searching for the defined product
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_id() == $except_product_id || $cart_item['product_id'] == $except_product_id )
return; // Exit if the defined product is in cart
}
if( WC()->cart->subtotal < $min_cart_amount ) {
wc_add_notice( sprintf(
__( "<strong>A Minimum of %s is required before checking out.</strong><br>The current cart's total is %s" ),
wc_price( $min_cart_amount ),
wc_price( WC()->cart->subtotal )
), 'error' );
}
}
回答1:
You overwrite your variable by using the same naming convention on every new line. So if you want to apply this for multiple IDs you have to put them in an array.
function min_cart_amount() {
## ----- EXCLUDES A PRODUCT FROM MINIMUM ORDER DOLLAR VALUE Your Settings below ----- ##
$min_cart_amount = 130; // Minimum cart amount
$except_product_id = array ( 2649, 2659, 1747 ); // Except for this product ID
// Loop though cart items searching for the defined product
foreach( WC()->cart->get_cart() as $cart_item ) {
// Product id
$product_id = $cart_item['product_id'];
// Exit if the defined product is in cart
if ( in_array( $product_id, $except_product_id) ) {
return;
}
}
if( WC()->cart->subtotal < $min_cart_amount ) {
wc_add_notice( sprintf(
__( "<strong>A Minimum of %s is required before checking out.</strong><br>The current cart's total is %s" ),
wc_price( $min_cart_amount ),
wc_price( WC()->cart->subtotal )
), 'error' );
}
}
add_action( 'woocommerce_check_cart_items', 'min_cart_amount', 10, 0 );
来源:https://stackoverflow.com/questions/62189109/minumun-cart-amount-except-for-several-specific-products-in-woocommerce