Displaying Product View Counts in Magento Category Listing
Are you looking to add view counts for your products directly on category listing pages in Magento? You’re not alone! In this post, we’ll guide you through the process of retrieving and displaying product view counts based on a specific product ID.
Understanding the Challenge with View Data
In earlier versions of Magento (up to 1.9), product view data was accessible via reports/product_collection. However, starting from Magento 2.0, this data has been moved into an entirely different structure due to architectural changes in the new platform version.
Don’t worry though! In Magento 2, you can still retrieve view count data, but it requires a slightly different approach that involves custom queries and indexing setup.
Retrieving Product View Counts
The key lies in using the Magento\Catalog\Model\Product model combined with a custom query to fetch the desired product view count. Here’s a step-by-step guide:
- First, open your terminal and run the following command to make sure your Magento 2 installation is up-to-date:
composer update
-
Next, navigate to your app/code directory and create a new custom module folder if you don’t have one already.
-
Inside your custom module’s etc/ folder, create an additional XML file for the database setup. Open this file and add the following code:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_Catalog">
<sequence>
<module name="Magento_Reports"/>
</sequence>
</module>
</config>
-
Now, let’s jump into the controller action that will fetch the product view count. Open your custom module and navigate to the Controller folder.
-
Create a new PHP file (e.g., ProductController.php) and add the following code:
namespace Magento\Catalog\Controller;
use Magento\Framework\App\Action;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
class ProductController extends Action implements \Magento\Framework\App\Action\HttpGetActionInterface {
public function execute(): void {
$productId = (int)$this->getRequest()->getParam('id');
if (!$productId) {
throw new NotFoundException(__('Not Found'));
}
$resultPage = $this->_view->getPage($pageIdentifier);
if ($productId > 0) {
$viewCount = $this->_objectManager->create('\Magento\Reports\Model\ResourceModel\Product')->getViewedProductCount((int)$productId);
// Add your logic to handle the view count
}
}
}
This code creates a basic controller that accepts a product ID and fetches the corresponding view count using the getViewedProductCount method from the reports model.
Displaying View Counts in Category Listing
To display these view counts directly on category listing pages, you’ll need to modify your category templates. This involves adding a new XML layout file and updating your template files accordingly. For detailed instructions on this process, please refer to our comprehensive guide:
Displaying Custom Data in Magento 2 Category Listing Pages
Conclusion
In conclusion, retrieving product view counts in Magento 2 requires a bit more effort compared to older versions. However, with the correct approach and understanding of the new architecture, you can successfully fetch and display view count data for your products.
Remember to keep your Magento installation up-to-date, create custom modules for your code, and update your category templates properly to reflect the fetched view counts.
Happy coding! Let us know if you have any further questions or need assistance with integrating this into your existing project.