Skip to content
Advertisement

WooCommerce: “You Only Need x pieces to Get Free Shipping!” @ Cart

Each product weighs is 1kg.

From the second product onwards (so 2kg) shipping is free.

how can I get this notice to appear?: you Only Need x pieces to Get Free Shipping!

add_action( 'woocommerce_before_cart', 'free_shipping_cart_notice' );
  
function free_shipping_cart_notice() {
  
   $min_amount = 2; //change this to your free shipping threshold
   
   $cart_item_quantities = WC()->wc_get_weight;
  
   if ( $cart_item_quantities < $min_amount ) {
      $added_text = 'Get free shipping if you order ' . wc_price( $min_amount - $cart_item_quantities ) . ' more!';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Acquista altro', $added_text );
      wc_print_notice( $notice, 'notice' );
   }
  
}

Thank you

Advertisement

Answer

Following from https://www.businessbloomer.com/woocommerce-add-need-spend-x-get-free-shipping-cart-page/ where I believe you got the snippet from, and based on your statement (basically if Cart is not empty and there is 1 item in the Cart, you only need 1 item to get to 2kg/free shipping), I’d use the following:

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice' );
  
function bbloomer_free_shipping_cart_notice() {
               
   if ( ! WC()->cart->is_empty() && WC()->cart->get_cart_contents_count() < 2 ) {
      $added_text = 'You only need 2 pieces to get free shipping!';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
      wc_print_notice( $notice, 'notice' );
   }
  
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement