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:

add_action('woocommerce_new_order', 'custom_shipping_costs',  1, 1);
function custom_shipping_costs($order_id)
{
    $order = wc_get_order($order_id);
    $shippingItems = (array) $order->get_items('shipping');
    foreach ($shippingItems as $item) {
        $item->cost = 0;
    }
    $order->save();
}

Some help please?

Update 1:

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

if ($order_id % 10 == 0) {
    // change shipping price for every 10-th order
}

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):

// Set a count based on placed orders for shipping items cost change
add_action('woocommerce_checkout_create_order', 'action_wc_checkout_create_order', 10, 2);
function action_wc_checkout_create_order($order, $data)
{
    // get $order count for shipping item change
    $orders_count = (int) get_option('wc_orders_count_for_shipping');

    // Increase count for next order (starting count at 2 as this hook is triggered after shipping items hook)
    update_option('wc_orders_count_for_shipping', $orders_count > 0 ? $orders_count + 1 : 2);
}

// Set shipping cost to zero every 10-th orders when order is placed
add_action('woocommerce_checkout_create_order_shipping_item', 'action_wc_checkout_create_order_shipping_item', 10, 4);
function action_wc_checkout_create_order_shipping_item($item, $package_key, $package, $order)
{
    $orders_count = (int) get_option('wc_orders_count_for_shipping');

    // Every 10-th orders 
    if ($orders_count > 0 && ($orders_count % 10) === 0) {
        $item->set_total(0);
        $item->set_taxes(['total' => '0']);
        //$item->set_total_tax('0');
        $item->save();
        $order->calculate_totals();
    }
}

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:

// Set a count based on placed orders for shipping items cost change
add_action( 'woocommerce_checkout_create_order', 'action_wc_checkout_create_order', 10, 2 );
function action_wc_checkout_create_order( $order, $data ) {
    $orders_count = (int) get_option('wc_orders_count_for_shipping'); // get $order count for shipping item change

    // Increase count for next order (starting count at 2 as this hook is triggered after shipping items hook)
    set_option('wc_orders_count_for_shipping', $orders_count > 0 ? $orders_count + 1 : 2 ); 
}

// Set shipping cost to zero every 10-th orders when order is placed
add_action( 'woocommerce_checkout_create_order_shipping_item', 'action_wc_checkout_create_order_shipping_item', 10, 4 );
function action_wc_checkout_create_order_shipping_item( $item, $package_key, $package, $order ) {
    $orders_count = (int) get_option('wc_orders_count_for_shipping');

    // Every 10-th orders 
    if( $orders_count > 0 && ( $orders_count % 10 ) === 0 ) {
        $item->set_total( '0' );
        $item->set_taxes( [ 'total' => '0' ] );
        $item->set_total_tax( '0' );
    }   
} 

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