How to get all woocommerce shipping packages in a custom template

I need to let customer edit their pending payment order. By default, woocommerce only allow change the payment method. So, I have created a custom template for this feature.

The problem now I encountered is I can’t get the shipping packages in the template.
Here is the code that I adapted from the wc_cart_totals_shipping_html() :

Read More
$packages = WC()->shipping->get_packages();
print_r($packages);
foreach ( $packages as $i => $package ) {
    //blah blah blah        
}

The print_r($packages) give me the null array. But on the checkout page, it’s working fine.
Any idea why? Or, get the shipping packages by other method?

Related posts

Leave a Reply

1 comment

  1. Please try this –

    global $woocommerce;
    
    $customerZipCode =  75098;
    $zipResultArr = csd_check_zip_and_state($customerZipCode);
    $bh_packages =  $woocommerce->cart->get_shipping_packages();
    
    $bh_packages[0]['destination']['state'] = $zipResultArr['state'];
    $bh_packages[0]['destination']['postcode'] = $customerZipCode ;
    $bh_packages[0]['destination']['city'] = $zipResultArr['city'];
    $bh_packages[0]['destination']['address'] = '';
    $bh_packages[0]['destination']['address_2'] = '';
    
    //Calculate costs for passed packages
    $bh_shipping_methods = array();
    
    foreach( $bh_packages as $bh_package_key => $bh_package ) {
        $bh_shipping_methods[$bh_package_key] = $woocommerce->shipping->calculate_shipping_for_package($bh_package, $bh_package_key);
    }
    $shippingArr = $bh_shipping_methods[0]['rates'];
    if(!empty($shippingArr)) {
        $response = array();
        foreach ($shippingArr as $value) {
            $shipping['label'] = $value->label;
            $shipping['cost'] = $value->cost;
            $response['shipping'][] = $shipping;
        }
    }
    
    // This is your shipping 
    print_r($response);