如何从 WooCommerce 获取自定义运输方式包 [英] How to get custom shipping method packages from WooCommerce

查看:82
本文介绍了如何从 WooCommerce 获取自定义运输方式包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据 WooCommerce Shipping Method API文档.它工作正常并且在计算购物车和结帐页面上的价格时正在做我需要的事情.此外,我在结帐页面上有验证功能,并且我正在获取包裹,然后将其与所选方法进行比较.

I wrote custom shipping method according to WooCommerce Shipping Method API documentation. It works fine and is doing what I need when calculating price on cart and checkout page. Also I have validation function on checkout page and and I'm getting packages, then comparing it to chosen methods.

代码如下:

$packages = WC()->shipping->get_packages();
$chosen_methods = wc_get_chosen_shipping_method_ids();

if (is_array($chosen_methods) && (in_array('new_standard', $chosen_methods) || in_array('new_fast', $chosen_methods) || in_array('new_express', $chosen_methods))) {        
    foreach ($packages as $i => $package) {         
        if ($chosen_methods[$i] != 'new_standard' && $chosen_methods[$i] != 'new_fast' && $chosen_methods[$i] != 'new_express') {
            continue;               
        }           
        // Here some validation code
        if ($package['destination']['city'] == 'Paris') {
            // Bla bla bla
        }
    }       
}

你能帮我理解为什么我需要为一个订单提供多个包裹吗?为什么我要获取运输方法的数组 wc_get_chosen_shipping_method_ids() 而不是获取方法的一个 id.为什么我要使用 foreach 循环来实现,因为它可以像这样更直接:

Can you help me to understand why do I need to have multiple packages for single order. Why I'm getting array for shipping methods wc_get_chosen_shipping_method_ids() instead of getting one id of method. Why I'm using foreach loops to get there, when it can be more straight forward like this:

// This is just example code of mine for showcase
$package = WC()->shipping->get_package();
if ($package['id'] == 'new_standard' || $package['id'] == 'new_fast' || $package['id'] == 'new_express') {
// validation code here
}

另外一个主要问题是:我怎样才能获得包并将它们与 WC_Order 一起使用.我需要调用一些外部 API,为此我需要做与上述相同的事情.

Also the main question is: How can I get packages and use them with WC_Order. I need to call some external API and for this I need to do same thing what is described above.

推荐答案

因为在使用某些 3rd Party 运输相关插件或某些自定义代码时,您可以将购物车拆分为多个运输包裹,因此您可以有多种运输方式(每个运输包裹一个).

Because when using some 3rd Party shipping related plugins or some custom code, you can split the cart into multiple shipping packages, and so you can have multiple shipping methods (one per shipping package).

想象一下,您销售的产品可能很重,也可能不重……想象一下,您允许通过飞机、船只和卡车运输……有些物品可以通过飞机运输,有些则不取决于它们的重量.
因此,当购物车中有混合商品(重型和普通)时,购物车将被拆分为不同的运输包裹.

Imagine that you sell products that can be heavy or not… Imagine that you allow shipping by plane, by boat and by truck… Some items will be shippable by plane some others will not depending on their weight.
So when there are mixed items in cart (heavy and normal), the cart will be split into different shipping packages.

这就是为什么有一系列选择的运输方式和一系列运输包裹(即使您不拆分购物车或不使用这些插件).

So that's why there is an array of chosen shipping methods and an array of shipping packages (even if you don't split your cart or don't use those plugins).

因此,只有少数 WooCommerce 商店使用多种运输包裹,用于混合购物车项目.

So there is only few WooCommerce shops that use multiple shipping packages, for mixed cart items.

WooCommerce 订单上运输方式的相关主题:

Related threads for shipping methods on WooCommerce orders:

这篇关于如何从 WooCommerce 获取自定义运输方式包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆