使用 Twig 和 Timber 时在 WooCommerce 循环中获取正确的产品 link

Getting correct product link within WooCommerce loop when using Twig and Timber

我已经自定义了一些 WooCommerce 模板,我正在使用 Twig 模板系统和 Timber。

在类别页面上,我刚刚注意到产品图片都 link 正确地对应了相应的产品,但它们下面的标题 all link转到页面上的第一个 产品。

两者之间的区别是正确的 linking 带有照片的部分是自定义的,而错误的 linking 标题是通过 WooCommerce 挂钩生成的。

这是我各自的文件:

archive-product.php

defined( 'ABSPATH' ) || exit;

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

$posts = Timber::get_posts();
$context['products'] = $posts;

$context['search_str'] = (get_search_query()) ? get_search_query() : '';

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 );
} elseif (is_shop()) {

    $shop_id = get_option( 'woocommerce_shop_page_id' );
    $post = new TimberPost($shop_id);

    $alt_title = (!empty($post->get_field('alt_page_title')['title'])) ? $post->get_field('alt_page_title')['title'] : '';
    $context['title'] = ($alt_title) ? $alt_title : $post->post_title;

}

Timber::render( 'views/woo/archive.twig', $context );

views/woo/archive.twig

{% extends 'archive.twig' %} {# Base Archive File #}

{% block before_article %}
    {% do action('woocommerce_before_main_content') %}
{% endblock %}

{% block below_h1 %}
    {{ fn('woocommerce_result_count') }}
{% endblock %}

{% block intro_content %}
    {% 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-holder col-xs-6 col-md-4">
                        {% do action('woocommerce_shop_loop') %}
                        {% include ["woo/partials/tease-product.twig"] %}
                    </div>
                {% endfor %}
            </div>
        </div>

        {% if fn('show_products_nav') %}
            <nav class="pagination" role="navigation">
                {{ fn('wp_paginate') }}
            </nav>
        {% endif %}

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

woo/partials/tease-product.twig

<article {{ fn('post_class', ['entry'] ) }}>

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

    {% set title = (post.title) ? post.title : fn('the_title') %}

    <div class="photo {% if not post.thumbnail %}placeholder{% endif %}">
        <a href="{{ post.link }}" class="link">
            {% if post.thumbnail %}
                <img src="{{ post.thumbnail.src|resize(600)|e('esc_url') }}" class="img-responsive" alt="{{ fn('esc_attr', title) }}" />
            {% else %}
                <img src="{{ fn('wc_placeholder_img_src') }}" class="img-responsive" alt="Awaiting product image" />
            {% endif %}
        </a>
    </div>

    <div class="details">

        <h3 class="entry-title">

            {% do action('woocommerce_before_shop_loop_item') %}
            {% do action('woocommerce_before_shop_loop_item_title') %}
                {{ title|e2 }}
            {% do action( 'woocommerce_after_shop_loop_item_title' ) %}
            {% do action( 'woocommerce_after_shop_loop_item' ) %}

        </h3>

    </div>

</article>

问题区是这个位:

<h3 class="entry-title">

    {% do action('woocommerce_before_shop_loop_item') %}
    {% do action('woocommerce_before_shop_loop_item_title') %}
        {{ title|e2 }}
    {% do action( 'woocommerce_after_shop_loop_item_title' ) %}
    {% do action( 'woocommerce_after_shop_loop_item' ) %}

</h3>

据我所知,我可以用这个修改锚标签:

remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'myprefix_woocommerce_template_loop_product_link_open', 10 );

function myprefix_woocommerce_template_loop_product_link_open() {
    echo '<a href="https://www.google.com.au/">';
}

我只是不确定放置此代码的最佳位置以及如何将正确的 permalink 传递给它。

永久链接可通过全局产品变量访问,正如您通过 {{ fn('timber_set_product', post) }}

部分定义的那样

将代码放入您的 functions.php。

remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'myprefix_woocommerce_template_loop_product_link_open', 10 );

function myprefix_woocommerce_template_loop_product_link_open() {
    global $product;
    $link = $product->get_permalink();
    echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
}