How to modify an order in WooCommerce using code

Sometimes you may need to programmatically modify certain aspects of an order in WooCommerce — whether it’s to correct data, apply custom business logic, or automate administrative workflows.

 

Important: HPOS Compatibility

WooCommerce has introduced HPOS (High-Performance Order Storage) as part of its performance improvements. This system stores orders in custom database tables, making WooCommerce more scalable and efficient.

Because of this, you should no longer use update_post_meta()or get_post_meta()when working with orders. These functions are not fully compatible with HPOS.

  • Avoid using update_post_meta() for orders. Instead, use the order object’s methods like set_billing_first_name(), get_meta(), save_meta_data(), etc.
  • HPOS is already available as an option in WooCommerce and will soon become the default for new stores. Now’s the time to future-proof your code.

 

Load the Order


 

Increase the Quantity of a Specific Product in the Order

get_items() as $itemId => $item ) {
    $product = $item->get_product();
    
    if ( $product && $product->get_sku() === 'product-sku' ) {
        $currentQty = $item->get_quantity();
        $item->set_quantity( $currentQty + 1 );
    }
}

$order->calculate_totals();
$order->save();

 

Change the Price of a Product in the Order

get_items() as $itemId => $item ) {
    $product = $item->get_product();

    if ( $product && $product->get_id() === 456 ) { // Product ID
        $item->set_total( 20.00 );     // Price (excluding taxes)
        $item->set_subtotal( 20.00 );
    }
}

$order->calculate_totals();
$order->save();

 

Change the Total Price of the Order

set_total( 75.00 );
$order->save();

Use with caution: this won’t update the line items automatically. It’s for specific use cases only.

 

Modify Billing Information

set_billing_first_name( 'Carlos' );
$order->set_billing_last_name( 'Perez' );
$order->set_billing_email( 'carlos@example.com' );
$order->set_billing_phone( '987654321' );

$order->save();

 

Modify Shipping Information

set_shipping_first_name( 'Laura' );
$order->set_shipping_last_name( 'Gomez' );
$order->set_shipping_address_1( '123 Fake Street' );
$order->set_shipping_city( 'Lima' );
$order->set_shipping_postcode( '15001' );

$order->save();

 

Additional Notes

  • Always call $order->calculate_totals(); before saving if you’re changing prices or quantities.
  • For custom fields or metadata, use $order->update_meta_data()$order->get_meta() and $order->update_meta_data() instead of post meta functions.
  • Make sure the order exists and the current user has permission to modify it.

 

Modifying WooCommerce orders via code is powerful — but must be done responsibly and in line with current standards. With HPOS becoming the default storage engine, it’s crucial to move away from legacy methods and embrace the modern order object API.

Have a specific case in mind? Drop a comment and let’s take a look together.

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Back to Top
0
Would love your thoughts, please comment.x
()
x