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

[Catalog] Feature #3333, add fallback to use system config value for Sort Direction #3372

Merged
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 @@ -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();
}
}
Loading