Sorting Magento Products by Date Added Across Categories
Sorting products in Magento by their date of addition can be a common requirement for e-commerce stores that want to highlight new arrivals or seasonal items. However, the built-in sorting options often sort categories first and then sort within each category separately. If you want to achieve a flat, category-agnostic sorting order, read on.
The Challenge: Category-Based Sorting
The default Magento approach sorts products based on the following hierarchy:
- Category
- Position (defined in catalog > manage categories)
- Name
- Date added
This means that within each category, products are sorted by their position and name, with newer items appearing later. If you have a single page displaying multiple categories, this can result in an odd sorting order where a product from one category appears before a newer product from another.
The Solution: Custom Product Collection
To achieve the desired flat sorting order, we need to create a custom product collection that ignores the category hierarchy and sorts only by the ‘created_at’ field. Here’s how you can do it:
-
Create a new module file in your Magento installation under
app/code/Your/Vendor/. -
Inside this folder, create a
Blockdirectory and then anProduct.phpfile inside it. -
In
Product.php, define the following block class:
class Product extends \Magento\Framework\View\Element\Template
{
protected $_productCollectionFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context, $data);
}
public function getSortedProducts()
{
$collection = $this->_productCollectionFactory->create();
// Add 'created_at' field to the collection
$collection->addAttributeToSort('created_at', 'asc');
return $collection;
}
}
This block retrieves a product collection and sorts it by the ‘created_at’ attribute in ascending order (newest first).
- Now, you can use this block in your template files:
<!-- Assuming you're using a phtml file -->
<?php $sortedProducts = $block->getSortedProducts(); ?>
- In your loop, display the products as needed.
Best Practices and Additional Considerations
- Use pagination: Remember to apply pagination to your sorted product list to prevent it from becoming too long.
- Handle visibility: Ensure that you are only fetching products that are visible in the catalog (i.e., enabled and not hidden).
- Caching: If your site has a large number of products, consider implementing caching for the sorted collection to improve performance.
Conclusion
Sorting Magento products by date added while ignoring categories can be achieved through custom product collections. By following this approach, you’ll create a flat, category-agnostic sorting order that displays newer products first across all categories on a single page.
Remember that this is a fundamental change to the default Magento behavior, so always test thoroughly before deploying it in a live environment.
For more advanced e-commerce development needs, consider working with experienced professionals. Visit Laravel Company for expert consultations and solutions tailored to your business requirements.