Skip to content

Commit 1391203

Browse files
committed
MC-41534: [On-Premise] Product themes don't apply as expected
1 parent 7f6e9a8 commit 1391203

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

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

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

8+
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
810
use Magento\Catalog\Model\Design;
9-
use Magento\Catalog\Model\ProductRepository;
1011
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1112
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
1213
use Magento\Framework\App\Action\Context;
@@ -60,14 +61,19 @@ class View extends ProductAction implements HttpGetActionInterface, HttpPostActi
6061
private $catalogDesign;
6162

6263
/**
63-
* @var ProductRepository
64+
* @var ProductRepositoryInterface
6465
*/
6566
private $productRepository;
6667

68+
/**
69+
* @var CategoryRepositoryInterface
70+
*/
71+
private $categoryRepository;
72+
6773
/**
6874
* @var StoreManagerInterface
6975
*/
70-
protected $storeManager;
76+
private $storeManager;
7177

7278
/**
7379
* Constructor
@@ -79,7 +85,8 @@ class View extends ProductAction implements HttpGetActionInterface, HttpPostActi
7985
* @param LoggerInterface|null $logger
8086
* @param Data|null $jsonHelper
8187
* @param Design|null $catalogDesign
82-
* @param ProductRepository|null $productRepository
88+
* @param ProductRepositoryInterface|null $productRepository
89+
* @param CategoryRepositoryInterface|null $categoryRepository
8390
* @param StoreManagerInterface|null $storeManager
8491
*/
8592
public function __construct(
@@ -90,7 +97,8 @@ public function __construct(
9097
?LoggerInterface $logger = null,
9198
?Data $jsonHelper = null,
9299
?Design $catalogDesign = null,
93-
?ProductRepository $productRepository = null,
100+
?ProductRepositoryInterface $productRepository = null,
101+
?CategoryRepositoryInterface $categoryRepository = null,
94102
?StoreManagerInterface $storeManager = null
95103
) {
96104
parent::__construct($context);
@@ -104,7 +112,9 @@ public function __construct(
104112
$this->catalogDesign = $catalogDesign ?: ObjectManager::getInstance()
105113
->get(Design::class);
106114
$this->productRepository = $productRepository ?: ObjectManager::getInstance()
107-
->get(ProductRepository::class);
115+
->get(ProductRepositoryInterface::class);
116+
$this->categoryRepository = $categoryRepository ?: ObjectManager::getInstance()
117+
->get(CategoryRepositoryInterface::class);
108118
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
109119
->get(StoreManagerInterface::class);
110120
}
@@ -196,9 +206,17 @@ public function execute()
196206
private function applyCustomDesign(int $productId): void
197207
{
198208
$product = $this->productRepository->getById($productId, false, $this->storeManager->getStore()->getId());
199-
$settings = $this->catalogDesign->getDesignSettings($product);
200-
if ($settings->getCustomDesign()) {
201-
$this->catalogDesign->applyCustomDesign($settings->getCustomDesign());
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+
}
202220
}
203221
}
204222
}

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

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

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

10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
1011
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
1113
use Magento\Catalog\Controller\Product\View;
1214
use Magento\Catalog\Helper\Product\View as ViewHelper;
1315
use Magento\Catalog\Model\Design;
14-
use Magento\Catalog\Model\ProductRepository;
1516
use Magento\Framework\App\Action\Context;
1617
use Magento\Framework\App\RequestInterface;
1718
use Magento\Framework\Controller\Result\ForwardFactory;
@@ -49,10 +50,15 @@ class ViewTest extends TestCase
4950
private $catalogDesignMock;
5051

5152
/**
52-
* @var ProductRepository|MockObject
53+
* @var ProductRepositoryInterface|MockObject
5354
*/
5455
private $productRepositoryMock;
5556

57+
/**
58+
* @var CategoryRepositoryInterface|MockObject
59+
*/
60+
private $categoryRepositoryMock;
61+
5662
/**
5763
* @var ProductInterface|MockObject
5864
*/
@@ -93,9 +99,8 @@ protected function setUp(): void
9399
$this->catalogDesignMock = $this->getMockBuilder(Design::class)
94100
->disableOriginalConstructor()
95101
->getMock();
96-
$this->productRepositoryMock = $this->getMockBuilder(ProductRepository::class)
97-
->disableOriginalConstructor()
98-
->getMock();
102+
$this->productRepositoryMock = $this->getMockForAbstractClass(ProductRepositoryInterface::class);
103+
$this->categoryRepositoryMock = $this->getMockForAbstractClass(CategoryRepositoryInterface::class);
99104
$this->productInterfaceMock = $this->getMockBuilder(ProductInterface::class)
100105
->disableOriginalConstructor()
101106
->getMock();
@@ -112,6 +117,7 @@ protected function setUp(): void
112117
null,
113118
$this->catalogDesignMock,
114119
$this->productRepositoryMock,
120+
$this->categoryRepositoryMock,
115121
$this->storeManagerMock
116122
);
117123
}

0 commit comments

Comments
 (0)