Roots Discourse

$product -> get_type() returns 'simple' for variable products

Dear all,

We are building a Woocommerce website with Sage and have run into an issue.

We have a custom post type called “internship” and each internship is linked to a Woo product. In the internship page, when we want to check whether the product is a simple or variable product, we are getting incorrect return value in get_type method for $product.

See code snippet snapshot - http://prntscr.com/nisk0k

(Woo,WP,Sage all latest versions as of today)

We have declare woocommerce support and wc-status shows Theme support is enabled.

Any help / tips on what we might be doing wrong?

You should try using @if in Blade instead of @php

Also… please share code as actual text and not a screenshot in the future

Sure, so here is where I am setting up a WC_Product instance

  $in_product_id = get_field('in_product');
  if(get_post_type($in_product_id) == 'product'):
    $product = new WC_Product($in_product_id);
  endif;

And later on I am trying to fetch the product type here:

@php if( $product->is_type( 'simple' ) ) { @endphp

// do something if simple              

@php }  else { @endphp

// do something if not simple

@php } @endphp

But even a variable products returns ‘simple’ for me.

Do not do this:

@php if { } @endphp
@php } else { @endphp
@php } @endphp

You need to actually use Blade, or use straight <?php

Hi @benword yes I appreciate your suggestion and noted with thanks! :slight_smile:

However, that does not resolve my problem of $product->get_type or $product->is_type returning wrongly.

Any idea around that?

The behavior you’re describing seems like it’s WooCommerce-related, not Sage-related.

Have you examined $product directly to verify that the correct products are being retrieved/created, and it’s only the type test that’s not working correctly?

Yes I’ve done a print_r of the $product and checked.

Other functionality like get_price etc are working.

It could be Woocommerce related I agree. Just asking here in case anyone knows a specific Sage issue related to this.

When I activate Twenty Sixteen, the issue no longer persists.

Hence, thought of using this forum.

This topic was automatically closed after 42 days. New replies are no longer allowed.