Php 更改Woocommerce 3中的订单项目价格

Php 更改Woocommerce 3中的订单项目价格,php,wordpress,woocommerce,orders,price,Php,Wordpress,Woocommerce,Orders,Price,我需要更改商品订单中的商品价格,但我发现的一切都是更改购物车中的价格,但这不是我需要的,因为我需要在结账过程后更改 有人能告诉我怎么做吗?您需要使用Woocommerce 3推出的新功能: 对于要使用的订单对象 对于订单“行项目”,您将使用 对于这两种方法,您也可以使用类似的save() 下面是一个使用静态价格和静态订单ID的基本示例: $order_id = 809; // Static order Id (can be removed to get a dynamic order ID

我需要更改商品订单中的商品价格,但我发现的一切都是更改购物车中的价格,但这不是我需要的,因为我需要在结账过程后更改

有人能告诉我怎么做吗?

您需要使用Woocommerce 3推出的新功能:

  • 对于要使用的订单对象
  • 对于订单“行项目”,您将使用
  • 对于这两种方法,您也可以使用类似的
    save()
下面是一个使用静态价格和静态订单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\u id
..

非常感谢,我花了4个小时寻找如何根据您的代码更改订单中产品的数量(我重写了必要的部分)我终于明白了!如果有人需要更改订单中产品的数量`

$order = wc_get_order( $_POST['orderID'] );

foreach( $order->get_items() as $item_id => $item ){
    $product = $item->get_product();
    $product_price = (int) $product->get_price(); // A static replacement product price
    $new_quantity = (int) $_POST['productQty'] // product Quantity
    
    // The new line item price
    $new_line_item_price = $product_price * $new_quantity;
    
    // Set the new price
    $item->set_quantity($_POST['orderQty']);
    $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();`

你到底什么时候需要这样做以及如何做(或者上下文是什么)…实际上你的问题还不清楚。嗨,很抱歉,这个想法是当客户下订单时,送货员去购买产品,但价格是可变的,所以送货员需要添加一个具有特定价格的普通产品,因此,这一刻是在客户下订单之后,在收到付款之前(货到付款),因为我建立了一个显示当前订单的页面,在那里我需要在文本字段中填写价格,并用这个特定的价格添加一般产品。我终于回答了…对此的一些反馈将非常感谢。谢谢,谢谢你!这为我节省了时间。我试图使用一个名为“你的价格插件”的定制捐赠表单。由于习惯的形式,当有人捐赠超过最低捐赠额时,捐赠价格和总金额会有所不同。这帮助我改变了捐款的价格,并将其正确地反映在他们通过电子邮件发送的收据上。非常感谢。谢谢,这对我来说也很好。我只想请hot exclude计算小计,只更改产品价格,不更改其他内容?想知道是否可以设置总包含税?