gpt4 book ai didi

php - 在 WooCommerce 中计算现有订单中的运输方式/费率

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:34:17 27 4
gpt4 key购买 nike

我基本上是在尝试复制购物车页面上的功能,用户可以在其中添加他们的 zip code 并计算可用的运费,但我试图从后端进行来自已创建的订单。

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

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

$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.

最佳答案

好的,多亏了@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'] 将包含订单内产品在该位置可用的所有运费。

关于php - 在 WooCommerce 中计算现有订单中的运输方式/费率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55151092/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com