函数hide_payment_gateways_based_on_weight的临界函数错误的错误错误

发布于 2025-01-23 17:45:28 字数 713 浏览 1 评论 0原文

尽管该功能似乎在前端很好地工作,但在后端我拥有:

关键的未透露错误:致电成员功能 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠北森屋 2025-01-30 17:45:28

您的功能是在管理员上施加关键错误的原因是,WC管理员中没有购物车。使用IS_ADMIN()在WC Admin中不起作用,因此您的$ tocul_weight声明正在抛出错误。

为确保您仅显示购物车或结帐上的网关,请使用这些条件。

另外,无论如何... WPC和PHPC不喜欢内联控制。您应该将if语句包装在括号中,以使其可读性更清洁。

add_filter( 'woocommerce_available_payment_gateways', 'hide_payment_gateways_based_on_weight', 10, 1 );
function hide_payment_gateways_based_on_weight( $available_gateways ) {
    // Check if it's the cart page or the checkout.
    if ( is_cart() || is_checkout() ) {
        $total_weight = WC()->cart->get_cart_contents_weight();
        if ( 2000 >= $total_weight && isset( $available_gateways['cod'] ) ) {
            unset( $available_gateways['cod'] ); // unset 'cod'.
        }
    }
    return $available_gateways;
}

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.

add_filter( 'woocommerce_available_payment_gateways', 'hide_payment_gateways_based_on_weight', 10, 1 );
function hide_payment_gateways_based_on_weight( $available_gateways ) {
    // Check if it's the cart page or the checkout.
    if ( is_cart() || is_checkout() ) {
        $total_weight = WC()->cart->get_cart_contents_weight();
        if ( 2000 >= $total_weight && isset( $available_gateways['cod'] ) ) {
            unset( $available_gateways['cod'] ); // unset 'cod'.
        }
    }
    return $available_gateways;
}
万劫不复 2025-01-30 17:45:28
$WC_Cart = new WC_Cart();
$total_weight  = $WC_Cart->get_cart_contents_weight();

尝试这样的体重。

$WC_Cart = new WC_Cart();
$total_weight  = $WC_Cart->get_cart_contents_weight();

Try getting the weight like this.

关于从前 2025-01-30 17:45:28

一阶问题是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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文