函数hide_payment_gateways_based_on_weight的临界函数错误的错误错误
尽管该功能似乎在前端很好地工作,但在后端我拥有:
关键的未透露错误:致电成员功能 null in Null in ........
这是函数的get_cart_contents_weight()。
add_filter( 'woocommerce_available_payment_gateways', 'hide_payment_gateways_based_on_weight', 10, 1 );
function hide_payment_gateways_based_on_weight( $available_gateways ) {
if ( is_admin() )
return $available_gateways;
$total_weight = WC()->cart->get_cart_contents_weight();
if ( $total_weight >= 2000 && isset ($available_gateways['cod']) )
unset($available_gateways['cod']); // unset 'cod'
return $available_gateways;
}
Although the function seems to work perfectly on the front end, on the backend I have :
CRITICAL Uncaught Error: Call to a member function
get_cart_contents_weight() on null in........
This is the function.
add_filter( 'woocommerce_available_payment_gateways', 'hide_payment_gateways_based_on_weight', 10, 1 );
function hide_payment_gateways_based_on_weight( $available_gateways ) {
if ( is_admin() )
return $available_gateways;
$total_weight = WC()->cart->get_cart_contents_weight();
if ( $total_weight >= 2000 && isset ($available_gateways['cod']) )
unset($available_gateways['cod']); // unset 'cod'
return $available_gateways;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的功能是在管理员上施加关键错误的原因是,WC管理员中没有购物车。使用
IS_ADMIN()
在WC Admin中不起作用,因此您的$ tocul_weight
声明正在抛出错误。为确保您仅显示购物车或结帐上的网关,请使用这些条件。
另外,无论如何... WPC和PHPC不喜欢内联控制。您应该将if语句包装在括号中,以使其可读性更清洁。
The reason your function is throwing a critical error on the admin is because there is no cart in the WC Admin. Using
is_admin()
doesn't work within the WC admin, so your$total_weight
declaration is throwing the error.To ensure you are only showing the gateways on the cart or checkout, use those conditionals.
Also, for whatever it's worth... WPCS and PHPCS don't like inline controls. You should wrap your if statements in brackets for cleaner readability.
尝试这样的体重。
Try getting the weight like this.
一阶问题是WC() - > CART中的购物车不是定义/null。您应该在尝试获得重量之前检查是否定义。为什么CART为NULL是一个二阶问题,我们无法真正使用此代码段来确定它。这可能只是一个普通的非直接案例或其他一些问题。
The first order problem is that cart in WC()->cart is not defined/null. You should check if that is defined before trying to get the weight. Why cart is null is a second order problem that we can't really determine with this code snippet. It could be just a normal uninitialized case or some other problem.