How To Add Custom Attribute Data To Recently Viewed Products Widget

Written by Jigar Patel

Oct 15

How To Add Custom Attribute  Data To Recently Viewed Products Widget

Widgets Feature Product Elements Content Magento Admin 1024x379 1we all will get to know what is Widget in Magento 2.

Widgets provide powerful functionalities that are used to add dynamic or static content to store pages or blocks. You can add widgets in any CMS block of Magento 2 to display such content or elements, for example:

  • Dynamic product data
  • Dynamic lists of the recently viewed products
  • Promotional banners
  • Interactive navigation elements and action blocks
  • Dynamic flash elements that are inserted in content pages

In the recently viewed products widget settings there are only name, price, image, and learn_more available.

For the product attribute name i’ve spotted the file


<column name="name" component="Magento_Catalog/js/product/name" sortOrder="1" displayArea="details-area">
        <label translate="true">Name</label>



<strong if="isAllowed()"
    <a attr="href: $row().url" html="$col.getLabel($row())"/>

Steps To Add Custom Attribute Data To Recently Viewed Products Widget

We are already learned how to create a basic module in Magento 2. We need to create module.xml and registration.php files. You can create module.xml and registration.php  from this tutorial.

Here we are using Dolphin as Vendor name and Recently as the name of the module.  You can change this according to your Vendor and Module name.

we have a custom product attribute color and we are going to include this attribute to be loaded in the recently viewed widget list view.

Step 1: Create widget file

Create a widget file widget.xml in app/code/Dolphin/Recently/etc folder with the following code.

<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi=""
    <widget id="catalog_recently_viewed">
            <parameter name="show_attributes" xsi:type="multiselect" required="true" visible="true">
                    <option name="color" value="color">
                        <label translate="true">Color</label>

Once apply the above code you will be able to choose the option in Widgets->Choose Widget->Widget Options->Product attributes to show
How To Add Custom Attribute Data To Recently Viewed Products Widget


Step 2: Create Dependency injection

Create dependency injection file di.xml in the app/code/Dolphin/Recently/etc folder with the following code.

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Ui\DataProvider\Product\ProductRenderCollectorComposite">
            <argument name="productProviders" xsi:type="array">
                <item name="color" xsi:type="object">\Dolphin\Recently\Ui\DataProvider\Product\Listing\Collector\Color</item>

Step 3: Create an Extention Attribute

Create Extention Attribute file extension_attributes.xml in the app/code/Dolphin/Recently/etc folder with the following code.

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\ProductRenderInterface">
        <attribute code="color" type="string"/>

Step 4: Create a class file for DataProvider

Create Class file Color.php in the app/code/Dolphin/Recently/Ui/DataProvider/Product/Listing/Collector folder with the following code.


namespace Dolphin\Recently\Ui\DataProvider\Product\Listing\Collector;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductRenderExtensionFactory;
use Magento\Catalog\Api\Data\ProductRenderInterface;
use Magento\Catalog\Ui\DataProvider\Product\ProductRenderCollectorInterface;

 * Class Color
 * @package Dolphin\Recently\Ui\DataProvider\Product\Listing\Collector
class Color implements ProductRenderCollectorInterface
    const KEY = "color";

     * @var ProductRenderExtensionFactory
    private $productRenderExtensionFactory;

     * color constructor.
     * @param ProductRenderExtensionFactory $productRenderExtensionFactory
    public function __construct(
        ProductRenderExtensionFactory $productRenderExtensionFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {

        $this->productRenderExtensionFactory = $productRenderExtensionFactory;
        $this->productFactory = $productFactory;

     * @param ProductInterface $product
     * @param ProductRenderInterface $productRender
    public function collect(ProductInterface $product, ProductRenderInterface $productRender)
        $extensionAttributes = $productRender->getExtensionAttributes();

        if (!$extensionAttributes) {
            $extensionAttributes = $this->productRenderExtensionFactory->create();

        if($color = $product->getColor()) {
            $colorValue= $this->getOptionLabelByValue('color',$color);


    public function getOptionLabelByValue($attributeCode,$optionId)
        $product = $this->productFactory->create();
        $isAttributeExist = $product->getResource()->getAttribute($attributeCode);
        $optionText = '';
        if ($isAttributeExist && $isAttributeExist->usesSource()) {
            $optionText = $isAttributeExist->getSource()->getOptionText($optionId);
        return $optionText;

Step 5: Create widget definition

Create widget definition widget_recently_viewed.xml in the app/code/Dolphin/Recently/view/frontend/ui_component folder with the following code.include our attribute as a column in the widget definition.

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="widget_columns">
        <column name="color" component="Dolphin_Recently/js/product/color" displayArea="description-area">
                <label translate="true">color</label>

Step 6: Create a js file

Create js file color.js in the app/code/Dolphin/Recently/view/frontend/web/js/product folder with the following code.

], function (Column, columnStatusValidator) {
    'use strict';

    return Column.extend({

         * @param row
         * @returns {boolean}
        hasValue: function (row) {
            return "color" in row['extension_attributes'];

         * @param row
         * @returns {*}
        getValue: function (row) {
            return row['extension_attributes']['color'];

         * @param row
         * @returns {*|boolean}
        isAllowed: function (row) {
            console.log(columnStatusValidator.isValid(this.source(), 'color', 'show_attributes') && this.hasValue(row) );
            return (columnStatusValidator.isValid(this.source(), 'color', 'show_attributes') && this.hasValue(row) );


Step 7: Create Html template file

Create html template file color.html in the app/code/Dolphin/Recently/view/frontend/web/template/product folder with the following code.

<strong if="isAllowed($row())" class="product product-item-color color">
    <span class="label" text="label"></span>
    <span class="value" text="getValue($row())"></span>

Now execute the below command and go to the system configuration page:

[dt_highlight color=”” text_color=”” bg_color=””]php bin/magento setup:upgrade[/dt_highlight]
[dt_highlight color=”” text_color=”” bg_color=””]php bin/magento setup:static-content:deploy -f[/dt_highlight]
[dt_highlight color=”” text_color=”” bg_color=””]php bin/magento cache:flush[/dt_highlight]




I Hope, This instruction will be helpful for you.

Happy Coding with magento2!! 🙂

If you have any difficulties regarding this blog, do consider them posting in the Comments section below!

I’m here to help.

Thank you!

Written by Jigar Patel

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.