dongshen7407 2019-01-26 22:00
浏览 139

在Twig / Timber中获取WooCommerce产品属性并调用PHP方法

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?

  • 写回答

1条回答 默认 最新

  • dop2144 2019-07-17 21:05
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?