Free Shipping And Discounts When Above Specific Total in WooCommerce

This code enables you to apply multiple discounts when the subtotal is above specific thresholds. It also enables you to apply free shipping and remove paid shipping options when the subtotal is above a set threshold.

In this example we :

  1. Apply a 10% discount to the sub totals at the cart and checkout when more than $100 worth of products have been added.
  2. Apply a 20% discount to the sub totals at the cart and checkout when more than $200 worth of products have been added.
  3. Remove flat rate shipping so only the free shipping and local pickup shipping options display when the sub total is $50 or more.

Discounts

In the code for the discounts :

  • The code defines thresholds for discounts based on the cart subtotal.
  • It calculates the cart subtotal and applies discounts accordingly.
  • If the subtotal is greater than or equal to a certain threshold (e.g., $200), it applies a 20% discount.
  • If the subtotal is between another threshold (e.g., $100) and the previous one, it applies a 10% discount.
  • The discounts are added to the cart as fees, reducing the total amount due.

Shipping

In this code we :

  • We loop through each shipping rate.
  • If the method ID of a shipping rate is flat_rate, we unset it.
  • We only remove the flat_rate shipping option when the cart subtotal is over $50.

Video Demo

Shows the discounts added when the total is above $100 and the flat rate shipping removed when the total is above $50 so only free shipping and local pickup display when above the $50 threshold.

Code Settings

  1. Swap out your thresholds on line 16 and 17 for your discounts.
  2. Swap out your discount percentages on lines 24,25 and 27,28.
  3. Swap out your free shipping threshold on line 37.
  4. Swap out your shipping method ID on line 45 if you want to remove a shipping option other than flat rate.

These PHP functions should be added to the end of your child theme’s functions.php file or a custom functionality/code snippets plugin.

Find more code snippets which uses the woocommerce_cart_calculate_fees hook for fees and discounts and the woocommerce_package_rates hook for modifying/customizing shipping options in WooCommerce.

If you have any questions or need further clarification, feel free to ask!

Related Solutions

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.