我们试图在传递一些数据时返回运费 .

例:

$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)
}

问题 - 我们试图以编程方式执行此操作而无需将产品添加到购物车 . 该数据将用于在单个产品页面(或另一个插件)上显示某些运费率详细信息 .

我们编写了一个函数,一旦您将产品添加到woocommerce购物车就会获得相同的信息,并且在购物车页面上,以下功能可以完美地运行:

$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);

一开始看起来很简单:如果我有客户地址和他们想要的产品,应该有办法快速获得运费方式的费率/成本 . (我们不关心船舶税或运输类) .

这已经超过2个月非常麻烦了 . 仍然无法解决工作功能 .