I tried to make the code from Cart Message for a specific shipping class and a minimal cart total in Woocommerce coexist with my recent code Free shipping cart message for a specific shipping class, a minimal amount and a specific Country in Woocommerce, with additional multiple conditions:
add_action( 'woocommerce_before_calculate_totals', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
if ( ! is_cart() || ( is_admin() && ! defined('DOING_AJAX') ) )
return;
$shipping_class = 'supplier';
$min_amout = 20;
$free_shipping = 50;
$amout_incl_tax = 0;
foreach( $cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_shipping_class() === $shipping_class ){
$amout_incl_tax += $cart_item['line_subtotal'] + $cart_item['line_subtotal_tax'];
}
}
if( $amout_incl_tax > 0 && $amout_incl_tax < $min_amout && is_cart() && WC_Geolocation::geolocate_ip()['country'] === 'IT' ){
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
wc_add_notice( sprintf( '<p><strong><font color="#cc1602">20 € of minimum amount on products SUPPLIER</font></strong></p><a href="/brand/supplier-slug" class="button">Add %s of products</a><span class="sub-button">' . wc_price($free_shipping - $amout_incl_tax) . 'for free shipping!</span>',
wc_price($min_amout - $amout_incl_tax) ), 'error' );
}
elseif( $amout_incl_tax > 0 && $amout_incl_tax < $min_amout && is_cart() && ! WC_Geolocation::geolocate_ip()['country'] === 'IT' ){
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
wc_add_notice( sprintf( '<p><strong><font color="#cc1602">20 € of minimum amount on products SUPPLIER</font></strong></p><a href="/brand/supplier-slug" class="button">Add %s of products</a>',
wc_price($min_amout - $amout_incl_tax) ), 'error' );
}
elseif( $amout_incl_tax >= $min_amout && $amout_incl_tax < $free_shipping && is_cart() && WC_Geolocation::geolocate_ip()['country'] === 'IT' ){
wc_add_notice( sprintf( '<p><strong><font color="green">FREE SHIPPING on products SUPPLIER width at least 50 €</font></strong></p><a href="/brand/supplier-slug" class="button free">Add %s of products for free shipping!</a>',
wc_price($free_shipping - $amout_incl_tax) ), 'notice' );
}
}
The cart "Ajax update" seems to be slowed down. Maybe it is possible to lighten the code?