在 Woocommerce 3 中更改订单商品价格 [英] Change Order item prices in Woocommerce 3

查看:61
本文介绍了在 Woocommerce 3 中更改订单商品价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改 woocommerce 订单中的商品价格,但我发现的所有内容都是更改购物车中的价格,但这不是我需要的,因为我需要在结帐流程后更改.

I need to change the item price in a woocommerce order but everything I found is to changing the price in the cart but this is not what I need because I need to change after the checkout process.

有人可以告诉我怎么做吗?

Does somebody can give me a clue on how to do that?

推荐答案

您需要使用新的 CRUD setter 方法:

  • For order object you will use WC_Order methods,
  • For order "line item" you will use WC_Order_Item_Product methods,
  • For both of them you could be also use some WC_Data methods like save()

这是一个带有静态价格和静态订单 ID 的工作基本示例:

Here is a working basic example with a static price and a static order ID:

$order_id = 809; // Static order Id (can be removed to get a dynamic order ID from $order_id variable)

$order = wc_get_order( $order_id ); // The WC_Order object instance

// Loop through Order items ("line_item" type)
foreach( $order->get_items() as $item_id => $item ){
    $new_product_price = 50; // A static replacement product price
    $product_quantity = (int) $item->get_quantity(); // product Quantity
    
    // The new line item price
    $new_line_item_price = $new_product_price * $product_quantity;
    
    // Set the new price
    $item->set_subtotal( $new_line_item_price ); 
    $item->set_total( $new_line_item_price );

    // Make new taxes calculations
    $item->calculate_taxes();

    $item->save(); // Save line item data
}
// Make the calculations  for the order and SAVE
$order->calculate_totals();

然后,您必须将静态价格替换为您在自定义页面中提交的新价格,这不是那么简单,因为您需要定位正确的 $item_id...

Then you will have to replace the static price by your submitted new price in your custom page, which is not so simple, as you will need to target the correct $item_id

这篇关于在 Woocommerce 3 中更改订单商品价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆