php - 未捕获的错误:调用成员函数 get_shipping_packages()
问题描述
我正在尝试获取用户的当前运输区域,但每次尝试获取它时都会出错
// The package.
// Get cart shipping packages
$shipping_packages = $woocommerce->cart->get_shipping_packages();
// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = $woocommerce->wc_get_shipping_zone( reset( $shipping_packages ) );
$zone_id = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name
// Testing output
echo '<p>Zone id: ' . $zone_id . ' | Zone name: ' . $zone_name . '</p>';
错误信息
解决方案
有2个问题:
$woocommerce
未定义全局变量(可以替换为WC()
),wc_get_shipping_zone()
不是 Woocommerce 方法,而是一个函数。
请尝试以下方法(插件见下文):
// Get cart shipping packages
$shipping_packages = WC()->cart->get_shipping_packages();
// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
$zone_id = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name
// Testing output
echo '<p>Zone id: ' . $zone_id . ' | Zone name: ' . $zone_name . '</p>';
它应该工作
对于插件,请尝试
global $woocommerce;
// Get cart shipping packages
$shipping_packages = $woocommerce->cart->get_shipping_packages();
// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
$zone_id = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name
推荐阅读
- kotlin - 我什么时候应该让我的正常功能暂停功能?
- python-3.x - TypeError:输入类型不支持 ufunc 'isnan'
- group-by - 扩展列表以包括 SAS 中的因子变换
- r - flexboard 闪亮的桌子水平和垂直滚动条不起作用
- check-constraints - SQL Server:检查约束表达式
- firebase - 使用 StreamBuilder 从 Firebase(实时数据库)Flutter 获取数据
- java - 如何使用 Talend Open Studio 或在 java 中冻结 .xslx 的标头
- testing - 浏览器堆栈上的文本日志中的 Testcafe “章节”
- apache-flink - 如何在 Apache Flink 作业中获取 System.getProperties()
- python - 在括号之间插入乘号