How To Add HTML Or Custom Text On Product View Page In Magento 2

Hello Everyone,                                     

In this blog, we will learn about how to Add HTML or Custom Text on Product View Page using Plugin in Magento 2.

Product View Page is the most important page in Magento 2 where customers can spend maximum time shopping. 

Without wasting your time, let us guide you straight away. Follow the easy step given below to Add HTML or Custom Text on Product View Page in Magento 2.

STEPS FOR ADD HTML OR CUSTOM TEXT ON PRODUCT View PAGE IN MAGENTO 2

Step 1: Create di.xml file

app/code/Vendor/Extension/etc/frontend/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Catalog\Block\Product\View">

        <plugin name="custom-button" type="Vendor\Module\Plugin\Catalog\Block\Product\View"/>

    </type>

</config>

Step 2: Create View.php file

app/code/Vendor/Extension/Plugin/Catalog/Block/Product/View.php

<?php

namespace Vendor\Extension\Plugin\Catalog\Block\Product;

use Magento\Catalog\Block\Product\View as ProductView;

class View

{

    private $displayBlocks = ['product.info.addtocart'];

    public function afterToHtml(ProductView $subject, $html)

    {

        if (in_array($subject->getNameInLayout(), $this->displayBlocks)) {

            return $html . '<button class="action primary tocart">Coupon Code</button>';

        }

        return $html;

    }

}

you can add layout references as per your need to display like: product.info.price, product.info.review, etc.

Step 3: Finally run the below commands

$ php bin/magento cache:clean

$ php bin/magento cache:flush

Step 4: Output:

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add HTML or Custom Text on Product View Page using Plugin in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *