I am using WooCommerce.
We are having a Purchase(A) with Purchase(B) promotion for Birthday perks. Customers must add at least one product from normal categories (A) before they are able to purchase items from BIRTHDAY category. (B)
However, the value of purchases from A must meet a minimum of $50 if customers want to buy from B. Minimum amount does not apply if customer is not buying from B.
Therefore, when viewing cart/submitting for order, we first need to check if the cart contains any products from "birthday" category, and if yes, we will need to check the total cart value (excluding products from B) is more than $50. If it's not more than $50, customer will not be allowed to checkout
I am not a developer, but from the answers to similar questions, I managed to kinda come out with a code, the first part works, but not the second function. will appreciate any help thanks in advance! :)
function fs_check_category_in_cart() {
// Set $cat_in_cart to false
$cat_in_cart = false;
// Loop through all products in the Cart
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
// If Cart has category "birthday", set $cat_in_cart to true
if ( has_term( 'birthday', 'product_cat', $product->get_id() ) ) {
$cat_in_cart = true;
break;
}
}
// Do something if category "birthday" is in the Cart
if ( $cat_in_cart ) {
// For example, print a notice
add_action( 'woocommerce_before_checkout_form', 'min_cart_amount' );
function min_cart_amount() {
## ----- Your Settings below ----- ##
$min_amount = 50; // Minimum cart amount
$except_ids = array(690); // Except for this product(s) ID(s)
// Loop though cart items searching for the defined product
foreach( WC()->cart->get_cart() as $cart_item ){
if( in_array( $cart_item['variation_id'], $except_ids )
|| in_array( $cart_item['product_cat'], $except_ids ))
return; // Exit if the defined product is in cart
}
if( WC()->cart->subtotal < $min_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_amount ),
wc_price( WC()->cart->subtotal )
), 'error' );
}
}
}
}```