Wordpress Critical Error Due to Plugin Conflict with my Functions php

0 votes

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 25 in Others by Kichu
• 19,040 points
180 views

1 answer to this question.

0 votes
// 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; 
} 


modify your code like this it will work 
answered Feb 26 by narikkadan
• 10,840 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,720 points
761 views
0 votes
1 answer

Simple question regarding PHP, Wordpress & Yoast SEO

the way the search engine displays your ...READ MORE

answered Feb 14 in Others by narikkadan
• 10,840 points
33 views
0 votes
0 answers

SEOPress plugin not working with Wordpress 5.7

An error of type E_ERROR was caused ...READ MORE

Feb 14 in Others by Kichu
• 19,040 points
84 views
0 votes
1 answer

Limit the size of the existing and new permalink slugs in Wordpress for SEO

?php   /**  * Trim native slugs  */ function pm_trim_native_slug($slug, $post_ID, ...READ MORE

answered Feb 25 in Others by narikkadan
• 10,840 points
171 views
0 votes
1 answer

Wordpress seo silo category/term-tree for sidebar

function children_sidebar_shortcode( $atts ) { ...READ MORE

answered Feb 26 in Others by narikkadan
• 10,840 points
109 views
0 votes
1 answer
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 in Others by narikkadan
• 10,840 points
87 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP