0

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' );
    }
}
    }
}```
kaeyenpei
  • 1
  • 2
  • There are lots of similar questions (and probably duplicates on stack overflow.) See for example https://stackoverflow.com/a/62190559/11987538. How you can proceed for your question. **1)** keep the foreach loop from your existing code. **2)** if it belongs to the category, set variable `$cat_in_cart` to true (as you already do in your code). **3)** add an else condition to the if condition, in which the total price of non-category products are added together. **4)** after the loop, check if the `$cat_in_cart` variable is true and if the total of the other products not has been reached – 7uc1f3r Jul 02 '20 at 18:28
  • Related: [Minimum cart item quantity for specific product categories in WooCommerce](https://stackoverflow.com/questions/55909734/minimum-cart-item-quantity-for-specific-product-categories-in-woocommerce/55911750#55911750) -- [Set a minimum quantity for cart items from specific WooCommerce product category](https://stackoverflow.com/questions/57582614/set-a-minimum-quantity-for-cart-items-from-specific-woocommerce-product-category/57582983#57582983). -- – 7uc1f3r Jul 02 '20 at 18:36

0 Answers0