Get product type (check is it "external") for further action (5 posts)

  1. dovich
    Posted 3 years ago #


    I'm trying set the condition in the header in my template. When customer enter the product page and the product type is "external", then some block appears in header.

    Here's how I tried to implement it (the code goes right after <body> tag in header.php):

    <?php global $product;
          if (is_product() and $product->product_type == 'external') : ?>
          <div>Some info for external products</div>
    <?php endif; ?>

    But it doesn't work. Can somebody help me whith this issue? I'm not familiar with wordpress so good and can be some stupid mistake in my code.

    Thanks in advance.


  2. helgatheviking
    Posted 3 years ago #

    is the $product set up yet? if you var_dump($product) do you get anything in the array? if not, then i suspect that you are calling this before setup_post which if i remember is where the product data is defined.

  3. dovich
    Posted 3 years ago #

    How can I set up it in header? var_dump($product) shows me only product title, meanwhile if i call var_dump directly on product page, it shows me a huge array.

  4. helgatheviking
    Posted 2 years ago #

    Funny, I found myself back on this thread thanks to google. Probably too late to help the OP, but for future googlers:

    To get the $product object at any time you can use the WooCommerce function get_product() and pass it the post ID.... which is the same as the product ID.


    global $post;
    if( function_exists('get_product') ){
    	$product = get_product( $post->ID );
    	if( $product->is_type( 'external' ) ){
    		// do something with external products

    If the $post object isn't even set up you're way too early in the WP action sequence. It should be set up by the wp action.

  5. blogjunkie
    Posted 2 years ago #

    It helped me today, thanks @helgatheviking!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic