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.

JavaScript

This is the log file:

JavaScript

System informations:

JavaScript

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’];

JavaScript

Function

JavaScript

OUTPUT:

JavaScript

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:

JavaScript

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):

JavaScript

The output:

JavaScript

EDIT 4:

JavaScript

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:

JavaScript

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:

JavaScript

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