Wordpress Critical Error Due to Plugin Conflict with my Functions php

0 votes

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.

Feb 23, 2022 in Others by Kichu
• 19,050 points
965 views

1 answer to this question.

0 votes

modify your code like this and will work for sure 

// 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; 
} 
answered Feb 24, 2022 by narikkadan
• 63,680 points

Related Questions In Others

0 votes
1 answer

I am trying to run following command But I end up with an error :

Hii Nishant, You are running this command inside ...READ MORE

answered Apr 6, 2020 in Others by Niroj
• 82,880 points
1,833 views
0 votes
1 answer

The uploaded file exceeds the upload_max_filesize directive in php.ini error while uploading plugin

After looking at your ERROR, it seems ...READ MORE

answered Feb 8, 2022 in Others by Soham
• 9,710 points
671 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

How to Access Yoast SEO tags in Wordpress API Callback

use rest api to request for the ...READ MORE

answered Feb 12, 2022 in Others by narikkadan
• 63,680 points
1,810 views
0 votes
1 answer

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

// WooCommerce Stock message add_filter( 'woocommerce_get_availability', 'mw_get_availability', ...READ MORE

answered Feb 26, 2022 in Others by narikkadan
• 63,680 points
1,094 views
0 votes
1 answer

How to add meta keyword in wordpress using yoast seo plugin.?

if you dont want to use code ...READ MORE

answered Feb 12, 2022 in Others by narikkadan
• 63,680 points
983 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP