How to Bulk Replace a Product Inside Existing WooCommerce Orders

How to Bulk Replace a Product Inside Existing WooCommerce Orders
On this page
  1. Why You'd Ever Need This
  2. Step 1: Trigger the Bulk Replacement
  3. Troubleshooting
  4. Frequently Asked Questions
  5. Conclusion

You can bulk replace a product inside existing WooCommerce orders by looping through past orders with PHP, removing the old line item, and adding the replacement product in its place — useful when a discontinued SKU needs to be swapped retroactively across historical records.

Why You’d Ever Need This

  • A product was merged into a new SKU and reporting needs old orders to reflect the current product
  • An item was mistakenly linked to the wrong product ID during an import and needs correcting in bulk
  • A manufacturer discontinued a product and you’re consolidating historical sales data under its replacement

Important: this kind of operation rewrites order data at scale. Always back up the database and test on staging first.

Step 1: Trigger the Bulk Replacement

The snippet below runs once when a specific admin-only URL parameter is present, looping through orders and swapping any line item matching the old product ID:

PHP
/**
 * Bulk-swap a discontinued product for its replacement across past orders.
 * Trigger by visiting: /wp-admin/index.php?run-product-swap=1
 */
add_action( 'admin_init', 'swap_discontinued_product_in_past_orders' );

function swap_discontinued_product_in_past_orders() {

    if ( ! isset( $_REQUEST['run-product-swap'] ) ) return;
    if ( ! current_user_can( 'manage_woocommerce' ) ) {
        wp_die( esc_html__( 'You do not have permission to run this.', 'woocommerce' ) );
    }

    $old_product_id = 4210;
    $new_product_id = 98765;
    $new_product    = wc_get_product( $new_product_id );
    if ( ! $new_product ) return;

    $order_ids = wc_get_orders( array( 'limit' => -1, 'return' => 'ids' ) );

    foreach ( $order_ids as $order_id ) {
        $order = wc_get_order( $order_id );
        if ( ! $order instanceof WC_Order ) continue;

        foreach ( $order->get_items() as $item_id => $item ) {
            if ( $item->get_product_id() == $old_product_id ) {
                $qty = $item->get_quantity();
                wc_delete_order_item( $item_id );
                $order->add_product( $new_product, $qty );
                $order->add_order_note( 'Line item swapped from product #' . $old_product_id . ' to #' . $new_product_id . ' via bulk update.' );
                $order->save();
            }
        }
    }

    wp_die( 'Product swap complete.' );
}
Bulk replace a product inside existing WooCommerce orders: the page before adding the code
Before
Bulk replace a product inside existing WooCommerce orders: the page after adding the code
After

Troubleshooting

The Script Times Out

On a store with thousands of orders, running the loop against every order at once can exceed your PHP execution time limit. Batch it using paged/limit arguments in wc_get_orders() and run it in smaller chunks instead of all at once.

Totals Look Wrong After the Swap

Removing and re-adding a line item resets its price to the new product’s current price, not the original sale price. If historical pricing accuracy matters, capture and reapply the original line total before deleting the item.

Frequently Asked Questions

Will this affect stock levels?

No, adding a product this way doesn’t automatically reduce stock — it only edits the order’s line items. Adjust stock manually if needed.

Can I limit this to orders within a date range?

Yes, pass date_created arguments to wc_get_orders() to scope the swap to a specific period.

Is there a safer way to test this first?

Yes — temporarily change the wp_die() call to log matched order IDs instead of modifying them, so you can review the list before running the real update.

Conclusion

Bulk replacing a product inside existing WooCommerce orders is a one-off but powerful operation. With a database backup and a staging test first, the snippet above can update thousands of historical orders in seconds instead of hours of manual editing.

See also How to Bulk Search and Replace SKUs in WooCommerce. Then read How to Bulk Edit Product Categories in WooCommerce.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call