WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Get product type (check is it "external") for further action (5 posts)

  1. dovich
    Member
    Posted 1 year ago #

    Hi!

    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.

    http://wordpress.org/extend/plugins/woocommerce/

  2. helgatheviking
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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
    Member
    Posted 6 months 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.

    so:

    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
    Member
    Posted 5 months ago #

    It helped me today, thanks @helgatheviking!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic