提问人:user9557 提问时间:10/8/2021 最后编辑:Mr. Jouser9557 更新时间:10/17/2021 访问量:4940
Woocommerce Uncaught 错误:在 null 上调用成员函数 get_stock_quantity()
Woocommerce Uncaught Error: Call to a member function get_stock_quantity() on null
问:
我收到了来自 WordPress 的一条关于致命错误的消息。它与WPML字符串翻译插件后端的特定页面有关。我认为一个特定的翻译:
访问捕获错误的页面 (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)
它还说
错误详细信息 ============= 在文件的第 259 行中导致 E_ERROR 类型的错误 /nas/content/live/mywebsite/wp-content/themes/babasouk/functions.php。 错误消息:未捕获错误:调用成员函数 get_stock_quantity() 在 null 中 /nas/content/live/mywebsite/wp-content/themes/mytheme/functions.php:259 堆栈跟踪: #0 /nas/content/live/mywebsite/wp-includes/class-wp-hook.php(303):bbloomer_custom_get_availability_text('库存 100 个',空)
#1 /nas/content/live/mywebsite/wp-includes/plugin.php(189):WP_Hook->apply_filters('100 库存',数组)
#2 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(2082): apply_filters('woocommerce_get...', '有货100件', 对象(WC_Product_Variation))
#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()
#5 /nas/content/live/mywebsite/wp-content/plugins/woocommerce/includes/class-wc-product-variable.php(367): wc_get_stock_html(对象(W
在我的PHP中,我回显get_stock_quantity()并返回一个值。我还尝试了回声$product并归还了所有东西。我禁用了它,因为它的信息太多了。 网站在这里。https://babasoukstage.wpengine.com/shop/product-category/decor/moroccan-kilim-pillows/
问题可能出在我声明用于翻译的“”空值上吗? __( '', '巴巴苏克' );
我的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;
}
答:
您的代码中存在错误,日志也会告诉您问题所在。由于您在文件中,因此没有可用的文件。因此,您会得到一个空指针,因为您不带任何内容覆盖可用的 product 变量。当我检查您从中获取的页面上的片段时,也没有使用过 - 只是想知道......functions.php
global $product
global $product
当你检查钩子时,你可以看到已经有产品传递给函数。只需使用它:
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;
}
我还改进了您的代码。最后,您的函数对我来说没有多大意义,但没关系 - 只是为了向您展示一种更好的嵌套和速度改进方法。如果只调用一次函数,而不是多次调用,则可以提高页面速度。
评论
if( has_term( 4, 'product_cat' ) ) { // do something if current product in the loop is in product category with ID 4 }
评论