From cc9f9a077f454ffd62e0afdcfa978cca19f17ff0 Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Mon, 9 Sep 2024 16:39:00 +0300 Subject: [PATCH] [Catalog] Feature #3333, add fallback to use system config value for Sort Direction --- .../SortDirectionPerCategoryPlugin.php | 60 +++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/module-elasticsuite-catalog/Plugin/Category/Toolbar/SortDirectionPerCategoryPlugin.php b/src/module-elasticsuite-catalog/Plugin/Category/Toolbar/SortDirectionPerCategoryPlugin.php index 28fd83d0c..b0924c12c 100644 --- a/src/module-elasticsuite-catalog/Plugin/Category/Toolbar/SortDirectionPerCategoryPlugin.php +++ b/src/module-elasticsuite-catalog/Plugin/Category/Toolbar/SortDirectionPerCategoryPlugin.php @@ -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. @@ -26,6 +30,8 @@ */ class SortDirectionPerCategoryPlugin { + const XML_PATH_LIST_DEFAULT_SORT_DIRECTION_BY = 'catalog/frontend/default_sort_direction_by'; + /** * @var CategoryRepositoryInterface */ @@ -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; } /** @@ -57,6 +83,7 @@ public function __construct( * @param mixed $collection Collection. * * @return array + * @throws NoSuchEntityException */ public function beforeSetCollection(ProductListToolbar $subject, $collection) { @@ -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(); } }