如何修复 WooCommerce 中的“未捕获错误:在 null 上调用成员函数 get_tax_class()”?

Die*_*dor 6 php wordpress woocommerce ionic-framework

我有一个电子商务应用程序,几天前运行良好。在我更新 wordpress 和 woocommerce 插件后,该应用程序突然停止工作。

\n

我认为这与我使用的插件(mstore app-mobile)有关,但不幸的是无法更新此插件。

\n

错误发生在此 updateCartQty 函数上。我的猜测是关于函数参数的一些事情。

\n
public function updateCartQty()\n{        \n   $cart_item_key = $_REQUEST[\'key\'];\n   $qty = (int)$_REQUEST[\'quantity\'];\n\n   global $woocommerce;\n   $woocommerce->cart->set_quantity($cart_item_key, $qty);   \n   $this->cart();    \n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是日志文件:

\n
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\nStack trace:\n#0 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(651): WC_Cart_Totals->get_items_from_cart()\n#1 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(143): WC_Cart_Totals->calculate_item_totals()\n#2 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(134): WC_Cart_Totals->calculate()\n#3 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart.php(1369): WC_Cart_Totals->__construct()\n#4 /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart.php(1328): WC_Cart->calculate_totals()\n#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()\n#6 , em /var/www/html/ecommerce/htdocs/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php, na linha 225\n
Run Code Online (Sandbox Code Playgroud)\n

系统信息:

\n
WC Version: 5.0.0\nREST API Version: \xe2\x9c\x94 5.0.0\nWC Blocks Version: \xe2\x9c\x94 4.0.0\nAction Scheduler Version: \xe2\x9c\x94 3.1.6\nWC Admin Version: \xe2\x9c\x94 1.9.0\nLog Directory Writable: \xe2\x9c\x94\nWP Version: 5.6.2\nWP Multisite: \xe2\x80\x93\nWP Memory Limit: 256 MB\nWP Debug Mode: \xe2\x80\x93\nWP Cron: \xe2\x9c\x94\nLanguage: pt_BR\nExternal object cache: \xe2\x80\x93\n\n### Server Environment ###\n\nServer Info: Apache/2.4.18 (Ubuntu)\nPHP Version: 7.4.15\nPHP Post Max Size: 8 MB\nPHP Time Limit: 30\nPHP Max Input Vars: 1000\ncURL Version: 7.47.0\nOpenSSL/1.0.2g\n\nSUHOSIN Installed: \xe2\x80\x93\nMySQL Version: 8.0.20\nMax Upload Size: 2 MB\nDefault Timezone is UTC: \xe2\x9c\x94\nfsockopen/cURL: \xe2\x9c\x94\nSoapClient: \xe2\x9c\x94\nDOMDocument: \xe2\x9c\x94\nGZip: \xe2\x9c\x94\nMultibyte String: \xe2\x9c\x94\nRemote Post: \xe2\x9c\x94\nRemote Get: \xe2\x9c\x94\n\n### Database ###\n\nWC Database Version: 5.0.0\n\n### Active Plugins (13) ###\n\nRedux: por Redux.io + Dovy Paukstys \xe2\x80\x93 4.1.24\nAkismet Anti-Spam: por Automattic \xe2\x80\x93 4.1.8\nAll in One SEO: por Equipe All in One SEO \xe2\x80\x93 4.0.15\nGoogle Analytics for WordPress by MonsterInsights: por MonsterInsights \xe2\x80\x93 7.16.2\nJetpack por WordPress.com: por Automattic \xe2\x80\x93 9.4\nMonetization Code plugin: por aerin Singh \xe2\x80\x93 1.0\nMstoreapp Mobile Multivendor: por Mstoreapp \xe2\x80\x93 9.0.1\nSimple Tags: por WebFactory Ltd \xe2\x80\x93 2.63\nWCFM - WooCommerce Frontend Manager: por WC Lovers \xe2\x80\x93 6.5.6\nWCFM - WooCommerce Multivendor Marketplace: por WC Lovers \xe2\x80\x93 3.4.6\nWCFM - WooCommerce Multivendor Membership: por WC Lovers \xe2\x80\x93 2.9.5\nWooCommerce: por Automattic \xe2\x80\x93 5.0.0\nWP Mail SMTP: por WPForms \xe2\x80\x93 2.6.0\n\n### Inactive Plugins (0) ###\n
Run Code Online (Sandbox Code Playgroud)\n

我该怎么做才能解决这个错误?我很感激任何帮助。感谢您的时间。

\n

编辑 1:我已将$_REQUEST更改为$_POST并将调试设置为 $cart_item_key = $_REQUEST[\'key\'​​];

\n
**File:** wp-content/plugins/mstoreapp-mobile-app-multivendor/public/class-mstoreapp-mobile-app-public.php(1276): WC_Cart->set_quantity()\n
Run Code Online (Sandbox Code Playgroud)\n

功能

\n
public function updateCartQty() {\n\n        $cart_item_key = $_POST[\'key\'];\n        $qty = (int)$_POST[\'quantity\'];\n\n        echo "<script>console.debug( \\"PHP DEBUG: $cart_item_key\\" );</script>";\n    \n        global $woocommerce;\n        $woocommerce->cart->set_quantity($cart_item_key, $qty);\n        $this->cart();    \n    }\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
<script>console.debug( "PHP DEBUG: 120705de7e61c5b322ad798b8ef225a7" );</script>\n
Run Code Online (Sandbox Code Playgroud)\n

功能改变

\n

编辑2:

\n

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

\n

我已经设置了数组位置,但错误仍然存​​在:

\n
$cart_item_key = $_POST[\'key\'][0];\n$qty = (int)$_POST[\'quantity\'][0];\n
Run Code Online (Sandbox Code Playgroud)\n

设置阵列位置

\n

输出

\n

输出

\n

编辑3:

\n

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

\n

使用 var_dump($_POST):

\n
public function updateCartQty() {\n   \n        $cart_item_key = $_POST[\'key\'][0];\n        $qty = (int)$_POST[\'quantity\'][0];\n          \n        var_dump($_POST);\n\n        global $woocommerce;\n        $woocommerce->cart->set_quantity($cart_item_key, $qty);\n\n        $this->cart();\n\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

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

编辑4:

\n
**File:** wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php\n\nprotected function get_items_from_cart() {\n                $this->items = array();\n\n                foreach ( $this->cart->get_cart() as $cart_item_key => $cart_item ) {\n                        $item                          = $this->get_default_item_props();\n                        $item->key                     = $cart_item_key;\n                        $item->object                  = $cart_item;\n                        $item->tax_class               = $cart_item[\'data\']->get_tax_class();\n                        $item->taxable                 = \'taxable\' === $cart_item[\'data\']->get_tax_status(); // LINE 225\n                        $item->price_includes_tax      = wc_prices_include_tax();\n                        $item->quantity                = $cart_item[\'quantity\'];\n                        $item->price                   = wc_add_number_precision_deep( $cart_item[\'data\']->get_price() * $cart_item[\'quantity\'] );\n                        $item->product                 = $cart_item[\'data\'];\n                        $item->tax_rates               = $this->get_item_tax_rates( $item );\n                        $this->items[ $cart_item_key ] = $item;\n                }\n        }\n
Run Code Online (Sandbox Code Playgroud)\n

编辑5: @Vincenzo Di Gaetano 建议的 \nas,在增量之前检查 cart_item_key 已经解决了问题。

\n
\n

全球$woocommerce;if ( $woocommerce->cart->get_cart_item(\n$cart_item_key ) ) {\n$woocommerce->cart->set_quantity( $cart_item_key, $qty ); }

\n
\n

Vin*_*ano 3

var_dump变量(编辑 3)$_POST中可以清楚地看出,您必须以这种方式获取购物车商品密钥和数量:

$cart_item_key = $_POST['key'];
$qty = (int)$_POST['quantity'];
Run Code Online (Sandbox Code Playgroud)

Uncaught Error: Call to a member function get_tax_class() on null错误可能是由于购物车商品密钥不存在所致。

所以你可以添加这样的检查:

global $woocommerce;
if ( $woocommerce->cart->get_cart_item( $cart_item_key ) ) {
    $woocommerce->cart->set_quantity( $cart_item_key, $qty );
}
Run Code Online (Sandbox Code Playgroud)

这肯定会修复错误。

但是,如果您的函数应该始终获取正确的购物车项目密钥,则您需要仔细检查如何获取(或如何传递)购物车项目密钥。

对您的评论的答复:

购物车商品密钥由generate_cart_id()该类的方法生成WC_Cart

add_to_cart该方法在同一个类的方法内调用WC_Cart