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.

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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序