Sorting Magento Products by Entity ID in Version 1.6
Sorting your products by entity ID can be incredibly useful for managing large catalogs, especially when you need to quickly locate specific items. While Magento doesn’t provide this option out-of-the-box, it’s possible to implement custom sorting without touching the core code.
Understanding the Current Sort Options
Before we dive into creating our custom sort, let’s review the built-in options:
- Position (ASC/DESC): This is the default sort method, arranging products based on their position in the category.
- Price: Sorts by product price, either ascending or descending.
Both of these methods are useful but don’t directly correlate to the underlying entity ID assigned by Magento during installation.
Creating Your Custom Sort Option: Step-by-Step
To add a “Sort by Entity ID” option, we’ll need to create a new module. Here’s how:
- Create the Module: First, generate your custom module using the following command in your terminal:
php bin/magento setup:module:create --name=YourCompany_SortById
Replace YourCompany with your actual company name.
- Override the Sort Options
In your new module’s etc/view/frontend/sales.xml file, add a new sort option under the catalog_product_list:
<referenceBlock name="product.list">
<block class="Magento\Catalog\Block\Product\ListProduct" name="product_list.your_sort_by_id" template="Magento_Catalog::product/your-sort-by-id.phtml">
<arguments>
<argument name="sortBy" xsi:type="string">entity_id</argument>
<argument name="direction" xsi:type="string">desc</argument>
<argument name="mode" xsi:type="string">custom</argument>
</arguments>
</block>
</referenceBlock>
- Create the New Template
Create a new template named Magento_Catalog::product/your-sort-by-id.phtml in your module’s view/frontend/templates folder.
In this file, you can use the following code to display the product list:
<?php if (count($block->getProductList())): ?>
<div class="products">
<?php foreach ($block->getProductList() as $_product): ?>
<div class="item product product-item">
// Your product HTML goes here
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
- Set the Custom Sort as Default
To make this your default sort option, navigate to the Admin panel > Stores > Configuration > Catalog > Frontend > Products Listing Sort by.
Select “Custom” and enter your_sort_by_id as the Sort Order ID.
Key Considerations
Remember that sorting by entity ID won’t be suitable for all scenarios. The entity IDs are assigned sequentially upon product creation, so they don’t necessarily correspond to a logical order based on attributes like date or category.
Also, always ensure you have clean backups before making any changes to your production site.
Conclusion: Sorting Your Way
Adding custom sorting options in Magento doesn’t require modifying core files. By creating a new module and following the steps above, you can quickly implement a “Sort by Entity ID” option tailored to your needs.
This approach keeps your store secure and maintainable while providing additional functionality for managing large catalogs effectively.
Remember, if you need further assistance with custom Magento development or e-commerce strategy, feel free to reach out to Laravel Company. We’re always here to help you maximize the power of your platform.