0

I have a Problem with a custom code. I want to display an error message if the order amount is smaller than € 15. But i also want to exclude specific product categories from that minimum amount. It works on single products(I have found a code here which worked fine), but when i get to a variable product it doesn't. I already have identified the problem by looking into the data provided by $cart_item['data] as it says that "category_id[]" is empty. Can someone help me getting the category_id from the parent product?

Here's the code I already have:

add_action( 'woocommerce_checkout_process', 'minimum_order_amount_excluding_categories' );
add_action( 'woocommerce_before_cart' , 'minimum_order_amount_excluding_categories' );

function minimum_order_amount_excluding_categories() {

// minimum order value
$minimum = 15;

if ( WC()->cart->total < $minimum && sizeof( WC()->cart->get_cart() ) > 0 ) {
    $products_min = false;

    // Check Cart Items
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
        $terms = get_the_terms( $product_id, 'product_cat' );
        foreach ($terms as $term) {
            $category_term_id = $term->term_id;
        }
        // Product Category
        if ( in_array( $category_term_id, array( 55, 49, 51) ) ) {
            $products_min = true;
        }


        /* This is just for the case, that i don't get it with categories, but it's a huge amount of products, so i would prefer category.        
        $Produkt_array = array (277,67,78);

        if ( in_array( $product_id, $Produkt_array ) || $product_id->is_type('variation') && ($product_id->get_parent_id(), $Produkt_array) ) { 
            $products_min = true;
        }*/
    }
    if( ( is_cart() || is_checkout() ) && $products_min!=true ) {
        wc_print_notice( sprintf( 
            __("You must have an order with a minimum of %s to place your order. Your current order total is %s."), 
            wc_price( $minimum ), 
            wc_price( WC()->cart->total )
        ), 'error' );
    }elseif ((is_cart()||is_checkout() ) && $products_min){
        return 0;
    }
}
}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.