由于插件与我的 Functions.php 冲突导致 Wordpress 严重错误

Wordpress Critical Error Due to Plugin Conflict with my Functions.php

我刚刚安装了一个新插件(WooCommerce SEO 插件),这导致我的网站出现严重错误,导致我无法进入后端 Wordpress/WooCommerce 仪表板的产品区域。 (显示发生严重错误并且页面为白色并带有该警告文本)

该网站实际上运行良好,但当此 WooCommerce SEO 插件激活时,我无法编辑或查看产品仪表板中的任何产品。

我已经缩小了冲突范围,但不知道如何解决。是的,我可以禁用插件,但我需要这个,冲突是基于我添加到我的子主题的 Functions.php 文件中的自定义代码。这会在库存低(只剩一个)时显示警报:


// WooCommerce Stock message 
add_filter( 'woocommerce_get_availability', 'mw_get_availability', 1, 2 ); 

function mw_get_availability( $availability, $_product ) { 

//change text "In Stock' to 'SPECIAL ORDER' 
global $product; 
if ( $_product->is_in_stock() && $product->get_stock_quantity() < 2 ) $availability['availability'] = $product->get_stock_quantity().' '.__('IN STOCK <p style=\"border:3px; border-style:solid; font-weight: bold; border-color:#FF0000; padding: 15px;\">HURRY! LAST ONE AVAILABLE!</p>', 'woocommerce'); 

//change text "Out of Stock' to 'SOLD OUT' 
if ( !$_product->is_in_stock() ) $availability['availability'] = __('SOLD OUT', 'woocommerce'); 

return $availability; 

} 

此代码非常适合我的网站。但是,当与 WooCommerce SEO 插件一起使用时,出现以下错误:

Error Details
=============
An error of type E_ERROR was caused in line 88 of the file /home/site/public_html/website/wp-content/themes/photome-child/functions.php. Error message: Uncaught Error: Call to a member function get_stock_quantity() on null in /home/site/public_html/website/wp-content/themes/photome-child/functions.php:88
Stack trace:
#0 /home/site/public_html/website/wp-includes/class-wp-hook.php(303): mw_get_availability(Array, Object(WC_Product_Simple))
#1 /home/site/public_html/website/wp-includes/plugin.php(189): WP_Hook->apply_filters(Array, Array)
#2 /home/site/public_html/website/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(2061): apply_filters('woocommerce_get...', Array, Object(WC_Product_Simple))
#3 /home/site/public_html/website/wp-content/plugins/wpseo-woocommerce/classes/woocommerce-seo.php(1280): WC_Product->get_availability()
#4 /home/site/public_html/website/wp-content/plugins/wpseo-woocommerce/classes/woocommerce-seo.php(916): Yoast_WooCommerce_SEO->localize_woo_script()
#5 /home/site/public_html/website/wp-includes/class-wp-hook.php(303): Yoast_WooCommerce_SEO->enqueue_scripts('edit.php')
#6 /home/the

错误开始行:

if ( $_product->is_in_stock() && $product->get_stock_quantity() < 2 ) $availability['availability'] = $product->get_stock_quantity().' '.__('IN STOCK <p style=\"border:3px; border-style:solid; font-weight: bold; border-color:#FF0000; padding: 15px;\">HURRY! LAST ONE AVAILABLE!</p>', 'woocommerce');  

我用谷歌搜索了一下,发现可能是因为 WooCommerce SEO 使用了一个数组,这会导致错误?

我真的很想使用 WooCommerce SEO 并且能够使用我的简单自定义代码,效果很好。

希望有人可以看到我的自定义代码的简单修复,这样我就可以更新它,这样它就不会导致此错误。

我被难住了。

非常感谢任何帮助,谢谢。

我稍微修改了你的代码,试试这个。

// WooCommerce Stock message 
add_filter( 'woocommerce_get_availability', 'mw_get_availability', 1, 2 ); 

function mw_get_availability( $availability, $_product ) { 
    //change text "In Stock' to 'SPECIAL ORDER' 
    global $product; 
    if ( $_product->is_in_stock() && $_product ->get_stock_quantity() < 2 ) $availability['availability'] = $_product->get_stock_quantity().' '.__('IN STOCK <p style=\"border:3px; border-style:solid; font-weight: bold; border-color:#FF0000; padding: 15px;\">HURRY! LAST ONE AVAILABLE!</p>', 'woocommerce'); 
    
    //change text "Out of Stock' to 'SOLD OUT' 
    if ( !$_product->is_in_stock() ) $availability['availability'] = __('SOLD OUT', 'woocommerce'); 
    
    return $availability; 
}