How to Add Custom Text or HTML on Product Page Using Plugin in Magento 2
An E-commerce store’s core strength is the product it offers. The presentation of the product can make or break a sale.
A product page is where the potential customers spent their maximum time before making the purchase. Also, it is the product page that gets shared the maximum number of times.
Hence, the product page must be designed strategically to attract more customers and improve sales. The default Magento 2 product page may fall short to fulfill the business requirements related to the product page design.
One may want to customize the product page by adding custom links, notification messages, additional functionalities, etc.
For example, you may want to offer the gift wrapping facility at an additional cost along with the product purchase, or you may want to display a custom notification bar.
It can be done easily with the programmatic solution to add custom text or HTML on product page using plugin in Magento 2.
Use the below solution to add any custom checkboxes, links, messages, etc. in your product page using plugin in Magento 2 store.
Enhance the product page design and enjoy higher conversion rates!
Steps to Add Custom Text or HTML on Product Page Using Plugin in Magento 2
-
- Create di.xml at app/code/Vendor/Module/etc/frontend and use the below code.
1234567<?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-text" type="Vendor\Module\Plugin\Catalog\Block\Product\View"/></type></config> - Create View.php at app/code/Vendor/Module/Plugin/Catalog/Block/Product and follow the below code.
123456789101112131415<?phpnamespace Vendor\Module\Plugin\Catalog\Block\Product;use Magento\Catalog\Block\Product\View as ProductView;class View{private $displayBlocks = ['product.info.addtocart'];// you can add layout references as per your need to display like: product.info.price, product.info.review, etc.public function afterToHtml(ProductView $subject, $html){if (in_array($subject->getNameInLayout(), $this->displayBlocks)) {return $html . '<div class="custom-text">Test 1</div>';}return $html;}}That’s it.
If you have any doubts, do mention them in the Comments section below.
If you are customizing the product page, you can use a custom price template in Magento 2 to change the way prices are displayed on it.
I would be glad to help.
Feel free to share the solution with Magento community via social media.
Thank You.
- Create di.xml at app/code/Vendor/Module/etc/frontend and use the below code.
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
2 Comments
I create a textbox to this page, how to take the values from the textbox and show in check out page
Hello Sneha,
Add custom input in add to cart form and get/save value using add to cart event/controller.
Thank You