'Set Fix Discount on Shipping based on X Quantities in Woocommerce 5.9+

Based on total Qty or units of items, I want to give X amount of discount on Shipping price, that I set as and under the Flat rate Shipping charges for the Specific Zone or country. Say, Country - India, in our Case.

We have defined a Flat Rate for India Country and name it as Standard. Then for one of the Class, we set the prices as -

40+ ( 40 * [qty] )

https://i2.paste.pics/FXPE0.png

The Title of Flat Rate Shipping Method is Std. or Free, as can be seen below- https://i2.paste.pics/FYK4D.png We are looking to have the solution of Shipping discount for this Flat Rate shipping method only. (and not for Express, as can be seen from the Screenshot)

So what is currently happening - Say, if a customer purchases 1 Qty of any product (that falls under this Shipping Class), the Shipping price is Rs. 80 For 2 units, it is 120, for 3 units it is 160 and so on. This condition (set in Shipping Class), we are currently applying on products from other categories. But want something specific (as below), for products from other Categories.

And What we are looking for- If customer purchase any item from Specified Categories (to be defined in array), with First Quantity, the Shipping price should be as it is, that is Rs. 80 For 2 to 3 Quantities, it should again be same Rs. 80 (means, no further increment of Rs. 40 per Qty.). Or a discount of Rs. 40 per Qty upto 3 units.

And then, from 4-6 quantities or units in Cart, it should then follow the above rule, that is -

40+ ( 40 * [qty] )

Means, additional Rs. 40 (for Shipping Charges) from 4th units onwards and upto 6 units in cart.

That is, for 4-6 quantities, the shipping price should be 80+40 = 120 Similarly, fFor 7-9 Quantities, the shipping price should be 40 more. That 120+40 = 160.. and so on. We want this rule to be for All products that falls under Specific Categories, say Kurtis, Bags

Similarly, for ALL products that have specific Product Tags, say designer-kurti, handbag

PS: If above condition can be met with Code snippet, without mentioning the Shipping Class to any product, then also, it would be OK. We just want this pricing rule or condition to be applied on products under Specific Categories, or Specific Product Tags

I tried to look at the Solutions posted by users @7uc1f3r and @LoicTheAztec, in below Thread Questions here, but could not quite get through this and as per my specific query. Percentage discount on shipping based on order total in WooCommerce

This below code solution works on percentage discount. We want to have Fixed amount of discount on Shipping on per Qty basis, in the Cart for X Categories or products with that specific Shipping Class

Also, in below solution, it requires to set a Cost amount as 1 for the Flat Rate method, that will affect ALL Classes, at once, as shown here. https://i.stack.imgur.com/DjmMX.png

But we already have few Classes for our Flat Rate method (as can be seen from Screenshot, we shared above)

Woocommerce conditional shipping cost reduction based on cart amount and item count

I am new to PHP, and could not find to make it work. Can someone please help me on this.

Regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source