Md *_*lam 2 php wordpress woocommerce hook-woocommerce woocommerce-bookings
我正在尝试获取用户当前的送货区域,但每次尝试获取时都会收到错误
// 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>';
Run Code Online (Sandbox Code Playgroud)
错误信息
有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>';
Run Code Online (Sandbox Code Playgroud)
应该有效
对于插件,请尝试
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
706 次 |
| 最近记录: |