• Resolved IAmTheAllspark

    (@iamtheallspark)


    Our store offers free shipping on orders over $75. The Free Shipping method is set up to offer free shipping if there is a valid coupon or a min. amount of $75.

    My questions is…is there any way to restrict or remove free shipping if the order amount is over a certain amount, say $200.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Try this:

    apply_filters( 'woocommerce_shipping_free_shipping_is_available', 'prefix_is_available_up_to_max_amount', 10, 3 );
    /**
     * @param bool                      $is_available
     * @param array                     $package Shipping package.
     * @param WC_Shipping_Free_Shipping $shipping_class
     *
     * @return bool
     */
    function prefix_is_available_up_to_max_amount( $is_available, $package, $shipping_class ) {
    	$max_amount = 200;
    	$total      = WC()->cart->get_displayed_subtotal();
    
    	if ( 'incl' === WC()->cart->tax_display_cart ) {
    		$total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
    	} else {
    		$total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() );
    	}
    
    	if ( $total >= $max_amount ) {
    		$is_available = false;
    	}
    
    	return $is_available;
    }

    https://gist.github.com/vanbo/817a959c8c0c4cae3bc09d3e3532ce13

    Thread Starter IAmTheAllspark

    (@iamtheallspark)

    Thanks for the speedy reply!

    Unfortunately, free shipping is still showing up over $200. I also tried clearing the WC Transients and the Expired Transients, but it’s still not working.

    Is it working for you?

    Sorry, the filter hook should be add_filter, not apply_filters. My bad.

    Thread Starter IAmTheAllspark

    (@iamtheallspark)

    That works. Thank-you soooooo much!

    So this is the code:

    add_filter( 'woocommerce_shipping_free_shipping_is_available', 'prefix_is_available_up_to_max_amount', 10, 3 );
    /**
     * @param bool                      $is_available
     * @param array                     $package Shipping package.
     * @param WC_Shipping_Free_Shipping $shipping_class
     *
     * @return bool
     */
    
    function prefix_is_available_up_to_max_amount( $is_available, $package, $shipping_class ) {
    	$max_amount = 200;
    	$total      = WC()->cart->get_displayed_subtotal();
    
    	if ( 'incl' === WC()->cart->tax_display_cart ) {
    		$total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
    	} else {
    		$total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() );
    	}
    
    	if ( $total >= $max_amount ) {
    		$is_available = false;
    	}
    
    	return $is_available;
    }
    
    ?>
    Thread Starter IAmTheAllspark

    (@iamtheallspark)

    Not to be a PITA, but is there any way I could have an exception by Coupon code? So if Coupon code is XXXXXX then don’t apply the free shipping not available over $200?

    Yes, add that to the top of the function

    if ( $coupons = WC()->cart->get_coupons() ) {
    	/**
    	 * @var WC_Coupon $coupon
    	 */
    	foreach ( $coupons as $code => $coupon ) {
    		// Check against a single code you add the codes to an array and check that
    		if ( 'your-coupont-code' == $coupon->get_code() ) {
    			// Bail
    			return $is_available;
    		}
    	}
    }
    Thread Starter IAmTheAllspark

    (@iamtheallspark)

    Works like a charm!

    Thanks man – You awesome.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Restricting Free Shipping Over a Certain Amount’ is closed to new replies.