添加更新或删除 WooCommerce 运输订单项目
Add update or remove WooCommerce shipping order items
我已经为从亚马逊同步的订单添加了运费。出于某种原因,我不得不在为亚马逊订单创建的 woo 订单中设置自定义运费统一价格。具体做法如下:
$OrderOBJ = wc_get_order(2343);
$item = new WC_Order_Item_Shipping();
$new_ship_price = 10;
$shippingItem = $OrderOBJ->get_items('shipping');
$item->set_method_title( "Amazon shipping rate" );
$item->set_method_id( "amazon_flat_rate:17" );
$item->set_total( $new_ship_price );
$OrderOBJ->update_item( $item );
$OrderOBJ->calculate_totals();
$OrderOBJ->save()
问题是,每次亚马逊状态更改时我都必须更新订单,这样做没有问题,问题是如果更新了运费我也必须更新。但是我还没有找到这样做的方法。谁能告诉我如何更新以这种方式设置的订单的运输项目?还是事实是,一旦设置了运输项目,我们就无法更新或删除它?
任何类型的建议都将受到高度赞赏。谢谢
要添加或更新运输项目,请使用以下方法:
$order_id = 2343;
$order = wc_get_order($order_id);
$cost = 10;
$items = (array) $order->get_items('shipping');
$country = $order->get_shipping_country();
// Set the array for tax calculations
$calculate_tax_for = array(
'country' => $country_code,
'state' => '', // Can be set (optional)
'postcode' => '', // Can be set (optional)
'city' => '', // Can be set (optional)
);
if ( sizeof( $items ) == 0 ) {
$item = new WC_Order_Item_Shipping();
$items = array($item);
$new_item = true;
}
// Loop through shipping items
foreach ( $items as $item ) {
$item->set_method_title( __("Amazon shipping rate") );
$item->set_method_id( "amazon_flat_rate:17" ); // set an existing Shipping method rate ID
$item->set_total( $cost ); // (optional)
$item->calculate_taxes( $calculate_tax_for ); // Calculate taxes
if( isset($new_item) && $new_item ) {
$order->add_item( $item );
} else {
$item->save()
}
}
$order->calculate_totals();
它应该更好用……
要删除发货项目,请使用以下方法:
$order_id = 2343;
$order = wc_get_order($order_id);
$items = (array) $order->get_items('shipping');
if ( sizeof( $items ) > 0 ) {
// Loop through shipping items
foreach ( $items as $item_id => $item ) {
$order->remove_item( $item_id );
}
$order->calculate_totals();
}
相关:
可以使用 2 种方法之一将 WC_Order_Item_Shipping
对象添加到订单中。
WC_ORDER->add_shipping( WC_Order_Item_Shipping )
这在 WooCommerce V3 中已弃用。
WC_ORDER->add_item( WC_Order_Item_Shipping )
如果您需要在数据库中保留此更改,请使用 WC_ORDER->save();
参考文献:woocommerce.github.io.../#add_shipping woocommerce.github.io.../#add_item
只需通过 id 获取订单并删除项目
$ordr_id = 4414;
$item_id = 986;
$order = wc_get_order($ordr_id);
$order->remove_item($item_id);
$order->calculate_totals();
我已经为从亚马逊同步的订单添加了运费。出于某种原因,我不得不在为亚马逊订单创建的 woo 订单中设置自定义运费统一价格。具体做法如下:
$OrderOBJ = wc_get_order(2343);
$item = new WC_Order_Item_Shipping();
$new_ship_price = 10;
$shippingItem = $OrderOBJ->get_items('shipping');
$item->set_method_title( "Amazon shipping rate" );
$item->set_method_id( "amazon_flat_rate:17" );
$item->set_total( $new_ship_price );
$OrderOBJ->update_item( $item );
$OrderOBJ->calculate_totals();
$OrderOBJ->save()
问题是,每次亚马逊状态更改时我都必须更新订单,这样做没有问题,问题是如果更新了运费我也必须更新。但是我还没有找到这样做的方法。谁能告诉我如何更新以这种方式设置的订单的运输项目?还是事实是,一旦设置了运输项目,我们就无法更新或删除它? 任何类型的建议都将受到高度赞赏。谢谢
要添加或更新运输项目,请使用以下方法:
$order_id = 2343;
$order = wc_get_order($order_id);
$cost = 10;
$items = (array) $order->get_items('shipping');
$country = $order->get_shipping_country();
// Set the array for tax calculations
$calculate_tax_for = array(
'country' => $country_code,
'state' => '', // Can be set (optional)
'postcode' => '', // Can be set (optional)
'city' => '', // Can be set (optional)
);
if ( sizeof( $items ) == 0 ) {
$item = new WC_Order_Item_Shipping();
$items = array($item);
$new_item = true;
}
// Loop through shipping items
foreach ( $items as $item ) {
$item->set_method_title( __("Amazon shipping rate") );
$item->set_method_id( "amazon_flat_rate:17" ); // set an existing Shipping method rate ID
$item->set_total( $cost ); // (optional)
$item->calculate_taxes( $calculate_tax_for ); // Calculate taxes
if( isset($new_item) && $new_item ) {
$order->add_item( $item );
} else {
$item->save()
}
}
$order->calculate_totals();
它应该更好用……
要删除发货项目,请使用以下方法:
$order_id = 2343;
$order = wc_get_order($order_id);
$items = (array) $order->get_items('shipping');
if ( sizeof( $items ) > 0 ) {
// Loop through shipping items
foreach ( $items as $item_id => $item ) {
$order->remove_item( $item_id );
}
$order->calculate_totals();
}
相关:
可以使用 2 种方法之一将 WC_Order_Item_Shipping
对象添加到订单中。
WC_ORDER->add_shipping( WC_Order_Item_Shipping )
这在 WooCommerce V3 中已弃用。WC_ORDER->add_item( WC_Order_Item_Shipping )
如果您需要在数据库中保留此更改,请使用 WC_ORDER->save();
参考文献:woocommerce.github.io.../#add_shipping woocommerce.github.io.../#add_item
只需通过 id 获取订单并删除项目
$ordr_id = 4414;
$item_id = 986;
$order = wc_get_order($ordr_id);
$order->remove_item($item_id);
$order->calculate_totals();