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

Time:09-16

I just installed a new Plugin (WooCommerce SEO plugin) which causes a critical error on my web site, causing me to not be able to enter the Products area of my Wordpress/WooCommerce Dashboard on the backend. (Shows a Critical Error has occured and the page is white with that warning text)

The website actually runs fine, but I cannot edit or view any Products in the Products Dashboard when this WooCommerce SEO Plugin activated.

I have narrowed down the conflict, but don't know how to fix it. Yes, I could disable the plugin, but I need this, and the conflict is based on a custom code I added to my Child theme's Functions.php file. This shows an alert when the stock is low (with only one left):


// 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; 

} 

This code works beautifully for my site. However, when used with the WooCommerce SEO plugin, I get the following error:

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

The error starts on the line:

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');  

I have googled this and found that it is because possibly WooCommerce SEO uses an array and this causes an error?

I would really like to use WooCommerce SEO and be able to use my simple custom code that works great.

Hopefully, there's a simple fix to my custom code that someone can see so I can update it so it will not cause this error.

I'm stumped.

Any help is greatly appreciated, thanks.

CodePudding user response:

I have modified your code a bit, Try this.

// 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; 
} 
  • Related