木材和 WooCommerce 循环问题不显示价格

Timber and WooCommerce loop problem not showing price

我正在使用 Timber + WooCommerce 并尝试在首页上显示产品的自定义查询,但循环缺少价格但显示图像和标题。然而,存档和单个产品页面确实显示了价格。

查询应该在 woocommerce.php 页面上吗?我在这里错过了什么?

page.php

$context = Timber::get_context();
$page = new TimberPost();
$context['post'] = $page;

if ( is_front_page() ) {
    $top_selling = [
        'post_type' => 'product',
        'posts_per_page' => 4,
        'post_status' => 'publish'
    ];
    $context['top_selling'] =  new Timber\PostQuery($top_selling);
}

Timber::render(array('page-' . $page->post_name . '.twig', 'page.twig'), $context);

page-home.树枝

{% for post in top_selling %}
{% include 'woocommerce/tease-product.twig' %}
{% endfor %}

tease-product.php

<div class="column is-6 is-4-desktop is-3-widescreen has-text-centered">

    {{ fn('timber_set_product', post) }}

    <a href="{{ post.link }}">
        <img src="{{ post.thumbnail.src | resize(450) }}" alt="{{ post.title }}" />
    </a>

    {% if post.brands | length > 0 %}
        {% for brand in post.brands %}
            <div>{{ brand.name }}</div>
        {% endfor %}
    {% endif %}

    <a href="{{ post.link }}">
        <div>{{ post.title }}</div>
    </a>

    // This part here shows the price on archive and single-product pages' only. It needs to show the price on front page, too.
    {% do action( 'woocommerce_after_shop_loop_item_title' ) %}
</div>

woocommerce.php

<?php 

if ( ! class_exists( 'Timber' ) ) {
    echo 'Timber not activated. Make sure you activate the plugin in <a href="/wp-admin/plugins.php#timber">/wp-admin/plugins.php</a>';

    return;
}

$context            = Timber::context();
$context['sidebar'] = Timber::get_widgets( 'shop-sidebar' );

if ( is_singular( 'product' ) ) {
    $context['post']    = Timber::get_post();
    $product            = wc_get_product( $context['post']->ID );
    $context['product'] = $product;

    // Get related products
    $related_limit               = wc_get_loop_prop( 'columns' );
    $related_ids                 = wc_get_related_products( $context['post']->id, $related_limit );
    $context['related_products'] =  Timber::get_posts( $related_ids );

    // Restore the context and loop back to the main query loop.
    wp_reset_postdata();

    Timber::render( 'views/woocommerce/single-product.twig', $context );
} else {
    $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/woocommerce/archive.twig', $context );
}

除非设置 global $product,否则价格将不会以您调用的方式显示。

你的 twig 模板 {{ fn('timber_set_product', post) }} 中的函数调用应该会处理这个问题,但是如果你已经复制并粘贴了 woocommerce 和木材教程中的那个,你将需要对其进行编辑。

原函数

<?php
function timber_set_product( $post ) {
    global $product;

    if ( is_woocommerce() ) {
        $product = wc_get_product( $post->ID );
    }
}

新功能

<?php
function timber_set_product( $post ) {
    global $product;

    // is_woocommerce() does not return true on the front_page by default!

    if ( is_woocommerce() or is_front_page() ) {
        $product = wc_get_product( $post->ID );
    }
}

您甚至可以完全取消 is_woocommerce() 检查,然后由您来确保它只在适当的时间被调用。