Skip to content

Commit 76825d1

Browse files
committed
MC-41534: [On-Premise] Product themes don't apply as expected
1 parent 19ee17c commit 76825d1

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

app/code/Magento/Catalog/Controller/Product/View.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Catalog\Controller\Product;
77

8-
use Magento\Catalog\Api\CategoryRepositoryInterface;
98
use Magento\Catalog\Api\ProductRepositoryInterface;
109
use Magento\Catalog\Model\Design;
1110
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
@@ -65,11 +64,6 @@ class View extends ProductAction implements HttpGetActionInterface, HttpPostActi
6564
*/
6665
private $productRepository;
6766

68-
/**
69-
* @var CategoryRepositoryInterface
70-
*/
71-
private $categoryRepository;
72-
7367
/**
7468
* @var StoreManagerInterface
7569
*/
@@ -86,7 +80,6 @@ class View extends ProductAction implements HttpGetActionInterface, HttpPostActi
8680
* @param Data|null $jsonHelper
8781
* @param Design|null $catalogDesign
8882
* @param ProductRepositoryInterface|null $productRepository
89-
* @param CategoryRepositoryInterface|null $categoryRepository
9083
* @param StoreManagerInterface|null $storeManager
9184
*/
9285
public function __construct(
@@ -98,7 +91,6 @@ public function __construct(
9891
?Data $jsonHelper = null,
9992
?Design $catalogDesign = null,
10093
?ProductRepositoryInterface $productRepository = null,
101-
?CategoryRepositoryInterface $categoryRepository = null,
10294
?StoreManagerInterface $storeManager = null
10395
) {
10496
parent::__construct($context);
@@ -113,8 +105,6 @@ public function __construct(
113105
->get(Design::class);
114106
$this->productRepository = $productRepository ?: ObjectManager::getInstance()
115107
->get(ProductRepositoryInterface::class);
116-
$this->categoryRepository = $categoryRepository ?: ObjectManager::getInstance()
117-
->get(CategoryRepositoryInterface::class);
118108
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
119109
->get(StoreManagerInterface::class);
120110
}
@@ -206,17 +196,9 @@ public function execute()
206196
private function applyCustomDesign(int $productId): void
207197
{
208198
$product = $this->productRepository->getById($productId, false, $this->storeManager->getStore()->getId());
209-
$productSettings = $this->catalogDesign->getDesignSettings($product);
210-
if ($productSettings->getCustomDesign()) {
211-
$this->catalogDesign->applyCustomDesign($productSettings->getCustomDesign());
212-
} else {
213-
foreach ($product->getCategoryIds() as $categoryId) {
214-
$category = $this->categoryRepository->get($categoryId, $this->storeManager->getStore()->getId());
215-
$categorySettings = $this->catalogDesign->getDesignSettings($category);
216-
if ($categorySettings->getCustomDesign()) {
217-
$this->catalogDesign->applyCustomDesign($categorySettings->getCustomDesign());
218-
}
219-
}
199+
$settings = $this->catalogDesign->getDesignSettings($product);
200+
if ($settings->getCustomDesign()) {
201+
$this->catalogDesign->applyCustomDesign($settings->getCustomDesign());
220202
}
221203
}
222204
}

app/code/Magento/Catalog/Model/Design.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
namespace Magento\Catalog\Model;
88

9+
use Magento\Catalog\Api\CategoryRepositoryInterface;
910
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager as CategoryLayoutManager;
1011
use Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager as ProductLayoutManager;
1112
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Data\Collection\AbstractDb;
1314
use Magento\Framework\DataObject;
15+
use Magento\Framework\Exception\NoSuchEntityException;
1416
use Magento\Framework\Model\Context;
1517
use Magento\Framework\Model\ResourceModel\AbstractResource;
1618
use Magento\Framework\Registry;
@@ -60,6 +62,16 @@ class Design extends \Magento\Framework\Model\AbstractModel
6062
*/
6163
private $productLayoutUpdates;
6264

65+
/**
66+
* @var Session
67+
*/
68+
private $catalogSession;
69+
70+
/**
71+
* @var CategoryRepositoryInterface
72+
*/
73+
private $categoryRepository;
74+
6375
/**
6476
* @param Context $context
6577
* @param Registry $registry
@@ -71,6 +83,8 @@ class Design extends \Magento\Framework\Model\AbstractModel
7183
* @param TranslateInterface|null $translator
7284
* @param CategoryLayoutManager|null $categoryLayoutManager
7385
* @param ProductLayoutManager|null $productLayoutManager
86+
* @param Session|null $catalogSession
87+
* @param CategoryRepositoryInterface|null $categoryRepository
7488
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7589
*/
7690
public function __construct(
@@ -83,7 +97,9 @@ public function __construct(
8397
array $data = [],
8498
TranslateInterface $translator = null,
8599
?CategoryLayoutManager $categoryLayoutManager = null,
86-
?ProductLayoutManager $productLayoutManager = null
100+
?ProductLayoutManager $productLayoutManager = null,
101+
?Session $catalogSession = null,
102+
?CategoryRepositoryInterface $categoryRepository = null
87103
) {
88104
$this->_localeDate = $localeDate;
89105
$this->_design = $design;
@@ -92,6 +108,10 @@ public function __construct(
92108
?? ObjectManager::getInstance()->get(CategoryLayoutManager::class);
93109
$this->productLayoutUpdates = $productLayoutManager
94110
?? ObjectManager::getInstance()->get(ProductLayoutManager::class);
111+
$this->catalogSession = $catalogSession
112+
?? ObjectManager::getInstance()->get(Session::class);
113+
$this->categoryRepository = $categoryRepository
114+
?? ObjectManager::getInstance()->get(CategoryRepositoryInterface::class);
95115
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
96116
}
97117

@@ -118,6 +138,17 @@ public function getDesignSettings($object)
118138
{
119139
if ($object instanceof Product) {
120140
$currentCategory = $object->getCategory();
141+
if (!$currentCategory) {
142+
$lastId = $this->catalogSession->getLastVisitedCategoryId();
143+
if ($object->canBeShowInCategory($lastId)) {
144+
$categoryId = $lastId;
145+
try {
146+
$currentCategory = $this->categoryRepository->get($categoryId);
147+
} catch (NoSuchEntityException $e) {
148+
$currentCategory = null;
149+
}
150+
}
151+
}
121152
} else {
122153
$currentCategory = $object;
123154
}

app/code/Magento/Catalog/Test/Unit/Controller/Product/ViewTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
namespace Magento\Catalog\Test\Unit\Controller\Product;
99

10-
use Magento\Catalog\Api\CategoryRepositoryInterface;
1110
use Magento\Catalog\Api\Data\ProductInterface;
12-
use Magento\Catalog\Api\ProductRepositoryInterface;
1311
use Magento\Catalog\Controller\Product\View;
1412
use Magento\Catalog\Helper\Product\View as ViewHelper;
1513
use Magento\Catalog\Model\Design;
14+
use Magento\Catalog\Model\ProductRepository;
1615
use Magento\Framework\App\Action\Context;
1716
use Magento\Framework\App\RequestInterface;
1817
use Magento\Framework\Controller\Result\ForwardFactory;
@@ -50,15 +49,10 @@ class ViewTest extends TestCase
5049
private $catalogDesignMock;
5150

5251
/**
53-
* @var ProductRepositoryInterface|MockObject
52+
* @var ProductRepository|MockObject
5453
*/
5554
private $productRepositoryMock;
5655

57-
/**
58-
* @var CategoryRepositoryInterface|MockObject
59-
*/
60-
private $categoryRepositoryMock;
61-
6256
/**
6357
* @var ProductInterface|MockObject
6458
*/
@@ -99,8 +93,9 @@ protected function setUp(): void
9993
$this->catalogDesignMock = $this->getMockBuilder(Design::class)
10094
->disableOriginalConstructor()
10195
->getMock();
102-
$this->productRepositoryMock = $this->getMockForAbstractClass(ProductRepositoryInterface::class);
103-
$this->categoryRepositoryMock = $this->getMockForAbstractClass(CategoryRepositoryInterface::class);
96+
$this->productRepositoryMock = $this->getMockBuilder(ProductRepository::class)
97+
->disableOriginalConstructor()
98+
->getMock();
10499
$this->productInterfaceMock = $this->getMockBuilder(ProductInterface::class)
105100
->disableOriginalConstructor()
106101
->getMock();
@@ -117,7 +112,6 @@ protected function setUp(): void
117112
null,
118113
$this->catalogDesignMock,
119114
$this->productRepositoryMock,
120-
$this->categoryRepositoryMock,
121115
$this->storeManagerMock
122116
);
123117
}

0 commit comments

Comments
 (0)