In this tutorial, Today I will explain to you how to change product view page layout based on price in Magento 2. Sometimes, customer requirement is like that they want to set product page layout when the product price is like XYZ. But, Magento doesn’t provide default functionality or any configuration about change product page layout based on price.
At that time, we need to do customization for that. So, Let’s see how to change the product page layout in Magento 2.
You may have also like this :
1) Create di.xml file for add plugin at app/code/RH/Helloworld/etc/frontend/ folder and paste the below code :
<?xml version="1.0"?>
<!--
/**
* Created By : Rohan Hapani
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Controller\Product\View">
<plugin name="RH_Helloworld::change_product_view" type="RH\Helloworld\Plugin\Catalog\Controller\Product\View" sortOrder="1" />
</type>
</config>
2) Create View.php plugin file at app/code/RH/Helloworld/Plugin/Catalog/Controller/Product/ and paste the below code :
<?php
/**
* Created By : Rohan Hapani
*/
namespace RH\Helloworld\Plugin\Catalog\Controller\Product;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class View
{
/**
* @var RequestInterface
*/
private $request;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @param RequestInterface $request
* @param ProductRepositoryInterface $productRepository
* @param StoreManagerInterface $storeManager
*/
public function __construct(
RequestInterface $request,
ProductRepositoryInterface $productRepository,
StoreManagerInterface $storeManager
) {
$this->request = $request;
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
}
public function afterExecute(\Magento\Catalog\Controller\Product\View $subject, $resultPage)
{
if ($resultPage instanceof ResultInterface)
{
$productId = (int) $this->request->getParam('id');
if ($productId)
{
try
{
$product = $this->productRepository->getById($productId, false, $this->storeManager->getStore()->getId());
if ($product->getFinalPrice() <= 100)
{
$pageConfig = $resultPage->getConfig();
$pageConfig->setPageLayout('2columns-right'); //Set your page layout here.
}
}
catch (NoSuchEntityException $e)
{
// Add you exception message here.
}
}
}
return $resultPage;
}
}
In this plugin file, you need to set your layout in setPageLayout(). Here, I set when product price will less than equal to 100 then, the product page layout will be display 2columns-right.
Now, Just clean the cache and check the output.
I hope this blog will helpful for easily understand how to change product view page layout based on price in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution