3

当传递一些数据时,我们正在尝试返回运费。

例子:

$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个多月以来,这一直非常令人不安。仍然无法解决工作功能。

4

0 回答 0