在WooCommerce中计算现有订单中的送货方式/费率

时间:2019-03-13 20:58:50

标签: php wordpress woocommerce orders shipping-method

我本质上是在尝试复制购物车页面上的功能,用户可以在其中添加他们的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.

3 个答案:

答案 0 :(得分:2)

尝试使用:

// 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

答案 1 :(得分: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']将具有该地点可用于订单内产品的所有运费。

答案 2 :(得分:0)

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

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

WC()->cart->empty_cart()

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

到目前为止,似乎必须使用购物车来正确计算运输包装。如果可以的话,有什么方法可以创建当前购物车的副本,然后计算运输包裹?在执行此计算时,我不想影响客户的原始购物车。