Skip to content
Advertisement

Change shipping cost for specific countries based on total orders by customer in WooCommerce

I follow this thread that works perfectly:

Set discount based on number of orders in WooCommerce.

But in my case I need to apply the discount not to the order total, but to the shipping cost and I would like to do this only for a specific country, specifically UK.

For example:

  • ONLY First order: 6€ (flat rate shipping cost)
  • From the second order onwards, always 25€ (flat rate shipping cost)

I already set the flat rate at 6€ for Uk, but I need to understand how to apply the 25€ surcharge only from the second order onwards.

This is my code attempt:

JavaScript

However, this does not adjust the costs, but removes the shipping method. Any advice?

Advertisement

Answer

Some notes on your code attempt/question:

  • The answer you refer to is about adding a (negative) fee, the woocommerce_package_rates hook is indeed better suited than the woocommerce_cart_calculate_fees hook for your question
  • You can use $package['destination']['country'] to determine the country
  • unset( $rates['flat_rate:15'] ) will not adjust the cost but remove the method
  • To get the total orders by a customer you can use the wc_get_customer_order_count() function

So you get:

JavaScript

Don’t forget to empty your cart to refresh shipping cached data!

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement