Raffaele Guarda
Raffaele Guarda

Reputation: 31

Minimum weight and mandatory product categories requirements in WooCommerce

I am using How to set a purchase limit at checkout with a minimum weight requirement for a certain category? code answer to my previous question, to enable a minimum weight in woocommerce for a specific product category.

I need now an additional rule for a specific product category can only be purchased with products from "Formaggi" product category. That specific product category is "Coltelli" (knives) that can only be purchased with an order of "Formaggi" (cheese).

How to make "Coltelli" (knives) product category purchasable only with an order of "Formaggi" (cheese)?

Upvotes: 1

Views: 268

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254286

The following will enable a minimum weight for "Formaggi" category (Cheese) and avoid to buy items from "Coltelli" category (knives) without items from "Formaggi" category (Cheese):

add_action( 'woocommerce_check_cart_items', 'minimum_weight_and_category_requirements' );
function minimum_weight_and_category_requirements() {
    // Only on cart and check out pages
    if( ! ( is_cart() || is_checkout() ) ) return;
    
    // Your settings:
    $min_weight = 0.750; // Minimum weight( 750 GR )
    $formaggi   = array('Formaggi'); // The category for weight calculation
    $coltelli   = array('Coltelli'); // The category that requires 'Formaggi'
    
    // Initializing
    $total_weight   = 0; 
    $coltelli_found = $remove_button = false;

    // Loop through cart items        
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {  
        // Has weight and 'Formaggi' category
        if ( $cart_item['data']->get_weight() > 0 && has_term( $formaggi, 'product_cat', $cart_item['product_id'] ) ) {
            // Add to total weight
            $total_weight += $cart_item['quantity'] * $cart_item['data']->get_weight();
        }

        // Has 'coltelli' category
        if ( has_term( $coltelli, 'product_cat', $cart_item['product_id'] ) ) {
            $coltelli_found = true; // Found 'coltelli'
        }
    }

    // When total weight is less than the minimum require weight for "Formaggi" category
    if( $total_weight > 0 && $total_weight < $min_weight ) {
        // Displays a dynamic error notice
        wc_add_notice( sprintf( '<strong>%s</strong> <br>%s: %s',
            __("Per i Formaggi è richiesto un acquisto minimo di 750 gr."),
            __("Peso dei Formaggi nel carrello"),
            wc_format_weight($total_weight)
        ), 'error' );
        
        $remove_button = true;
    }
    
    // When "Coltelli" category is purchased without "Formaggi" category
    if( $total_weight == 0 && $coltelli_found ) {
        // Displays a dynamic error notice
        wc_add_notice( sprintf( __("Gli articoli %s possono essere acquistati solo con un ordine di %s."),
            '<strong>"' . __("Coltelli") . '"</strong>',
            '<strong>"' . __("Formaggi") . '"</strong>'
        ), 'error' );
        
        $remove_button = true;
    }
    
    // Remove "Proceed to checkout" button
    if( $remove_button ) {
        remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
    }
}

Code goes on functions.php file of your active child theme (or active theme). It should works.

Related: Minimum cart amount for specific product categories in WooCommerce

Upvotes: 1

Related Questions