dsf1222 2018-10-11 11:53
浏览 26
已采纳

在Woocommerce 3中访问Shipping Rate对象数据

I want to get the possible shipping methods in Woocommerce. I can get to the Class WC_Shipping_Rate class but i don't know how to get 'deeper' and it's driving me crazy.

Here's my code:

$packages = WC()->shipping->get_packages();

foreach ( $packages as $i => $package ) {

    foreach($package['rates'] as $value){
        echo '<pre>'.var_export($value, true).'</pre>';
    }

}

This piece of code prints this on my screen:

WC_Shipping_Rate::__set_state(array(
   'data' => 
  array (
    'id' => 'flat_rate:1',
    'method_id' => 'flat_rate',
    'instance_id' => 1,
    'label' => 'Per post',
    'cost' => '2.50',
    'taxes' => 
    array (
    ),
  ),
   'meta_data' => 
  array (
    'Items' => '1 Kip × 1',
  ),
))
WC_Shipping_Rate::__set_state(array(
   'data' => 
  array (
    'id' => 'free_shipping:2',
    'method_id' => 'free_shipping',
    'instance_id' => 2,
    'label' => 'Download kaart',
    'cost' => '0.00',
    'taxes' => 
    array (
    ),
  ),
   'meta_data' => 
  array (
    'Items' => '1 Kip × 1',
  ),
))

My question is: How can i access the data array and then the label value? I know how to get the label value, but i cant 'reach' the data...

I have tried to get it using methods, more arrays but the WC_Shipping_Rate::__set_state(array( is confusing me. Any help would be appriciated.

  • 写回答

1条回答 默认 最新

  • duanlun2827 2018-10-11 13:33
    关注

    Try the following using WC_Shipping_Rate methods or directly the properties:

    // Loop though shipping packages
    foreach ( WC()->shipping->get_packages() as $key => $package ) {
        // Loop through Shipping rates
        foreach($package['rates'] as $rate_id => $rate ){
    
            echo $rate->get_label(). '<br>';
    
            // Or:
            echo $rate->label. '<br>';
        }
    }
    

    Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站