Bạn có thể có được điều này bằng cách sử dụng plugin. Hãy thử cách sau:
ứng dụng / mã / 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>
ứng dụng / mã / SR / MagentoStackExchange / Plugin / Catalogue / Trình điều khiển / Sản phẩm / 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;
}
}