DEV Community

Zaw Htut Win
Zaw Htut Win

Posted on

Uncaught Error: Call to a member function get_type() on bool - BackinStock Notifier Wordpress Plugin

It's because of the following code, the error was being thrown.

/plugins/backinstocknotifier/backinstocknotifier.php

            $product = wc_get_product($post_id);  
            if($product->get_type() == 'variable') {
                // the rest of the code
            }
Enter fullscreen mode Exit fullscreen mode

It doesn't check the post is the product or not, so when the post is not the product, the plugin will throw error.
So we can correct the code such as,

            $product = wc_get_product($post_id);  
            if(isset($product) && $product === false){return;} // exit the function if the post_id not corresponding to product
            if($product->get_type() == 'variable') {
                // the rest of the code
            }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)