Skip to content
Advertisement

Set a custom shipping cost on every 10th Order in WooCommerce

WP 5.3.3

I need to change shipping cost programmatically, after order creation.

This code doesn’t affect:

JavaScript

Some help please?

Update 1:

It’s important that I need to change shipping price in every n-th order. Something like this:

JavaScript

Update 2 (the solution):

Thanks @LoicTheAztec – the solution is based on his answer, with a little changes:

  1. set_option -> update_option
  2. set_total_tax throws an error, so I changed this part a bit (This thread helped me).

The final code (goes in functions.php file of your active child theme or active theme):

JavaScript

Advertisement

Answer

First as Order IDs are not sequential because they are based on the post ID that is used also on WordPress pages, posts and all other custom posts like Woocommerce products and coupons. So we need to enable a sequential count on your WooCommerce orders to make changes on every 10th orders.

The following will set shipping costs to zero every 10th orders, when order is placed before saving order data to database:

JavaScript

Code goes in functions.php file of your active child theme (or active theme). It should works.

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