Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADS-4289 Support more sorting options #4

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
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
@@ -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
TobiasGraml11 marked this conversation as resolved.
Show resolved Hide resolved
{
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
2 changes: 1 addition & 1 deletion src/Search/Request/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(

private array $filters = [];

private array $sessionParams = [];
private ?array $sessionParams = null;

public function setQuery(string $query): void
{
Expand Down
Loading