首页 > 解决方案 > 在 Twig/Timber 中获取 WooCommerce 产品属性并调用 PHP 方法

问题描述

我正在构建一个 WooCommerce 网站,同时使用 Twig 模板系统和 Timber WordPress 插件,并按照本指南获取取笑模板。

然而,看看 WooCommerce 自己的默认设置templates/content-product.php,他们在最上面有这个:

// Ensure visibility.
if ( empty( $product ) || ! $product->is_visible() ) {
    return;
}

所以我在想我们应该在模板文件中做同样的检查吗?但是我不确定如何在 Twig 中检查这个值。

我试过dump(post.is_visible())了,但每个人都回来了(bool) false

我也不知道从哪里来showthumb,也找不到关于这个特定设置/属性的太多信息或文档。

编辑:这是我的控制器:(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 );

这是我的archive.twig文件:

{% 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 %}

这是我的 tease-product.twig文件:

<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>

如何检索该is_visible方法的正确值?

标签: phpwordpresswoocommercetwigtimber

解决方案


我认为OP现在已经解决了这个问题。但是,以供将来参考...

您应该能够{{ post.is_visible }}输出该值。

同样,if下面的两个陈述都对我有用。

{% if (product.is_visible) %} {% if (product.is_visible()) %}

现在,我对 WooCommerce 和 Timber 还很陌生,is_visible我对 . 但是,我知道我可以在树枝模板中使用WC_Product 成员函数,例如 {{ 变量。函数名}},即。{{ product.get_title }}或者{{ product.get_price }},所以我希望is_visible不会有什么不同。


推荐阅读