Displaying Recently Viewed Products for Guest Users in Magento
As an experienced e-commerce developer, I often get asked how to show recently viewed products for guest users in Magento. While the platform supports this feature for registered users out of the box, it requires a bit more work for anonymous guests. Let me guide you through the process step by step.
Understanding the Challenge
Magento uses the core/session namespace to store recently viewed items data. This session is tied to the user’s ID (customer_id). For registered users, the customer_id is available, but for guest users, it remains null, which causes issues when trying to retrieve and display the products.
Our Approach: Using a Cookie
To overcome this limitation, we’ll use a cookie to store the recently viewed items data for each session. This approach allows us to provide functionality similar to Magento’s built-in solution without requiring registration.
Step 1: Create a Model to Store Recently Viewed Products
First, let’s create a model to store our recent view data. In your app/code/Vendor/Module/etc directory, you’ll need to add the following schema file:
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="recently_viewed_products">
<column name="session_id" type="varchar(255)" nullable="false"/>
<column name="product_id" type="int" unsigned="true" nullable="false"/>
<constraint name="session_id_product_id_idx" type="index" is_unique="false"/>
</table>
</schema>
Step 2: Create a Model to Interact with the Table
Generate an installer and an entity model for this table using Magento’s schema generation tool.
php bin/magento setup:db-declaration:generate-entities --module=Vendor_Module
This will generate the necessary files in app/code/Vendor/Module/Model.
Step 3: Create a Cookie Observer
Create an observer that gets triggered whenever a product is viewed. This observer will handle updating our recently view data.
class RecentlyViewedProductsObserver implements ObserverInterface
{
protected $_productRepository;
protected $recentlyViewedProductModel;
public function __construct(
\Magento\Catalog\Model\ResourceModel\ProductFactory $productRepository,
RecentlyViewedProduct $recentlyViewedProductModel
) {
$this->_productRepository = $productRepository;
$this->recentlyViewedProductModel = $recentlyViewedProductModel;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$productId = (int)$observer->getEvent()->getRequest()->getParam('id');
if ($productId) {
$this->addToRecentlyViewedProducts($productId);
}
}
protected function addToRecentlyViewedProducts($productId)
{
// Logic to add productId to the recently viewed products table
}
}
Step 4: Create a Controller to Display Recently Viewed Products
Finally, create an API controller to retrieve and display the recently viewed products.
class RecentlyViewedController extends \Magento\Framework\App\Action\Action
{
protected $recentlyViewedProductModel;
public function __construct(
\Magento\Framework\App\Action\Context $context,
RecentlyViewedProduct $recentlyViewedProductModel
) {
parent::__construct($context);
$this->recentlyViewedProductModel = $recentlyViewedProductModel;
}
public function execute()
{
// Logic to retrieve and display recently viewed products using the model
}
}
Conclusion
Displaying recently viewed products for guest users in Magento requires a creative approach using cookies and custom models. While it’s not as seamless as the built-in feature, this method allows you to provide an enhanced user experience without forcing registration.
Remember to clear your cache after making these changes:
php bin/magento cache:clean
I hope this guide helps you implement this feature in your Magento store. If you’re looking for more advanced customizations and extensions, feel free to explore our Magento development services at Laravel Company.
Don’t hesitate to reach out if you have any further questions or need additional assistance!