gpt4 book ai didi

php - 如何在自定义模板中获取所有 woocommerce 运输包裹

转载 作者:行者123 更新时间:2023-12-02 21:18:07 25 4
gpt4 key购买 nike

我需要让客户编辑他们的待处理付款订单。默认情况下,woocommerce 仅允许更改付款方式。因此,我为此功能创建了一个自定义模板。

我现在遇到的问题是无法获取模板中的运输包裹。这是我从 wc_cart_totals_shipping_html() 改编的代码:

$packages = WC()->shipping->get_packages();
print_r($packages);
foreach ( $packages as $i => $package ) {
//blah blah blah
}

print_r($packages) 给我空数组。但在结账页面上,一切正常。知道为什么吗?或者,通过其他方式获取运输包裹?

最佳答案

请尝试这个 -

global $woocommerce;

$customerZipCode = 75098;
$zipResultArr = csd_check_zip_and_state($customerZipCode);
$bh_packages = $woocommerce->cart->get_shipping_packages();

$bh_packages[0]['destination']['state'] = $zipResultArr['state'];
$bh_packages[0]['destination']['postcode'] = $customerZipCode ;
$bh_packages[0]['destination']['city'] = $zipResultArr['city'];
$bh_packages[0]['destination']['address'] = '';
$bh_packages[0]['destination']['address_2'] = '';

//Calculate costs for passed packages
$bh_shipping_methods = array();

foreach( $bh_packages as $bh_package_key => $bh_package ) {
$bh_shipping_methods[$bh_package_key] = $woocommerce->shipping->calculate_shipping_for_package($bh_package, $bh_package_key);
}
$shippingArr = $bh_shipping_methods[0]['rates'];
if(!empty($shippingArr)) {
$response = array();
foreach ($shippingArr as $value) {
$shipping['label'] = $value->label;
$shipping['cost'] = $value->cost;
$response['shipping'][] = $shipping;
}
}

// This is your shipping
print_r($response);

关于php - 如何在自定义模板中获取所有 woocommerce 运输包裹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265257/

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