php - 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)?
Answer
Solution:
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
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function format() on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.