GuiDrn
GuiDrn

Reputation: 333

Error using WooCommerce - using is_type function on $product

I encounter som problem using WordPress, WooCommerce, Divi and particulary a plugin named Divi Body Commerce

When I create a variable product, and go to my cart, I have a php error. It is not my code, but I need to fix it.

Here is the beginning of the code :

<?php
if( ! defined( 'ABSPATH' ) ) exit;

$mydata = get_option( 'divi-bodyshop-woo_options' );

$mydata = unserialize($mydata);

if(isset($mydata['variation_striketrhough'][0])) {

     $variation_striketrhough = $mydata['variation_striketrhough'][0];
}
else{
    $variation_striketrhough = "0";
}
if ($variation_striketrhough == 1) {
    if ( ! is_admin() ) {

        function wcbv_variation_is_active( $active, $variation ) {
            if( ! $variation->is_in_stock() ) {
                return false;
            }
            return $active;
        }
        add_filter( 'woocommerce_variation_is_active', 'wcbv_variation_is_active', 10, 2 );
        add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name', 10, 1 );
        function customizing_variations_terms_name( $term_name ){

            global $product; 
            if( $product->is_type( 'variable' ) || $product->is_type( 'subscription-variation' ) ) {
                 /*...........*/
            }
        }
    }
}

?>

The error occur when I call the is_type function on the variable product.

PHP Fatal error:  Uncaught Error: Call to a member function is_type() on null 

I am not used to WordPress and WooCommerce so if somebody have an answer for me it will be great.

Upvotes: 1

Views: 1051

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29660

You could add an extra check, normally this will prevent the error message

global $product; 

if ( is_a( $product, 'WC_Product' ) ) {
    if ( $product->is_type( 'variable' ) ) {
        // Continue..
    }
}

Upvotes: 4

Related Questions