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?
I reckon OP has solved the problem by now. But, for future reference...
You should be able to do {{ post.is_visible }}
to output the value.
Similarly, both if
statements below work for me.
{% if (product.is_visible) %}
{% if (product.is_visible()) %}
Now, I am fairly new to WooCommerce and Timber, and I don't know much about is_visible
, so I apologize if I am wrong here. But, I know I Am able to use WC_Product member functions in twig templates like {{ variable.function_name }}, ie. {{ product.get_title }}
or {{ product.get_price }}
, so I expect is_visible
to be no different.