0

I would like to add a message in cart for the free shipping, depend a mininum amount for a specific shipping class, and a specific Country (I have the Geolocation active). I have modified the code from Cart Message for a specific shipping class and a minimal cart total in Woocommerce answer.

My solution seems to work, i would like to make sure i wrote it correctly on all devices.

add_action( 'woocommerce_before_calculate_totals', 'cart_items_shipping_class_message1aa', 20, 1 );
function cart_items_shipping_class_message1aa( $cart ){
    if ( ! ( is_cart() && WC_Geolocation::geolocate_ip()['country'] === 'IT' ) || ( 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 >= $min_amout && $amout_incl_tax < $free_shipping && is_cart() ){           
        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 from Supplier</a>',
            wc_price($free_shipping - $amout_incl_tax) ), 'notice' );
    }
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
fabiocs81
  • 63
  • 1
  • 5
  • 1
    Your code seems to be correct… – LoicTheAztec Mar 30 '21 at 21:04
  • I tried to replicate a similar message (without call-to-action) next to the total shipping, replacing woocommerce_before_calculate_totals with woocommerce_after_shipping_rates, and inserting the echo function instead of wc_add_notice, but it doesn't work. – fabiocs81 Mar 31 '21 at 05:37

0 Answers0