Skip to content
Advertisement

How to fix “Uncaught Error: Call to a member function get_tax_class() on null” in WooCommerce?

I’ve an ecommerce app that’s was working fine some days ago. After I update the wordpress and woocommerce plugin, the app suddenly stopped working.

I think that’s something about the plugin that I use (mstore app-mobile), but unfortunately is not possible to update this plugin.

The error happen on this updateCartQty function. My guess is that something about the function parameter.

public function updateCartQty()
{        
   $cart_item_key = $_REQUEST['key'];
   $qty = (int)$_REQUEST['quantity'];

   global $woocommerce;
   $woocommerce->cart->set_quantity($cart_item_key, $qty);   
   $this->cart();    
}

This is the log file:

2021-02-22T23:43:48+00:00 CRITICAL Uncaught Error: Call to a member function get_tax_class() on null in /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php:225
Stack trace:
#0 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(651): WC_Cart_Totals->get_items_from_cart()
#1 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(143): WC_Cart_Totals->calculate_item_totals()
#2 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(134): WC_Cart_Totals->calculate()
#3 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart.php(1369): WC_Cart_Totals->__construct()
#4 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart.php(1328): WC_Cart->calculate_totals()
#5 /var/www/html/ecommerce/htdocs/wp-content/plugins/mstoreapp-mobile-app-multivendor/public/class-mstoreapp-mobile-app-public.php(1277): WC_Cart->set_quantity()
#6 , em /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php, na linha 225

System informations:

WC Version: 5.0.0
REST API Version: ✔ 5.0.0
WC Blocks Version: ✔ 4.0.0
Action Scheduler Version: ✔ 3.1.6
WC Admin Version: ✔ 1.9.0
Log Directory Writable: ✔
WP Version: 5.6.2
WP Multisite: –
WP Memory Limit: 256 MB
WP Debug Mode: –
WP Cron: ✔
Language: pt_BR
External object cache: –

### Server Environment ###

Server Info: Apache/2.4.18 (Ubuntu)
PHP Version: 7.4.15
PHP Post Max Size: 8 MB
PHP Time Limit: 30
PHP Max Input Vars: 1000
cURL Version: 7.47.0
OpenSSL/1.0.2g

SUHOSIN Installed: –
MySQL Version: 8.0.20
Max Upload Size: 2 MB
Default Timezone is UTC: ✔
fsockopen/cURL: ✔
SoapClient: ✔
DOMDocument: ✔
GZip: ✔
Multibyte String: ✔
Remote Post: ✔
Remote Get: ✔

### Database ###

WC Database Version: 5.0.0

### Active Plugins (13) ###

Redux: por Redux.io + Dovy Paukstys – 4.1.24
Akismet Anti-Spam: por Automattic – 4.1.8
All in One SEO: por Equipe All in One SEO – 4.0.15
Google Analytics for WordPress by MonsterInsights: por MonsterInsights – 7.16.2
Jetpack por WordPress.com: por Automattic – 9.4
Monetization Code plugin: por aerin Singh – 1.0
Mstoreapp Mobile Multivendor: por Mstoreapp – 9.0.1
Simple Tags: por WebFactory Ltd – 2.63
WCFM - WooCommerce Frontend Manager: por WC Lovers – 6.5.6
WCFM - WooCommerce Multivendor Marketplace: por WC Lovers – 3.4.6
WCFM - WooCommerce Multivendor Membership: por WC Lovers – 2.9.5
WooCommerce: por Automattic – 5.0.0
WP Mail SMTP: por WPForms – 2.6.0

### Inactive Plugins (0) ###

What can I do to solve this error? I appreciate any help. Thank you for your time.

EDIT 1: I’ve changed the $_REQUEST to $_POST and set the debug on $cart_item_key = $_REQUEST[‘key’];

**File:** wp-content/plugins/mstoreapp-mobile-app-multivendor/public/class-mstoreapp-mobile-app-public.php(1276): WC_Cart->set_quantity()

Function

public function updateCartQty() {

        $cart_item_key = $_POST['key'];
        $qty = (int)$_POST['quantity'];

        echo "<script>console.debug( "PHP DEBUG: $cart_item_key" );</script>";
    
        global $woocommerce;
        $woocommerce->cart->set_quantity($cart_item_key, $qty);
        $this->cart();    
    }

OUTPUT:

<script>console.debug( "PHP DEBUG: 120705de7e61c5b322ad798b8ef225a7" );</script>

Function changed

EDIT 2:

File: wp-content/plugins/mstoreapp-mobile-app-multivendor/public/class-mstoreapp-mobile-app-public.php(1276): WC_Cart->set_quantity()

I’ve set the array position but error still persists:

$cart_item_key = $_POST['key'][0];
$qty = (int)$_POST['quantity'][0];

Setting the array position

OUTPUT

Output

EDIT 3:

File: wp-content/plugins/mstoreapp-mobile-app-multivendor/public/class-mstoreapp-mobile-app-public.php(1276): WC_Cart->set_quantity()

Using var_dump($_POST):

public function updateCartQty() {
   
        $cart_item_key = $_POST['key'][0];
        $qty = (int)$_POST['quantity'][0];
          
        var_dump($_POST);

        global $woocommerce;
        $woocommerce->cart->set_quantity($cart_item_key, $qty);

        $this->cart();

    }

The output:

array(6) {
  ["key"]=>
  string(32) "431cfe4bd4a84b68398e14af4be0bdc3"
  ["quantity"]=>
  string(1) "3"
  ["update_cart"]=>
  string(11) "Update Cart"
  ["_wpnonce"]=>
  string(10) "52e75170fc"
  ["lang"]=>
  string(2) "en"
  ["mstoreapp"]=>
  string(1) "1"
}

EDIT 4:

**File:** wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php

protected function get_items_from_cart() {
                $this->items = array();

                foreach ( $this->cart->get_cart() as $cart_item_key => $cart_item ) {
                        $item                          = $this->get_default_item_props();
                        $item->key                     = $cart_item_key;
                        $item->object                  = $cart_item;
                        $item->tax_class               = $cart_item['data']->get_tax_class();
                        $item->taxable                 = 'taxable' === $cart_item['data']->get_tax_status(); // LINE 225
                        $item->price_includes_tax      = wc_prices_include_tax();
                        $item->quantity                = $cart_item['quantity'];
                        $item->price                   = wc_add_number_precision_deep( $cart_item['data']->get_price() * $cart_item['quantity'] );
                        $item->product                 = $cart_item['data'];
                        $item->tax_rates               = $this->get_item_tax_rates( $item );
                        $this->items[ $cart_item_key ] = $item;
                }
        }

EDIT 5: as suggested by @Vincenzo Di Gaetano, checking the cart_item_key before increment has solved the problem.

global $woocommerce; if ( $woocommerce->cart->get_cart_item( $cart_item_key ) ) { $woocommerce->cart->set_quantity( $cart_item_key, $qty ); }

Advertisement

Answer

From the var_dump of the $_POST variable (EDIT 3) it is clear that you will have to get the cart item key and the quantity in this way:

$cart_item_key = $_POST['key'];
$qty = (int)$_POST['quantity'];

The Uncaught Error: Call to a member function get_tax_class() on null error may be due to the fact that the cart item key does not exist.

So you can add a check like this:

global $woocommerce;
if ( $woocommerce->cart->get_cart_item( $cart_item_key ) ) {
    $woocommerce->cart->set_quantity( $cart_item_key, $qty );
}

This will definitely fix the error.

However, if your function should always get the correct cart item key you need to double check how you get (or how it is passed) the cart item key.

The answer to your comment.:

The cart item key is generated by the generate_cart_id() method of the WC_Cart class.

This method is called within the add_to_cart method of the same WC_Cart class.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement