• Ever since changing to the widget location “other” and using the provided functions, we see this error below, about 250k times in the last 5 days:

    PHP message: PHP Fatal error: Uncaught Error: Call to a member function get_reviews_allowed() on null in /var/www/site.com/htdocs/wp-content/plugins/yotpo-social-reviews-for-woocommerce/wc_yotpo.php:110

    How can we fix this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I looked into this for one of our clients. The error stems from the following code:

    function wc_yotpo_show_reviews_widget() {		 
      global $product;
      if($product->get_reviews_allowed() == true) {
        echo generate_reviews_widget_code($product);
      }						
    }

    In some cases, when the function runs, the global $product variable is not a valid instance of a product, and that raises the fatal error.

    A quick way to prevent the error would be to modify the function as follows:

    function wc_yotpo_show_reviews_widget() {		 
      global $product;
      
      // Check that the $product variable points to a product instance, 
      // before trying to access the get_reviews_allowed() method
      if(isset($product) && ($product instanceof WC_Product) && $product->get_reviews_allowed() == true) {
        echo generate_reviews_widget_code($product);
      }						
    }

    A better solution would be to determine when the function wc_yotpo_show_reviews_widget() is supposed to be called (based on its logic, it seems that it should only be called on a product page) and ensure that it runs only when needed.

    Thread Starter Razorfrog Web Design

    (@razorfrog)

    Thanks @diego. I appreciate you keeping an eye on this even if Yotpo isn’t.

    I solved this by wrapping their provided functions in is_product().

    Our issue was that the code was also being called every time a product was accessed via the API, which was invalid for review display.

    Thanks for your feedback. Checking for is_product(), or using a check like the one I suggested, is a good safeguard. However, I still think that it would be best for YOTPO not to run the function at all, when the context is the wrong one. Running the code only when actually needed could improve the performance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Another Fatal Error (get_reviews_allowed() on null)’ is closed to new replies.