How To

Magento 2: How to Add Frontend Product URL Column to Admin Product Grid

Hello Magento Kins,

How are things going? I will guide How to Add Frontend Product URL Column to Admin Product Grid in this article of Magento 2 How To series. You shall get some good ideas from the previous tutorial about Magento 2: Show Related Products On The List Page.

Basically, there is a requirement to view the product on the front-end or if you want to see a product in your front end then you have to search that product by name or with the help of SKU from the front end. And one has to find the SKU and product name manually by visiting the product grid and then copy that SKU or the product name and after this search that product from the Front-End.

Therefore, to overcome this problem here is the solution in which there is some code of implementation and with the help of that user can effortlessly redirect that product from the product grid from the admin. Here, just after writing some mandatory code we successfully add one column on the product grid which contains a link which is simply redirected to view the product on the frontend.

Steps to Code

Step 1: First you need to create product_listing.xml in following path:

app\code\Vendor\Extension\view\adminhtml\ui_component\product_listing.xml

<?xml version="1.0" encoding="UTF-8"?>

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="  urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">

<columns name="product_columns">

    <column name="preview" class="Vendor\Extension\Ui\Component\Columns\Productgrid">

        <argument name="data" xsi:type="array">

            <item name="config" xsi:type="array">

                <item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>

                <item name="filter" xsi:type="string">text</item>

                <item name="label" xsi:type="string" translate="true">View</item>

            </item>

        </argument>

    </column>

</columns>   




</listing>

Step 2: Now you need to add Productgrid.php file in the following path:

app\code\Vendor\Extension\Ui\Component\Columns\Productgrid.php

<?php

namespace Vendor\Extension\Ui\Component\Columns;

use \Magento\Framework\View\Element\UiComponent\ContextInterface;

use \Magento\Framework\View\Element\UiComponentFactory;

use \Magento\Ui\Component\Listing\Columns\Column;

use \Magento\Catalog\Api\ProductRepositoryInterface;




class Productgrid extends Column

{

    protected $ProductRepositoryInterface;




    public function __construct(

      ContextInterface $context,

      UiComponentFactory $uiComponentFactory,

      ProductRepositoryInterface $ProductRepositoryInterface,

      array $components = [], array $data = []

    )

    {

      parent::__construct($context, $uiComponentFactory, $components, $data);

      $this->ProductRepositoryInterface = $ProductRepositoryInterface;

    }




    public function prepareDataSource(array $dataSource)

    {

      if (isset($dataSource["data"]["items"])) {

          $fieldName = $this->getData("name");




        foreach ($dataSource["data"]["items"] as $key => $item)  {

           $product = $this->ProductRepositoryInterface->getById($item['entity_id'], false);

           $url =  $product->getUrlModel()->getUrlInStore($product, ['_escape' => true]);

           $html = "<a target='_blank' href=" .$url. ">";

           $html .=  "view";

           $html .= "</a>";

           $dataSource["data"]["items"][$key][$fieldName] =  $html;

        }

    }

    return $dataSource;

   }

}

And after implementing the following steps you will be displayed the added column on the admin grid of the product.

Wrapping Up

Apply the simple steps illustrated above and make yourself more capable of viewing directly the desired products in the frontend and no need to work hard and waste time in searching the SKU and the product name to view the products in the frontend. And if face any problem in implementing the above steps then contact our Support Team the will guide you accordingly.

Do comment down your opinions regarding this tutorial and if it seems to be helpful then share it with your Magento pals and help them improve their knowledge.

Happy Coding?

Click to rate this post!
[Total: 4 Average: 2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago