Skip to content

Commit

Permalink
Merge pull request #4 from Nosto/ADS-4289_implement_more_sorting_options
Browse files Browse the repository at this point in the history
ADS-4289 Support more sorting options
  • Loading branch information
TobiasGraml11 authored Nov 7, 2023
2 parents 491a914 + ca3ee43 commit cc01e1f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Nosto\NostoIntegration\Search\Request\Handler\SortHandlers;

use Nosto\NostoIntegration\Search\Request\SearchRequest;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;

class CustomFieldSortingHandler implements SortingHandlerInterface
{
public function supportsSorting(FieldSorting $fieldSorting): bool
{
return str_starts_with($fieldSorting->getField(), 'customFields');
}

public function generateSorting(FieldSorting $fieldSorting, SearchRequest $searchNavigationRequest): void
{
$searchNavigationRequest->setSort($fieldSorting->getField(), $fieldSorting->getDirection());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Nosto\NostoIntegration\Search\Request\Handler\SortHandlers;

use Nosto\NostoIntegration\Search\Request\SearchRequest;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;

class ProductNumberSortingHandler implements SortingHandlerInterface
{
public function supportsSorting(FieldSorting $fieldSorting): bool
{
return $fieldSorting->getField() === 'product.productNumber';
}

public function generateSorting(FieldSorting $fieldSorting, SearchRequest $searchNavigationRequest): void
{
$searchNavigationRequest->setSort('customFields.productNumber', $fieldSorting->getDirection());
}
}
21 changes: 21 additions & 0 deletions src/Search/Request/Handler/SortHandlers/RatingSortingHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Nosto\NostoIntegration\Search\Request\Handler\SortHandlers;

use Nosto\NostoIntegration\Search\Request\SearchRequest;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;

class RatingSortingHandler implements SortingHandlerInterface
{
public function supportsSorting(FieldSorting $fieldSorting): bool
{
return $fieldSorting->getField() === 'product.ratingAverage';
}

public function generateSorting(FieldSorting $fieldSorting, SearchRequest $searchNavigationRequest): void
{
$searchNavigationRequest->setSort('ratingValue', $fieldSorting->getDirection());
}
}
21 changes: 21 additions & 0 deletions src/Search/Request/Handler/SortHandlers/StockSortingHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Nosto\NostoIntegration\Search\Request\Handler\SortHandlers;

use Nosto\NostoIntegration\Search\Request\SearchRequest;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;

class StockSortingHandler implements SortingHandlerInterface
{
public function supportsSorting(FieldSorting $fieldSorting): bool
{
return $fieldSorting->getField() === 'product.stock';
}

public function generateSorting(FieldSorting $fieldSorting, SearchRequest $searchNavigationRequest): void
{
$searchNavigationRequest->setSort('inventoryLevel', $fieldSorting->getDirection());
}
}
10 changes: 9 additions & 1 deletion src/Search/Request/Handler/SortingHandlerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Nosto\NostoIntegration\Search\Request\Handler;

use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\CustomFieldSortingHandler;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\PriceSortingHandler;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\ProductNameSortingHandler;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\ProductNumberSortingHandler;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\RatingSortingHandler;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\ReleaseDateSortingHandler;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\ScoreSortingHandler;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\SortingHandlerInterface;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\StockSortingHandler;
use Nosto\NostoIntegration\Search\Request\Handler\SortHandlers\TopSellerSortingHandler;
use Nosto\NostoIntegration\Search\Request\SearchRequest;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
Expand All @@ -32,10 +36,14 @@ public function handle(SearchRequest $searchNavigationRequest, Criteria $criteri
protected function getSortingHandlers(): array
{
return [
new ScoreSortingHandler(),
new CustomFieldSortingHandler(),
new PriceSortingHandler(),
new ProductNameSortingHandler(),
new ProductNumberSortingHandler(),
new RatingSortingHandler(),
new ReleaseDateSortingHandler(),
new ScoreSortingHandler(),
new StockSortingHandler(),
new TopSellerSortingHandler(),
];
}
Expand Down

0 comments on commit cc01e1f

Please sign in to comment.