phpwordpresswoocommerceshipping-method

Accessing Shipping Rate object data in Woocommerce 3


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.


Solution

  • 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.


    You can also get the available shipping packages from the current cart with:

    foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
        $data = WC()->session->get( "shipping_for_package_{$package_key}" );
        foreach ( $data['rates'] as $rate ) {
            echo $rate->get_label(). '<br>';
        
            // Or:
            echo $rate->label. '<br>';
        }
    }
    

    The available properties for WC_Shipping_Rate object are:

    id           // The rate ID (string),
    method_id    // The method ID (string),
    instance_id  // The instance ID (integer),
    label        // The displayed Label name (string),
    cost         // The cost (float),
    taxes        // The taxes (array),
    

    Or the available getter methods are:

    get_id()          // The rate ID (string),
    get_method_id()   // The method ID (string),
    get_instance_id() // The instance ID (integer),
    get_label()       // The displayed Label name (string),
    get_cost()        // The cost (float),
    get_taxes()       // The taxes (array),
    
    get_shipping_tax() // The tax cost (float)
    get_meta_data()    // The metadata for the rate (array)
    

    Note that you can use the properties or the methods on a WC_Shipping_Rate object.