dou448172583 2017-09-07 17:51
浏览 112
已采纳

在WooCommerce 3中获取订单发货方法详情

How can I get the order shipping method id....

For example 'flate_rate'.

Since WooCommerce 3.x.x it's not so easy as everything has changed.

I have try it with $order->get_data() in a foreach loop but the data is protected.

  • 写回答

1条回答 默认 最新

  • dongll0502 2017-09-07 23:57
    关注

    If you want to get the Order Items Shipping data, you need first to get them in a foreach loop (for 'shipping' item type) and to use WC_Order_Item_Shipping methods to access data

    $order_id = 528; // For example
    
    // An instance of 
    $order = wc_get_order($order_id);
    
    // Iterating through order shipping items
    foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ){
        $order_item_name             = $shipping_item_obj->get_name();
        $order_item_type             = $shipping_item_obj->get_type();
        $shipping_method_title       = $shipping_item_obj->get_method_title();
        $shipping_method_id          = $shipping_item_obj->get_method_id(); // The method ID
        $shipping_method_instance_id = $shipping_item_obj->get_instance_id(); // The instance ID
        $shipping_method_total       = $shipping_item_obj->get_total();
        $shipping_method_total_tax   = $shipping_item_obj->get_total_tax();
        $shipping_method_taxes       = $shipping_item_obj->get_taxes();
    }
    

    You can also get an array of this (unprotected and accessible) data using the WC_Data method get_data() inside this foreach loop:

    $order_id = 528; // For example
    
    // An instance of 
    $order = wc_get_order($order_id);
    
    // Iterating through order shipping items
    foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ){
        // Get the data in an unprotected array
        $shipping_item_data = $shipping_item_obj->get_data();
    
        $shipping_data_id           = $shipping_data['id'];
        $shipping_data_order_id     = $shipping_data['order_id'];
        $shipping_data_name         = $shipping_data['name'];
        $shipping_data_method_title = $shipping_data['method_title'];
        $shipping_data_method_id    = $shipping_data['method_id'];
        $shipping_data_instance_id  = $shipping_data['instance_id'];
        $shipping_data_total        = $shipping_data['total'];
        $shipping_data_total_tax    = $shipping_data['total_tax'];
        $shipping_data_taxes        = $shipping_data['taxes'];
    }
    

    To finish you can use the following WC_Abstract_Order methods related to "Shipping data", like in this examples:

    // Get an instance of the WC_Order object
    $order = wc_get_order(522);
    
    // Return an array of shipping costs within this order.
    $order->get_shipping_methods(); // same thing than $order->get_items('shipping')
    
    // Conditional function based on the Order shipping method 
    if( $order->has_shipping_method('flat_rate') ) { 
    
        // Output formatted shipping method title.
        echo '<p>Shipping method name: '. $order->get_shipping_method()) .'</p>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么