小编Mal*_*sch的帖子

更改 Shopware 6 中产品列表的默认排序

我在本文档之后创建了一个新的排序

    <argument>a-sorting</argument>
    <argument>New Sorting</argument>
    <argument type="collection">
        <argument key="product.markAsTopseller">desc</argument>
        <argument key="product.updatedAt">desc</argument>
    </argument>
    <tag name="shopware.sales_channel.product_listing.sorting" />
</service>
Run Code Online (Sandbox Code Playgroud)

您现在可以在前端选择新的排序,它工作正常。但我不知道如何将此排序设置为页面默认值。我的意思是产品列表应该在页面加载后最初进行排序。

我用ProductListingCriteriaEventProductListingResultEvent解决了这个问题

<?php declare(strict_types=1);

namespace MyPlugin\Storefront\Subscriber;

use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ProductSubscriber implements EventSubscriberInterface
{

    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents()
    {
        return [
            ProductListingCriteriaEvent::class  => 'handleRequest',
            ProductListingResultEvent::class    => 'handleResult'
        ];
    }

    /**
     * @param ProductListingResultEvent $event
     */
    public function handleResult(ProductListingResultEvent $event): void
    {
        $request = $event->getRequest();

        /* Sorting is not selected …
Run Code Online (Sandbox Code Playgroud)

shopware symfony4

5
推荐指数
1
解决办法
2233
查看次数

标签 统计

shopware ×1

symfony4 ×1