WordPress / Web Development Tutorials
(Best WordPress Tutorials)

CSSHTMLJavaScriptjQueryMySQLPHPSilvaTechnologiesWooCommerceWordpress
Silva Web Designs - Blog

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

This is a nice addition to any WooCommerce store, in my E-Commerce experience, customers who are near the ‘free shipping’ threshold will try to add more products to the cart to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the WooCommerce cart page:-


add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice_zones' );
 
function bbloomer_free_shipping_cart_notice_zones() {
 
global $woocommerce;
 
// Get Free Shipping Methods for Rest of the World Zone & populate array $min_amounts
 
$default_zone = new WC_Shipping_Zone(0);
$default_methods = $default_zone->get_shipping_methods();
 
foreach( $default_methods as $key => $value ) {
    if ( $value->id === "free_shipping" ) {
      if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
    }
}
 
// Get Free Shipping Methods for all other ZONES & populate array $min_amounts
 
$delivery_zones = WC_Shipping_Zones::get_zones();
 
foreach ( $delivery_zones as $key => $delivery_zone ) {
  foreach ( $delivery_zone['shipping_methods'] as $key => $value ) {
    if ( $value->id === "free_shipping" ) {
    if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
    }
  }
}
 
// Find lowest min_amount
 
if ( is_array($min_amounts) ) {
 
$min_amount = min($min_amounts);
 
// Get Cart Subtotal inc. Tax excl. Shipping
 
$current = WC()->cart->subtotal;
 
// If Subtotal < Min Amount Echo Notice
// and add "Continue Shopping" button
 
if ( $current < $min_amount ) {
$added_text = esc_html__('Get free shipping if you order ', 'woocommerce' ) . wc_price( $min_amount - $current ) . esc_html__(' more!', 'woocommerce' );
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), $added_text );
wc_print_notice( $notice, 'notice' );
}
 
}
 
}

Simply add this to your functions.php and ensure you have configured the shipping methods for both flat rate and free shipping and you should be good to go.

You will end up with a notification in the basket as follows:-

 

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital-related. His main expertise is with WordPress and various other CMS frameworks. If you need responsive design, SEO, speed optimisation or anything else in the world of digital, you can contact Silva Web Designs here; [email protected]

It’s good to share

Join the discussion

Related Posts

Related - How to Skip the Cart Page and Go Straight to the Checkout Page in WooCommerce

WooCommerce / 17th September 2020

How to Skip the Cart Page and Go Straight to the Checkout Page in WooCommerce

Read More