[英]Woocommerce Uncaught Error: Call to a member function get_stock_quantity() on null
I have received a message from WordPress about a fatal error.我收到了来自 WordPress 的关于致命错误的消息。 It relates to a particular page in the backend in the WPML string translation plugin.它与 WPML 字符串翻译插件中后端的特定页面有关。 I think a particular translation:我认为一个特定的翻译:
visit the page where the error was caught (xxxxxxxx/wp-admin/admin.php?page=wpml-translation-management%2Fmenu%2Ftranslations-queue.php&return_url=%2Fwp-admin%2Fpost.php%3Fpost%3D117195%26action%3Dedit%26lang%3Den%26message%3D6&job_id=3741&update_needed=1&trid=129987&language_code=fr)访问发现错误的页面 (xxxxxxxx/wp-admin/admin.php?page=wpml-translation-management%2Fmenu%2Ftranslations-queue.php&return_url=%2Fwp-admin%2Fpost.php%3Fpost%3D117195%26action% 3Dedit%26lang%3Den%26message%3D6&job_id=3741&update_needed=1&trid=129987&language_code=fr)
It also says它还说
Error Details ============= An error of type E_ERROR was caused in line 259 of the file /nas/content/live/mywebsite/wp-content/themes/babasouk/functions.php.错误详细信息 ============== 文件 /nas/content/live/mywebsite/wp-content/themes/babasouk/functions.php 的第 259 行导致了类型为 E_ERROR 的错误。 Error message: Uncaught Error: Call to a member function get_stock_quantity() on null in /nas/content/live/mywebsite/wp-content/themes/mytheme/functions.php:259 Stack trace: #0 /nas/content/live/mywebsite/wp-includes/class-wp-hook.php(303): bbloomer_custom_get_availability_text('100 in stock', NULL)错误消息:未捕获的错误:在 /nas/content/live/mywebsite/wp-content/themes/mytheme/functions.php:259 中的 null 上调用成员函数 get_stock_quantity() 堆栈跟踪:#0 /nas/content/live /mywebsite/wp-includes/class-wp-hook.php(303): bbloomer_custom_get_availability_text('100 in stock', NULL)
#1 /nas/content/live/mywebsite/wp-includes/plugin.php(189): WP_Hook->apply_filters('100 in stock', Array) #1 /nas/content/live/mywebsite/wp-includes/plugin.php(189): WP_Hook->apply_filters('100 in stock', Array)
#2 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(2082): apply_filters('woocommerce_get...', '100 in stock', Object(WC_Product_Variation)) #2 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(2082): apply_filters('woocommerce_get...', '100 in stock', Object (WC_Product_Variation))
#3 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(2058): WC_Product->get_availability_text() #3 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(2058): WC_Product->get_availability_text()
#4 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/wc-template-functions.php(3512): WC_Product->get_availability() #4 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/wc-template-functions.php(3512): WC_Product->get_availability()
#5 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/class-wc-product-variable.php(367): wc_get_stock_html(Object(W #5 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/class-wc-product-variable.php(367): wc_get_stock_html(Object(W)
In my PHP I echo get_stock_quantity()and it returns a value.在我的 PHP 中,我 echo get_stock_quantity() 并返回一个值。 I also tried echo of $product and returned everything.我还尝试了 $product 的 echo 并返回了所有内容。 I disabled it because it is too much information.我禁用了它,因为它的信息太多。 Website is here.网站在这里。 https://babasoukstage.wpengine.com/shop/product-category/decor/moroccan-kilim-pillows/ https://babasoukstage.wpengine.com/shop/product-category/decor/moroccan-kilim-pillows/
Could the problem be the '' empty value I am declaring for translation?问题可能是我声明要翻译的 '' 空值吗? __( '', 'babasouk' ); __( '', 'babasouk');
My PHP is我的 PHP 是
function bbloomer_show_stock_shop() {
global $product;
echo wc_get_stock_html( $product );
}
// CHANGE STOCK MESSAGES
add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
function bbloomer_custom_get_availability_text( $availability, $product ) {
global $product; // TO MAKE CATEGORY AVAILABLE
$stock = $product->get_stock_quantity(); //THIS IS LINE 259
//echo $product; does echo all product details when active
echo $stock;//test to see if stock quantity returned. Does display on category and product pages.
// OUTSIDE THE CATEGORY EXCLUSION SO THEY APPLY TO ALL
if ( !$product->is_in_stock() )$availability = __( 'Sorry, I am sold out!', 'babasouk' ); //OUT OF STOCK MESSAGE
if ( $product->is_in_stock() && ( $stock >= 2 ) )$availability = __( '', 'babasouk' ); //NO MESSAGE IF STOCK OVER 1
//EXCLUDE THESE CATEGORIES FOR 1 ITEM LEFT MESSAGE. IF THESE CATEGORIES MESSAGE IS BLANK.
if ( has_term( array( 'ORIGINAL ARTWORKS', 'OEUVRES ORIGINALES' ), 'product_cat' ) ) {
if ( $product->is_in_stock() && ( $stock == 1 ) )$availability = __( '', 'babasouk' ); //
} //IF NOT EXCLUDED CATEGORIES MESSAGE IS 'Only 1 Available!'
else if ( $product->is_in_stock() && ( $stock == 1 ) )$availability = __( 'Only 1 Available!', 'babasouk' );
return $availability;
}
You have an error in your code and the logs are telling you the issue as well.您的代码中有错误,日志也告诉您这个问题。 Since you are in the functions.php
file, there is no global $product
available.由于您在functions.php
文件中,因此没有可用的global $product
。 Because of this you get a null pointer since you overwrite the available product variable with nothing.因此,您会得到一个空指针,因为您没有覆盖可用的产品变量。 When I check the snippet on the page you got it from, there is also no global $product
used - just wondering...当我检查您从中获得的页面上的代码段时,也没有使用global $product
- 只是想知道......
When you check the hook, you can see that there is the product already passed to the function.当您检查钩子时,您可以看到已经传递给函数的产品。 Just use it:只需使用它:
add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
function bbloomer_custom_get_availability_text( $availability, WC_Product $product ) {
$stock = $product->get_stock_quantity(); //THIS IS LINE 259
//echo $product; does echo all product details when active
error_log( $stock );//test to see if stock quantity returned. Does display on category and product pages.
// OUTSIDE THE CATEGORY EXCLUSION SO THEY APPLY TO ALL
if ( $product->is_in_stock() ) {
if ( $stock > 1 ) {
$availability = __( '', 'babasouk' );
}
if ( $stock === 1 ) {
if ( has_term( [ 'ORIGINAL ARTWORKS', 'OEUVRES ORIGINALES' ], 'product_cat', $product ) ) {
$availability = __( '', 'babasouk' );
} else {
$availability = __( 'Only 1 Available!', 'babasouk' );
}
}
} else {
$availability = __( 'Sorry, I am sold out!', 'babasouk' );
}
return $availability;
}
I have also improved your code a bit.我也稍微改进了你的代码。 At the end your function makes not very much sense to me but never mind - just to show you a better way of nesting and speed improvements.最后,您的功能对我来说没有多大意义,但没关系-只是为了向您展示更好的嵌套和速度改进方法。 If you call a function only once instead multiple times you can improve your page speed.如果您只调用一次函数而不是多次调用,则可以提高页面速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.