rachael-portier
8/13/2019 - 9:09 PM

Spend XX more for free shipping

/* Must enable free shipping in WooCom settings for specific zone for this to work  */



function add_x_for_free_shipping() {

	if ( ! is_cart() && ! is_checkout() ) { // Remove partial if you don't want to show it on cart/checkout
		return;
	}

	$packages = WC()->cart->get_shipping_packages();
	$package = reset( $packages );
	$zone = wc_get_shipping_zone( $package );

	$cart_total = WC()->cart->get_displayed_subtotal();
	if ( WC()->cart->display_prices_including_tax() ) {
		$cart_total = round( $cart_total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
	} else {
		$cart_total = round( $cart_total - WC()->cart->get_discount_total(), wc_get_price_decimals() );
	}
	foreach ( $zone->get_shipping_methods( true ) as $k => $method ) {
		$min_amount = $method->get_option( 'min_amount' );


		if ( $method->id == 'free_shipping' && ! empty( $min_amount ) && $cart_total < $min_amount ) {
			$remaining = $min_amount - $cart_total;
			echo '<shipping>';
			echo 'Add $' . $remaining . ' more to get free shipping!';
			echo '</shipping>';
		}
		elseif ( $method->id == 'free_shipping' && ! empty( $min_amount ) && $cart_total > $min_amount ) {
			$remaining = $min_amount - $cart_total;
			echo '<shipping>';
			echo 'You qualify for free shipping!';
			echo '</shipping>';
		}
		
	}
	
	

}
add_shortcode( 'free_shipping_notice', 'add_x_for_free_shipping' );