如何获得来自国家的运费?

最大值

我想这不是真正的重复,我试图以与 woocommerce 相同的方式计算运费,每当客户下订单时,“相关”问题似乎都在谈论为订单设置固定价格......

我正在尝试根据客户所在国家/地区在动态生成的订单(基本上是由 创建的订单wc_create_order())中计算运费,但似乎使用 calculate_totals 方法并没有多大作用。

我尝试在网上搜索但没有任何有用的显示,我尝试使用calculate_shipping()from方法WC_Abstract_Order但没有用,我该怎么做才能计算出正确的运费?

某处是否有一个函数可以返回送货地址的送货价格/费率?

这是我尝试过的片段(我省略了添加项目的部分)


    // Retrieving the shipping and billing address from the POST request
    $shipping = json_decode(stripslashes($_POST['shipping']), true);
    $products_ids = json_decode(stripslashes($_POST['products']), true);

    // Adding them to the order
    $order->set_address($shipping, 'shipping');

    if (isset($_POST['billing'])){
        $bill = json_decode(stripslashes($_POST['billing']), true);
        $bill['email'] = $shipping['email'];
    }
    else {
        $bill = $shipping;
    }

    $order->set_address($bill,'billing');

       ............

    // Calculating the order total based on the items inside the cart 
    // (the calculate_shipping doesn't really do much)
    $order->calculate_shipping();
    $order->calculate_totals();
最大值

因此,经过一番挣扎后,我找到了解决问题的方法,虽然不是一个干净的方法,但仍然没有那么糟糕。这是代码!

if( class_exists( 'WC_Shipping_Zones' ) ) {
    $all_zones = WC_Shipping_Zones::get_zones();

    $country_code = "CN"; //Just testing with a random country code, this should be your input

    foreach ($all_zones as $key => $zona) {
        $total_zones = $zona['zone_locations'];
        // Getting all the zones and iterating them until you find one matching $country_code
        foreach ($total_zonesa as $cur_country_code) {
            if ($cur_country_code->code == $country_code){
                // If you find a match you iterate over shipping_methods, if you don't have more than one flat_rate you should just break this like I did, otherwise you would have to struggle a little bit more with this
                //The $flat_rate->cost is the thing you want to return
                foreach ($zona['shipping_methods'] as $key => $value) {
                    $instance_id = $key;
                    $flat_rate = new WC_Shipping_Flat_Rate($instance_id);
                    print_r($flat_rate->cost);
                    break;
                }
            }
        }
    }
}
return false;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

获得来自MvcResult对象的响应

来自分类Dev

我如何在此表格中获得来自此数据的正确数字?

来自分类Dev

如何获得来自ImageLocation的图片框图像的宽度和高度?

来自分类Dev

如何获得来自不同服务器的CPU / RAM使用率?

来自分类Dev

使用execute()通过Retrofit获得来自错误响应的正文

来自分类Dev

使用execute()通过Retrofit获得来自错误响应的正文

来自分类Dev

如何获得来电者的电话号码?

来自分类Dev

通过Github GraphQL API在一处Promise中获得来自不同阵列的结果

来自分类Dev

Shell脚本可扫描日志以一次性获得来自不同服务器的异常

来自分类Dev

如何按国家查询来自PostGIS的数据?

来自分类Dev

如何获得自治市的国家?

来自分类Dev

如何获得国家及其城市的清单?

来自分类Dev

WooCommerce如何获得用户所在的国家

来自分类Dev

如何获得国家及其城市的清单?

来自分类Dev

如何获得可用的国家和货币?

来自分类Dev

如何在WooCommerce中获得运费和付款方式费用的总和

来自分类Dev

如何在WooCommerce中获得运费和付款方式费用的总和

来自分类Dev

Magento 1.9中运费的地区(非国家)代码

来自分类Dev

在 magento 1.9 中更改国家/地区时未显示运费

来自分类Dev

如何重定向来自特定国家/地区的用户?

来自分类Dev

如何获得所选国家/地区的国定假日

来自分类Dev

我如何获得最大的国家为给定的ArrayList

来自分类Dev

如何获得所选国家/地区的国定假日

来自分类Dev

Swift和Xcode:如何获得默认的国家/地区列表?

来自分类Dev

如何快速获得其他国家时间?

来自分类Dev

如何更好地将所选国家/地区的特定国家/地区边界(来自geoJson文件)加载到传单地图上?

来自分类Dev

如何停止舍入 Woocommerce 中的运费

来自分类Dev

通过Cloudflare获得真实的国家

来自分类Dev

如何使用django模型获得每个国家/地区的产品数量

Related 相关文章

  1. 1

    获得来自MvcResult对象的响应

  2. 2

    我如何在此表格中获得来自此数据的正确数字?

  3. 3

    如何获得来自ImageLocation的图片框图像的宽度和高度?

  4. 4

    如何获得来自不同服务器的CPU / RAM使用率?

  5. 5

    使用execute()通过Retrofit获得来自错误响应的正文

  6. 6

    使用execute()通过Retrofit获得来自错误响应的正文

  7. 7

    如何获得来电者的电话号码?

  8. 8

    通过Github GraphQL API在一处Promise中获得来自不同阵列的结果

  9. 9

    Shell脚本可扫描日志以一次性获得来自不同服务器的异常

  10. 10

    如何按国家查询来自PostGIS的数据?

  11. 11

    如何获得自治市的国家?

  12. 12

    如何获得国家及其城市的清单?

  13. 13

    WooCommerce如何获得用户所在的国家

  14. 14

    如何获得国家及其城市的清单?

  15. 15

    如何获得可用的国家和货币?

  16. 16

    如何在WooCommerce中获得运费和付款方式费用的总和

  17. 17

    如何在WooCommerce中获得运费和付款方式费用的总和

  18. 18

    Magento 1.9中运费的地区(非国家)代码

  19. 19

    在 magento 1.9 中更改国家/地区时未显示运费

  20. 20

    如何重定向来自特定国家/地区的用户?

  21. 21

    如何获得所选国家/地区的国定假日

  22. 22

    我如何获得最大的国家为给定的ArrayList

  23. 23

    如何获得所选国家/地区的国定假日

  24. 24

    Swift和Xcode:如何获得默认的国家/地区列表?

  25. 25

    如何快速获得其他国家时间?

  26. 26

    如何更好地将所选国家/地区的特定国家/地区边界(来自geoJson文件)加载到传单地图上?

  27. 27

    如何停止舍入 Woocommerce 中的运费

  28. 28

    通过Cloudflare获得真实的国家

  29. 29

    如何使用django模型获得每个国家/地区的产品数量

热门标签

归档