Showing New Products in a Magento Category
As a senior developer specializing in e-commerce platforms like Magento, I often encounter requests to display new products within specific categories based on date ranges. In this post, I’ll guide you through the process of achieving this using both XML and PHP methods.
Understanding the Problem
The user is struggling to show only the new products (defined by their ‘new from’ and ‘new to’ dates) within a custom category layout. They’re looking for solutions via XML or PHP.
Why Show New Products in a Category?
Displaying new products within categories helps improve customer experience, drives engagement, and can boost conversion rates. It’s a standard e-commerce practice that enhances the shopping journey.
[Learn more about enhancing Magento UX from our expert guide at https://laravelcompany.com/magento-ux-best-practices/]
The XML Solution: Category Layout Update
To filter products in your category via XML, update the layout file for your custom category. This involves using the collection_pager and product_collection handles to apply filters.
- Navigate to your theme’s template directory (e.g.,
app/design/frontend/Vendor/Theme Magento_Theme/layout). - Create or edit the XML file corresponding to your category layout.
- Add the following code inside the
<layout>tags:
<referenceContainer name="category.products">
<block type="Magento\Catalog\Block\Product\New" name="category.new.product.list"
before="-" template="Magento_Catalog::product/new/grid.phtml">
<action method="setCategoryId"><argument name="category_id" xsi:type="number">15</argument></action>
</block>
</referenceContainer>
Replace 15 with the actual ID of your category.
- Save and refresh your storefront.
This will display new products (based on Magento’s default ‘new from’ setting) in a grid layout within the specified category view.
The PHP Solution: Manual Collection Filtering
If you prefer to handle this via PHP, follow these steps:
- Open your custom category template file (usually located at
app/design/frontend/Vendor/Theme/template/catalog/category/view.phtml). - Find or add a new PHP block within the product listing section.
- Use the following code to fetch and filter products:
<?php
$categoryId = 15; // Replace with your category ID
$fromDate = '2022-01-01'; // Set your desired 'new from' date
$toDate = '2022-12-31'; // Set your desired 'new to' date
$products = $block->getLayer()->getNewProducts($categoryId, $fromDate, $toDate);
foreach ($products as $product) {
echo $product->getName() . ' - New from: ' . $product->getNewFromDate();
}
?>
- Save and refresh your storefront.
This PHP code fetches the new products within the specified category and date range directly using Magento’s layer model methods.
Best Practices for Showing New Products
When displaying new products:
- Be Specific: Use a narrow timeframe (e.g., 1 week) to keep content fresh.
- Cross-link: Link to product pages to drive traffic and conversions.
- Combine Filters: Consider using additional filters like price or popularity.
Conclusion: Displaying New Products Effortlessly
In this post, we explored two effective methods for showing new products within a Magento category:
- The XML approach via layout updates is simpler but limited to the default ‘new from’ setting.
- The PHP method offers more flexibility and control over date ranges.
Choose the solution that best fits your project’s requirements. Both options leverage Magento’s built-in features, ensuring seamless integration with your existing platform.
Remember, displaying new products is a key strategy for driving customer engagement and sales (see our guide on Magento marketing strategies at https://laravelcompany.com/magento-marketing-strategies/).
With these techniques, you can create engaging category pages that highlight the latest arrivals and keep shoppers excited about your offerings.