【问题标题】:Calculate shipping methods/rates within an existing order in WooCommerce在 WooCommerce 中计算现有订单中的运输方式/费率
【发布时间】:2019-08-04 16:09:37
【问题描述】:

我实际上是在尝试复制购物车页面上的功能,用户可以在其中添加他们的zip code 并计算可用的运费,但我试图从后端从已经创建的顺序。

我找不到直接从WC_Order 实例执行此操作的方法,因此我要做的下一个最好的事情是清除购物车会话,将订单中的所有项目添加到购物车会话,然后尝试计算它。

这是我目前所拥有的。我总是纠结于如何计算整个订单的费率。

$order_id       = isset($_POST['order_id'])?$_POST['order_id']:0;
$country        = isset($_POST['country'])?$_POST['country']:0;
$state          = isset($_POST['state'])?$_POST['state']:0;
$postcode       = isset($_POST['postcode'])?$_POST['postcode']:0;
$city           = isset($_POST['city'])?$_POST['country']:0;
$order          = wc_get_order( $order_id );
$order_items    = $order->get_items();

// Don't know if this would save country of logged in user, or only create a temporary guest user session which is what I'd need
if ( $country != '' ) {
    WC()->customer->set_billing_location( $country, $state, $postcode, $city );
    WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
} else {
    WC()->customer->set_billing_address_to_base();
    WC()->customer->set_shipping_address_to_base();
}

// Remove all current items from cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
    WC()->cart->empty_cart();
}

// Add all items from the order to the cart
foreach ($order_items as $order_item) {
    WC()->cart->add_to_cart($order_item['product_id'], $order_item['qty']);
}

$totals = WC()->shipping->get_packages();

// $totals returns rates but I believe it is per each "package". It's not a cumulative rate like the cart page shows.

【问题讨论】:

标签: php wordpress woocommerce orders shipping-method


【解决方案1】:

注意:这不是答案,我无法发表评论,因此我在这里写。 就我而言,接受的答案存在问题:

如果客户已经在购物车中添加了一些商品,它们将会因为以下代码行而丢失:

WC()->cart->empty_cart()

相反,购物车由过去订单中的商品填充。

到目前为止,似乎有必要使用购物车来正确计算运输包裹。如果是这样,有没有办法可以创建当前购物车的克隆,然后计算运输包裹?在进行此计算时,我不想影响客户的原始购物车。

【讨论】:

  • 我的问题和答案适用于从 WordPress 后端计算运输方式。因此,它通常只能由管理员或管理成员完成,在这种情况下,清空购物车并不重要,因为它不会清空您要计算其运输方式的用户的购物车。它清空当前登录用户的购物车。我会尝试类似$cart_contents = WC()->cart->get_cart_contents() 然后WC()->cart->empty_cart() 和所有其他逻辑,最后是WC()->cart->set_cart_contents($cart_contents)。没有测试过。
【解决方案2】:

好的,多亏了@mujuonly,我才弄明白了。

以下是获取所有计算运费的方法,与购物车页面上显示的方式相同。

// Post variables
$order_id   = isset($_POST['order_id'])?$_POST['order_id']:0;
$country    = isset($_POST['country'])?$_POST['country']:0;
$state      = isset($_POST['state'])?$_POST['state']:0;
$postcode   = isset($_POST['postcode'])?$_POST['postcode']:0;
$city       = isset($_POST['city'])?$_POST['city']:0;

// Order and order items
$order          = wc_get_order( $order_id );
$order_items    = $order->get_items();

// Reset shipping first
WC()->shipping()->reset_shipping();

// Set correct temporary location
if ( $country != '' ) {
    WC()->customer->set_billing_location( $country, $state, $postcode, $city );
    WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
} else {
    WC()->customer->set_billing_address_to_base();
    WC()->customer->set_shipping_address_to_base();
}

// Remove all current items from cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
    WC()->cart->empty_cart();
}

// Add all items to cart
foreach ($order_items as $order_item) {
    WC()->cart->add_to_cart($order_item['product_id'], $order_item['qty']);
}

// Calculate shipping
$packages = WC()->cart->get_shipping_packages();
$shipping = WC()->shipping->calculate_shipping($packages);
$available_methods = WC()->shipping->get_packages();

$available_methods[0]['rates'] 将获得该位置订单中产品可用的所有运费。

【讨论】:

  • 您能否发送购物车中的完整代码以获取此解决方案?
  • 您能否分享一下您是如何实现这一点的,以便其他人可以对他们的 WC 后端做同样的事情?
  • @inspirednz 它是作为后端管理手动订单的插件实现的。
【解决方案3】:

尝试使用:

// Calculate totals
WC()->cart->calculate_totals();
WC()->cart->calculate_shipping();

// Retrieve the shipping total
$shipping_total = WC()->cart->get_shipping_total();

WC_Cart 类应该具有重新创建您可能需要的任何购物车功能所需的所有方法。我建议你应该通读并熟悉这里的类定义......

https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 2020-08-31
    • 2014-09-25
    • 2019-12-18
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    相关资源
    最近更新 更多