Fatal PHP error when not checking for “cart” method on WC()
-
Under certain circumstances when your plugin has the “Replace cart url” setting enabled and is being used with other select themes and plugins, the following fatal error is generated when a page is being saved:
PHP Fatal error: Uncaught TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given in /public_html/wp-content/plugins/woocommerce-direct-checkout/lib/view/frontend/class-general.php:115
This problem is easily solved by simply checking if the ‘cart’ method exists on WC() before checking if the ‘is_empty’ method exists on WC()->cart:
if ( method_exists( WC(), 'cart' ) && method_exists( WC()->cart, 'is_empty' ) && WC()->cart->is_empty() ) {
Would you mind updating line 115 of /woocommerce-direct-checkout/lib/view/frontend/class-general.php with this extra check? It would make your plugin more robust and work better with specialized combinations of plugins and themes. Thank you!
- The topic ‘Fatal PHP error when not checking for “cart” method on WC()’ is closed to new replies.