Следующий код проверяет, относится ли продукт в корзине к категории: special-box и скрывает" перейти к кнопке оформления заказа ", если минимальный вес не достигнут.
function checkout_required_min_weight_mood () {
// Only on cart and check out pages
if( ! ( is_cart() || is_checkout() ) ) return;
// The minimum weight
$minimum_weight = 20; // 20 kg
// Get the Cart's content total weight
$total_weight = WC()->cart->get_cart_contents_weight();
// 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 ) {
// If Cart has category "special-box", set $cat_in_cart to true
if ( has_term( 'special-box', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
}
// If the total weight is less than the minimum, we avoid payment and display an error notice & category is in the Cart
if( $total_weight < $minimum_weight && $cat_in_cart ) {
// Displays a dynamic error warning
wc_add_notice( sprintf(
'<strong>Il minimo peso di %s è richiesto prima di acquistare.</strong>'
. '<br />Il peso totale dei prodotti nel tuo carrello al momento è di %s',
wc_format_weight($minimum_weight),
wc_format_weight($total_weight)
), 'error' );
// Removing the Proceed to checkout button from the Cart page
remove_action( 'woocommerce_proceed_to_checkout','woocommerce_button_proceed_to_checkout', 20);
}
}
add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight_mood' );