Removing Sort Options from Magento Category Display Settings
The Problem: Hidden Sort Options Still Appear
You’ve unchecked “use all attributes” and selected specific sorting options for your category. You’ve cleared cache and reindexed data, but to no availâthe unwanted sort options remain visible in the dropdown menu.
Your modified toolbar.phtml file is an attempt to directly manipulate the sort options by excluding the “Position” attribute using an if-statement. However, this approach seems to be having no effect on your current issue.
Understanding Magento’s Sorting Logic
Magento’s default behavior includes all available attributes for sorting in the dropdown menu. The logic isn’t tied to your category settings or specific product attributes you’ve selected. Instead, it provides a comprehensive list of potential sort criteria that can be applied across multiple categories and products.
To change this behavior, we need to adjust Magento’s core configuration files and override some of its built-in functionality.
Overriding the Core Functionality
Step 1: Disable “Use All Attributes”
The first step is to ensure that you have disabled “use all attributes” for your category. Go to Catalog > Categories > select your category > Display Settings tab. Under “Sort By,” make sure “Use All Attributes” is set to “No.”
Step 2: Create a Custom Module
To override Magento’s core sorting logic, we’ll create a custom module. This allows us to modify the default behavior without directly editing core files, which is a best practice for maintaining upgrade compatibility.
Navigate to your Magento root directory and create a new folder structure under app/code:
app/code/YourVendor/SortOptions
Within this folder, create the following files and directories:
registration.php: Register your module with Magento.etc/config.xml: Define configuration settings for your module.etc/di.xml: Configure dependency injection for your custom sorting logic.
Step 3: Implement Custom Sorting Logic
In your di.xml file, you’ll need to override the Magento\Catalog\Model\Layer\Filter\Attribute class. This is where Magento generates the list of available sort options.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Layer\Filter\Attribute">
<arguments>
<argument name="availableAttributes" xsi:type="array">
<item name="category" xsi:type="string">catalog_category_id</item>
<item name="price" xsi:type="string">catalog_product_price</item>
<!-- Add your desired sort options here -->
</argument>
</arguments>
</type>
</config>
Step 4: Clear Cache
After implementing the custom module, you need to clear Magento’s cache for the changes to take effect. You can do this by running the following command in your terminal:
php bin/magento cache:clean
Conclusion
Removing unwanted sort options from a Magento category requires a combination of adjusting display settings and overriding core functionality using a custom module.
Remember, it’s crucial to follow best practices when modifying Magento’s default behavior. Directly editing core files can lead to upgrade issues in future versions.
If you’re new to Magento development or unsure about implementing these changes, consider seeking professional assistance from experienced Magento developers like the team at Laravel Company.
By following this guide and understanding the underlying logic, you’ll be able to effectively control which sort options are displayed in your Magento store’s category dropdown menus.