Home > database >  CRITICAL Uncaught Error for function hide_payment_gateways_based_on_weight
CRITICAL Uncaught Error for function hide_payment_gateways_based_on_weight

Time:04-23

Although the function seems to work perfectly, 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;         
     }

any help???

CodePudding user response:

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

Try getting the weight like this.

CodePudding user response:

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.

  • Related