Home > other >  Woocommerce fatal error: Uncaught Error: Call to a member function get_type() on string
Woocommerce fatal error: Uncaught Error: Call to a member function get_type() on string

Time:10-30

I am trying to build a plugin and i am stack on this weird fatal error when i think it shouldn't be there.

add_action( 'wp_head', 'my_custom_plugin' );
function my_custom_plugin() {   
  global $product;
  if (is_product()) {
    $product_type = $product->get_type();
    if ($product_type =='variable') { 
        ...things happening here....
    }
  }
}

I have also this code included in my file require_once ($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

thank you :)

CodePudding user response:

Not sure exactly what's happening, but you could try this code to see if it works for you or not. It works on my end:

add_action( 'wp_head', 'my_custom_plugin' );

function my_custom_plugin() 
{   
  global $post;

  if (is_product()) {
    $product = wc_get_product($post->ID);
    $product_type = $product->get_type();
    if($product_type == 'variable'){ 
    // ...things happening here....
    }
  }
}

Let me know if it works!

  • Related