【问题标题】:WooCommerce archive for sale Products does not have the have all capabilitiesWooCommerce 存档待售产品不具备所有功能
【发布时间】:2018-11-20 23:21:07
【问题描述】:

我正在构建一个基于 WooCommerce 的电子商务网站,并使用 Timber (Twig) 进行模板。我现在正在尝试建立一个页面(e.g. domain.com/sale),其中列出了我所有的正在销售的产品。由于我完全自定义了整个 WooCommerce 模板结构(因为我在 Twig 中进行模板化),所以不能选择短代码。

我的问题:
我设法建立了一个自定义查询来显示我的销售产品,但似乎我遗漏了一些东西,因为底部没有显示分页,我的 AJAX 过滤器也没有工作..等等..可能是因为我不是调用默认查询?即使我采用非 AJAX 方式:domain.com/sale/page/2 它也会返回我在第一页上已经得到的相同结果。字面上没有改变任何东西......

我做了什么:
我在我的 Wordpress 中创建了一个名为“Sale”的页面。转到我的index.php 并添加以下代码:

<!-- index.php -->

$products = Timber::get_posts(
    array(
        'post_type'      => 'product',
        'nopaging'               => false,
        'posts_per_archive_page' => '24',
        'meta_query'     => array(
            'relation' => 'OR',
            array( // Simple products type
                'key'           => '_sale_price',
                'value'         => 0,
                'compare'       => '>',
                'type'          => 'numeric'
            ),
            array( // Variable products type
                'key'           => '_min_variation_sale_price',
                'value'         => 0,
                'compare'       => '>',
                'type'          => 'numeric'
            )
        )
    )
);

$context['products'] = $products;

Timber::render( '/pages/landingpages/sale.twig', $context );

在我的选择中,这完全可以正常工作。而且,当请求domain.com/sale 时,它被正确的 if 子句包围。我现在去了我的sale.twig 文件并添加了以下代码:

sale.twig

$context = Timber::get_context();

{% block content %}
<div id="product_list" class="product_listing grid">

    {{ fn('woocommerce_breadcrumb', breadcrumb_settings) }}
    <h1>Sale</h1>

    <div id="product_list" class="product_listing grid">

        {# Filters #}
        <div class="product_filter">
            {{ fn('do_shortcode', '[br_filters_group group_id=7861]') }}
        </div>

        {# Order by #}
        <div class="product_sorting">
            <label for="orderby">Order by:</label>
            {{ fn('woocommerce_catalog_ordering') }}
        </div>

        {# Products #}
        <div class="product_listing-content">
            <ul class="product_results products">
                {% for product in products %}
                    {% include ('partials/loop-product.twig') %}
                {% endfor %}
            </ul>
            <div class="products-footer">
                {% include 'partials/pagination.twig' %}
            </div>
        </div>

    </div>
</div>
{% endblock content %}

在这一点上,我必须说我正在使用BeRocket AJAX Filters 作为我的过滤器。包含的文件loop-product.twigpagination.twig 如下所示:

loop-product.twig //为简单起见删除了一些行

<li id="{{ 'product-' ~ product._sku }}" class="product-item {{ product._stock_status }}">
    {{ product.name }}
</li>

pagination.twig

<ul class="pagination clearfix">
    {% if posts.pagination.prev %}
        <li class="prev-item">
            <a href="{{posts.pagination.prev.link}}" class="prev {{posts.pagination.prev.link|length ? '' : 'invisible'}}">
                <span class="sr-only">Prev</span>
            </a>
        </li>
    {% endif %}
    {% for page in posts.pagination.pages %}
        <li class="page">
            {% if page.link %}
                <a href="{{page.link}}" class="{{page.class}}">{{page.title}}</a>
            {% else %}
                <span class="{{page.class}}">{{page.title}}</span>
            {% endif %}
        </li>
    {% endfor %}
    {% if posts.pagination.next %}
        <li class="next-item">
            <a href="{{posts.pagination.next.link}}" class="next {{posts.pagination.next.link|length ? '' : 'invisible'}}">
                <span class="sr-only">Next</span>
            </a>
        </li>
    {% endif %}
</ul>

再次提醒我不要误会:我以正确的方式接收我的所有产品。我的问题是它的行为不像存档页面。分页、过滤器等。

也许对这个话题有完全不同的方法,但似乎它是唯一的一种。我在网上找到了一大堆类似问题的代码 sn-ps,但没有一个适合我...

任何帮助表示赞赏!


编辑:
仅用于测试,我尝试添加常见的 WooCommerce 短代码 [sale_products] 并且至少我的过滤器再次工作。我的分页仍然没有出现...

【问题讨论】:

    标签: php wordpress woocommerce twig


    【解决方案1】:

    我会尽力帮忙。如果我不在基地,请原谅我。古登堡块对我来说仍然很难掌握。

    您的 sale.twig 正在动态提取数据并使用 JS 通过 php include 填充内容,但它实际上从未通过 WP 挂钩将内容写入 /sale 页面或帖子从中提取数据的数据库。这是故意的吗?

    还请注意,应该为您提供来自 dom 的分页的 twig js 与通过 link_template.php 创建的 dom 不匹配 看看

    我觉得你必须这样称呼它

    <?php previous_post_link( '<strong>%link</strong>' ); ?> 
    

    link_template

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-27
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2020-05-16
      相关资源
      最近更新 更多