5
5
*/
6
6
namespace Magento \Catalog \Controller \Product ;
7
7
8
+ use Magento \Catalog \Api \CategoryRepositoryInterface ;
9
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
8
10
use Magento \Catalog \Model \Design ;
9
- use Magento \Catalog \Model \ProductRepository ;
10
11
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
11
12
use Magento \Framework \App \Action \HttpGetActionInterface as HttpGetActionInterface ;
12
13
use Magento \Framework \App \Action \Context ;
@@ -60,14 +61,19 @@ class View extends ProductAction implements HttpGetActionInterface, HttpPostActi
60
61
private $ catalogDesign ;
61
62
62
63
/**
63
- * @var ProductRepository
64
+ * @var ProductRepositoryInterface
64
65
*/
65
66
private $ productRepository ;
66
67
68
+ /**
69
+ * @var CategoryRepositoryInterface
70
+ */
71
+ private $ categoryRepository ;
72
+
67
73
/**
68
74
* @var StoreManagerInterface
69
75
*/
70
- protected $ storeManager ;
76
+ private $ storeManager ;
71
77
72
78
/**
73
79
* Constructor
@@ -79,7 +85,8 @@ class View extends ProductAction implements HttpGetActionInterface, HttpPostActi
79
85
* @param LoggerInterface|null $logger
80
86
* @param Data|null $jsonHelper
81
87
* @param Design|null $catalogDesign
82
- * @param ProductRepository|null $productRepository
88
+ * @param ProductRepositoryInterface|null $productRepository
89
+ * @param CategoryRepositoryInterface|null $categoryRepository
83
90
* @param StoreManagerInterface|null $storeManager
84
91
*/
85
92
public function __construct (
@@ -90,7 +97,8 @@ public function __construct(
90
97
?LoggerInterface $ logger = null ,
91
98
?Data $ jsonHelper = null ,
92
99
?Design $ catalogDesign = null ,
93
- ?ProductRepository $ productRepository = null ,
100
+ ?ProductRepositoryInterface $ productRepository = null ,
101
+ ?CategoryRepositoryInterface $ categoryRepository = null ,
94
102
?StoreManagerInterface $ storeManager = null
95
103
) {
96
104
parent ::__construct ($ context );
@@ -104,7 +112,9 @@ public function __construct(
104
112
$ this ->catalogDesign = $ catalogDesign ?: ObjectManager::getInstance ()
105
113
->get (Design::class);
106
114
$ this ->productRepository = $ productRepository ?: ObjectManager::getInstance ()
107
- ->get (ProductRepository::class);
115
+ ->get (ProductRepositoryInterface::class);
116
+ $ this ->categoryRepository = $ categoryRepository ?: ObjectManager::getInstance ()
117
+ ->get (CategoryRepositoryInterface::class);
108
118
$ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()
109
119
->get (StoreManagerInterface::class);
110
120
}
@@ -196,9 +206,17 @@ public function execute()
196
206
private function applyCustomDesign (int $ productId ): void
197
207
{
198
208
$ 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
+ }
202
220
}
203
221
}
204
222
}
0 commit comments