I'm building a WooCommerce site whilst utilising the Twig templating system along with the Timber WordPress plugin and following along with this guide for the tease template.
However, looking at WooCommerce's own default templates/content-product.php they have this at the very top:
// Ensure visibility.
if ( empty( $product ) || ! $product->is_visible() ) {
return;
}
So I'm thinking that we should be doing the same check in the template file? However I am unsure how to check this value in Twig.
I have tried dump(post.is_visible()) but every one returns (bool) false.
I additionally have no idea where showthumb is coming from and couldn't find much information or docs about this particular setting/attribute.
Edit: Here is my controller: (my-theme/woocommerce/archive-product.php)
$context = Timber::get_context();
$context['shop-sidebar'] = Timber::get_widgets( 'shop-sidebar' );
$posts = Timber::get_posts();
$context['products'] = $posts;
if ( is_product_category() ) {
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$context['category'] = get_term( $term_id, 'product_cat' );
$context['title'] = single_term_title( '', false );
}
Timber::render( 'views/woo/archive.twig', $context );
Here is my archive.twig file:
{% extends 'archive.twig' %} {# Base Archive File #}
{% block before_article %}
{% do action('woocommerce_before_main_content') %}
{% endblock %}
{% block below_h1 %}
{% do action('woocommerce_archive_description') %}
{% endblock %}
{% block primary_block %}
{% if products|length > 0 %}
<div class="before-products">
{% do action('woocommerce_before_shop_loop') %}
</div>
<div class="products-wrap">
<div class="products row flex">
{% for post in products %}
<div class="product col-sm-6 col-md-4">
{% do action('woocommerce_shop_loop') %}
{% include ["woo/partials/tease-product.twig"] %}
</div>
{% endfor %}
</div>
</div>
<div class="after-products">
{% do action('woocommerce_after_shop_loop') %}
</div>
{% else %}
<div class="no-products">
{% do action('woocommerce_no_products_found') %}
</div>
{% endif %}
{% endblock %}
{% block after_article %}
{% do action('woocommerce_after_main_content') %}
{{ parent() }}
{% endblock %}
Here is my tease-product.twig file:
<article {{ fn('post_class', ['entry'] ) }}>
{{ fn('timber_set_product', post) }}
<div class="media">
<div class="media-figure {% if not post.thumbnail %}placeholder{% endif %}">
<a href="{{ post.link }}">
{% if post.thumbnail %}
<img src="{{ post.thumbnail.src|resize(600)|e('esc_url') }}" class="img-responsive" />
{% else %}
<span class="thumb-placeholder"><i class="icon-camera"></i></span>
{% endif %}
</a>
</div>
<div class="media-content">
{% do action('woocommerce_before_shop_loop_item') %}
{% do action('woocommerce_before_shop_loop_item_title') %}
{% if post.title %}
<h3 class="entry-title"><a href="{{ post.link }}">{{ post.title }}</a></h3>
{% else %}
<h3 class="entry-title"><a href="{{ post.link }}">{{ fn('the_title') }}</a></h3>
{% endif %}
{% do action( 'woocommerce_after_shop_loop_item_title' ) %}
{% do action( 'woocommerce_after_shop_loop_item' ) %}
</div>
</div>
</article>
How can I retrieve the correct value of the is_visible method?
showThumbis a variable passed from the controller to the view. You are callingproduct.is_visible()correctly. Are you sure you are using the correctproduct? - DarkBeewoocommerce.phpfile, instead I am doing it the other per template way - I will update my question with the controller. Only reason I was asking aboutshowThumbis they were using it in their view without setting it in their controller, so I was curious if it was supposed to come from somewhere else. - Bretttwigvariable set in the config. Not familiar withwoocommercemyself. But as I said in my commentproduct.is_visble()should suffice to use it inside twig - DarkBee