Skip to content

Commit

Permalink
Merge pull request Smile-SA#3372 from vahonc/3333-elasticsuite-catalo…
Browse files Browse the repository at this point in the history
…g-fallback-to-use-default-sort-direction-2.10_feature

[Catalog] Feature Smile-SA#3333, add fallback to use system config value for Sort Direction
  • Loading branch information
romainruaud authored Sep 26, 2024
2 parents 8d1e310 + cc9f9a0 commit ac34511
Showing 1 changed file with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

use Magento\Catalog\Block\Product\ProductList\Toolbar as ProductListToolbar;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Plugin which is modified the behavior of sorting arrows based on the custom sort direction attribute.
Expand All @@ -26,6 +30,8 @@
*/
class SortDirectionPerCategoryPlugin
{
const XML_PATH_LIST_DEFAULT_SORT_DIRECTION_BY = 'catalog/frontend/default_sort_direction_by';

/**
* @var CategoryRepositoryInterface
*/
Expand All @@ -36,18 +42,38 @@ class SortDirectionPerCategoryPlugin
*/
private $request;

/**
* Scope configuration.
*
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Store manager.
*
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* Toolbar constructor.
*
* @param CategoryRepositoryInterface $categoryRepository Category Repository.
* @param Http $request Http request.
* @param ScopeConfigInterface $scopeConfig Scope configuration.
* @param StoreManagerInterface $storeManager Store manager.
*/
public function __construct(
CategoryRepositoryInterface $categoryRepository,
Http $request
Http $request,
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager
) {
$this->categoryRepository = $categoryRepository;
$this->request = $request;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
}

/**
Expand All @@ -57,6 +83,7 @@ public function __construct(
* @param mixed $collection Collection.
*
* @return array
* @throws NoSuchEntityException
*/
public function beforeSetCollection(ProductListToolbar $subject, $collection)
{
Expand All @@ -69,30 +96,55 @@ public function beforeSetCollection(ProductListToolbar $subject, $collection)
return [$collection];
}

/**
* Retrieve Product List Default Sort Direction By
*
* @return string|null
* @throws NoSuchEntityException
*/
private function getProductListDefaultSortDirectionBy()
{
// Get the current store ID.
$storeId = $this->storeManager->getStore()->getId();

// Fetch system configuration value for 'default_sort_direction_by' at the store level.
return $this->scopeConfig->getValue(
self::XML_PATH_LIST_DEFAULT_SORT_DIRECTION_BY,
ScopeInterface::SCOPE_STORE,
$storeId
);
}

/**
* Get the custom sort direction from the current category.
*
* @return string|null
* @throws NoSuchEntityException
*/
private function getCustomSortDirection()
{
$categoryId = $this->request->getParam('id');

if (!$categoryId) {
return null; // Return null if category ID is not available.
return $this->getProductListDefaultSortDirectionBy(); // Fallback to system config value if no category ID.
}

try {
$category = $this->categoryRepository->get($categoryId);

// Check if the category has a custom sort direction set.
$customDirection = $category->getSortDirection();

// If a custom sort direction exists for the category and is valid, return it.
if ($customDirection && in_array($customDirection, ['asc', 'desc'])) {
return $customDirection;
}
} catch (\Exception $e) {
return null; // Handle category not found or other exceptions.
// Handle exceptions (e.g., category not found) by falling back to the system config.
return $this->getProductListDefaultSortDirectionBy();
}

return null;
// If no custom sort direction for the category, return the default system config.
return $this->getProductListDefaultSortDirectionBy();
}
}

0 comments on commit ac34511

Please sign in to comment.