未捕获的错误:调用成员函数get_shipping_packages() [英] Uncaught Error: Call to a member function get_shipping_packages()

查看:81
本文介绍了未捕获的错误:调用成员函数get_shipping_packages()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户的当前送货地区,但每次尝试获取时都会收到错误消息

I am trying to get the current shipping zone of the user but I am getting an error every time I am trying to get it

// 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方法,而是函数.
  • $woocommerce global variable is not defined (and it's can be replaced by WC()),
  • wc_get_shipping_zone() is not a Woocommerce method but a function.

请尝试以下操作,而不是(有关插件,请参见下文):

Try the following instead (for plugins see below):

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

这篇关于未捕获的错误:调用成员函数get_shipping_packages()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆