添加更新或删除 WooCommerce 运输订单项目 [英] Add update or remove WooCommerce shipping order items

查看:70
本文介绍了添加更新或删除 WooCommerce 运输订单项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为从亚马逊同步的订单添加了运费.出于某种原因,我不得不在为亚马逊订单创建的 woo-orders 中设置自定义运输统一价格.操作如下:

I have added shipping cost for the orders that are synced from amazon. For some reason I had to set custom shipping flat price in woo-orders created for amazon-order. It is done as follow:

    $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()

问题是,我必须在每次亚马逊状态更改时更新订单,这样做没有问题,问题是如果更新,我也必须更新运费.但我还没有发现无论如何要这样做.谁能告诉我如何更新以这种方式设置的订单的运输项目?或者是因为一旦设置了运输项目,我们就无法更新或删除它?任何类型的建议都受到高度赞赏.谢谢.

The problem is, I have to update orders in each time the status is changed in amazon, there is no problem doing that, problem is I have to update the shipping cost also if it is updated. But I have not found anyway to do so. Can anyone tell me how to update the shipping items of orders set in this way ? Or is it the fact that, once shipping item is set then we cannot update or delete it ? Any kinds of suggestion are highly appreciated. Thanks.

推荐答案

要添加或更新运输项目,请使用以下内容:

To add or update shipping items use the following:

$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();

它应该更好地工作......

It should better work…

要删除运输项目,请使用以下命令:

To remove shipping items use te following:

$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();
}

<小时>

相关:添加运费在 Woocommerce 3 中以编程方式下单

这篇关于添加更新或删除 WooCommerce 运输订单项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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