我需要在woocommerce订单中更改商品价格,但我发现的一切都是更改购物车中的价格,但这不是我需要的,因为我需要在结帐过程后更改。
有人可以给我一个如何做到这一点的线索吗?
您需要使用 WooCommerce 3 引入的新 CRUD setters 方法:
- 对于订单对象,您将使用
WC_Order
方法, - 对于订单"行项目",您将使用
WC_Order_Item_Product
方法, - 对于它们,您也可以使用一些
WC_Data
方法,例如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_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();`
@LoicTheAztec
通过手动输入标识符完成自动商业付款
The shopper goes online, creates an account , adds a payment method, and fills their cart .
We hold the amount plus 15% when they check out.
woocommerce sends order details to the delivery team that takes the gig .
They go to the store and shop
After checking out at the physical store, the new invoice total is uploaded to woo-commerce via the shopping app .
This manually entered amount will be the IDENTIFIER in the stripe that TRIGGERS the order completion