php - 由于插件与我的 Functions.php 冲突导致的 Wordpress 严重错误
问题描述
我刚刚安装了一个新插件(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;
}
推荐阅读
- html - 为什么按钮打开视频文件但不下载?
- sikuli - 如何获取图像的一部分并检查它是否存在?
- python - 为什么即使代码已经退出函数运行,子进程也无法完成?
- javascript - 如何在 Node.js 中强制重置套接字?
- java - “全球”假装拦截器并未被所有假装客户拾取
- visual-studio-code - 如何使用 vs 代码扩展 API 向用户询问用户名或其他数据
- python - django 模型权限与组权限
- ms-access - 从组合框数据访问查询表达式返回整数而不是文本数据(设计视图)
- ios - 如何在swift 4中比较两个日期并在它们之间进行计时器倒计时
- qt - osgEarth 和 Qt 中未显示的行