You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Woocommerce 3中修改结账后的订单商品价格?

Got it, I totally get what you're asking here—you don't want to tweak prices in the cart, you need to adjust order item prices after checkout is fully completed. Let's break down the practical ways to do this, either for one-off manual changes or automated/programmatic scenarios:


1. Manual Price Adjustment (For Occasional Changes)

If you just need to edit a single order here and there, the WooCommerce admin interface has you covered:

  • Head to your WooCommerce dashboard → Orders, find the target order and open its detail page
  • Scroll down to the "Order Items" section, click the small pencil icon (edit button) next to the item you want to modify
  • In the popup window, directly update the "Price" field (this controls the unit price for that item in the order)
  • Hit "Save" on the popup, then don't forget to click Update at the bottom of the order page to lock in all changes

2. Programmatic Adjustment (For Bulk/Automated Workflows)

If you need to adjust prices at scale or trigger changes automatically (e.g., when an order hits "Completed" status), use WooCommerce's official functions to safely modify order data without breaking core functionality.

Example 1: Modify a Specific Order's Item Price

This function lets you target a single order and update a specific product's price within it:

function update_target_order_item_price( $order_id, $target_product_id, $new_unit_price ) {
    // Fetch the order object
    $order = wc_get_order( $order_id );
    if ( ! $order ) return;

    // Loop through all items in the order
    foreach ( $order->get_items() as $item_id => $item ) {
        // Match the product we want to update
        if ( $item->get_product_id() === $target_product_id ) {
            // Update subtotal and total (account for quantity)
            $item->set_subtotal( $new_unit_price * $item->get_quantity() );
            $item->set_total( $new_unit_price * $item->get_quantity() );
            
            // Recalculate taxes if your store uses them
            $item->calculate_taxes();
            
            // Save the updated item
            $item->save();
        }
    }

    // Recalculate and save the full order totals
    $order->calculate_totals();
    $order->save();
}

// Usage example: Update product ID 45 in order ID 123 to $99.99
// update_target_order_item_price( 123, 45, 99.99 );

Example 2: Auto-Adjust Prices When Orders Marked Completed

If you want price changes to trigger automatically when an order reaches the "Completed" status, hook into WooCommerce's order status action:

add_action( 'woocommerce_order_status_completed', 'auto_adjust_prices_on_order_completion', 10, 1 );
function auto_adjust_prices_on_order_completion( $order_id ) {
    $order = wc_get_order( $order_id );
    if ( ! $order ) return;

    // Custom logic here: e.g., apply 10% discount to all items in completed orders
    foreach ( $order->get_items() as $item_id => $item ) {
        $original_price = $item->get_product()->get_regular_price();
        $discounted_price = $original_price * 0.9;

        $item->set_subtotal( $discounted_price * $item->get_quantity() );
        $item->set_total( $discounted_price * $item->get_quantity() );
        $item->calculate_taxes();
        $item->save();
    }

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

Drop this code into your theme's functions.php file or a custom plugin to activate it.

Key Notes for Programmatic Changes

  • Backup First: Always back up your database before modifying order data programmatically
  • Tax Handling: If your store uses taxes, the calculate_taxes() method ensures tax amounts update alongside prices
  • Payment Sync: Modifying order prices won't automatically trigger refunds or additional charges—you'll need to handle payment adjustments manually via your payment gateway
  • Order History: WooCommerce logs order changes, so customers will see price updates in their order history if you have order logging enabled

内容的提问来源于stack exchange,提问作者Sergio Ríos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:51:29