woocommerce – return shipping rates function

We’re trying to return shipping rates when passed a little bit of data.

example:

Read More
$ship_data = array("country" => "US",  "state" => "MD",  "zip" => "21228" );
$product_data = array( "id" => "1" );

function get_all_shipping_rates( $ship_data , $product_data )
{
      // do something and get the rates (just like a shipping calculator)
     // calculate the shipping using product id 1 and the ship data

      return $rates;     //  array showing the name and costs ie (UPS ground $10, Flat Rate $5, etc)
}

Issue- we’re trying to do this programmatically without adding a product to the cart. The data will be used to display certain shipping rate details on the single product page (or in another plugin).

We wrote a function that will get this same info once you add a product to the woocommerce cart , and on the cart page the below function works perfectly:

$aval_shipping_methods = array();

    $package_rates = $woocommerce->shipping->packages[0]['rates'];
    foreach($package_rates as $shipping_method) {

        $aval_shipping_methods[] = array(
                        'typeId' => $shipping_method->id,
                        'type' => $shipping_method->label,
                        'cost' => ($shipping_method->cost * 100)
                    );
    }
    //echo "<pre>";
    print_r($aval_shipping_methods);

It seems very simple at first: If I had the customers address and the product they want, there should be a way to quickly get the shipping method rates/costs. (we do not care about ship taxes or shipping classes).

This has been very troubling for over 2 months. Still can’t resolve a working function.

Related posts