Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,25 @@ class Migration1615470599ExampleSorting extends MigrationStep

## Create individual sorting at runtime

You can subscribe to the `ProductListingCriteriaEvent` to add a `ProductSortingEntity` as available sorting on the fly. If you don't know how to do this, head over to our [Listening to events](../../framework/event/listening-to-events.md) guide.
You can subscribe to the `ProductListingCollectSortingEvent` to add a `ProductSortingEntity` as an available sorting on the fly. If you don't know how to do this, head over to our [Listening to events](../../framework/event/listening-to-events.md) guide.

::: info
This event is available since Shopware 6.7.15.0.
:::

::: info
While possible, it is not recommended adding an individual sorting at runtime. If you just wish for your individual sorting to be not editable by users in the Administration, create a migration and set the parameter `locked` to be `true`.
:::

Here's an example how your subscriber could look like:
Here's an example of what your subscriber could look like:

```php
// <plugin root>/src/Subscriber/ExampleListingSubscriber.php
<?php declare(strict_types=1);

namespace Swag\BasicExample\Subscriber;

use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingCollection;
use Shopware\Core\Content\Product\Events\ProductListingCollectSortingEvent;
use Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingEntity;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand All @@ -117,17 +120,12 @@ class ExampleListingSubscriber implements EventSubscriberInterface
public static function getSubscribedEvents(): array
{
return [
// be sure to subscribe with high priority to add you sorting before the default shopware logic applies
// otherwise storefront will throw a ProductSortingNotFoundException
ProductListingCriteriaEvent::class => ['addMyCustomSortingToStorefront', 500],
ProductListingCollectSortingEvent::class => 'addMyCustomSortingToStorefront',
];
}

public function addMyCustomSortingToStorefront(ProductListingCriteriaEvent $event): void
public function addMyCustomSortingToStorefront(ProductListingCollectSortingEvent $event): void
{
/** @var ProductSortingCollection $availableSortings */
$availableSortings = $event->getCriteria()->getExtension('sortings') ?? new ProductSortingCollection();

$myCustomSorting = new ProductSortingEntity();
$myCustomSorting->setId(Uuid::randomHex());
$myCustomSorting->setActive(true);
Expand All @@ -143,13 +141,11 @@ class ExampleListingSubscriber implements EventSubscriberInterface
],
]);

$availableSortings->add($myCustomSorting);

$event->getCriteria()->addExtension('sortings', $availableSortings);
$event->getSortings()->add($myCustomSorting);
}
}
```

## Next steps

To [add a custom filter](./add-listing-filters.md) to your listing in the Storefront head over to the corresponding guide.
To [add a custom filter](./add-listing-filters.md) to your listing in the Storefront, head over to the corresponding guide.
Loading