7

I want to change product detail page layout according to product specific price range I know it is possible for using product_id and product_sku like below.

<catalog_product_view_id_productid>

And

<catalog_product_view_sku_productsku>

But how can I achieve it for specific price range ?

7

You can get this using plugin. Try following way:

app/code/SR/MagentoStackExchange/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\Controller\Product\View">
        <plugin name="SR_MagentoStackExchange::product_view" type="SR\MagentoStackExchange\Plugin\Catalog\Controller\Product\View" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoStackExchange/Plugin/Catalog/Controller/Product/View.php


<?php
namespace SR\MagentoStackExchange\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;

    /**
     * View constructor.
     *
     * @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() <= 34) {
                        $pageConfig = $resultPage->getConfig();
                        $pageConfig->setPageLayout('2columns-left');
                    }
                } catch (NoSuchEntityException $e) {

                }
            }
        }

        return $resultPage;
    }
}
  • Thanks @Sohel Rana its working you are awesome ! – Manish Goswami Jan 15 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.