5
5
*/
6
6
namespace Magento \Catalog \Controller \Product ;
7
7
8
+ use Magento \Catalog \Model \Design ;
9
+ use Magento \Catalog \Model \ProductRepository ;
8
10
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
9
11
use Magento \Framework \App \Action \HttpGetActionInterface as HttpGetActionInterface ;
10
12
use Magento \Framework \App \Action \Context ;
11
13
use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \Controller \Result \Forward ;
15
+ use Magento \Framework \Controller \Result \ForwardFactory ;
16
+ use Magento \Framework \Controller \Result \Redirect ;
17
+ use Magento \Framework \DataObject ;
18
+ use Magento \Framework \Exception \NoSuchEntityException ;
19
+ use Magento \Framework \Json \Helper \Data ;
12
20
use Magento \Framework \View \Result \PageFactory ;
13
21
use Magento \Catalog \Controller \Product as ProductAction ;
22
+ use Magento \Store \Model \StoreManagerInterface ;
23
+ use Psr \Log \LoggerInterface ;
14
24
15
25
/**
16
26
* View a product on storefront. Needs to be accessible by POST because of the store switching
@@ -25,57 +35,84 @@ class View extends ProductAction implements HttpGetActionInterface, HttpPostActi
25
35
protected $ viewHelper ;
26
36
27
37
/**
28
- * @var \Magento\Framework\Controller\Result\ ForwardFactory
38
+ * @var ForwardFactory
29
39
*/
30
40
protected $ resultForwardFactory ;
31
41
32
42
/**
33
- * @var \Magento\Framework\View\Result\ PageFactory
43
+ * @var PageFactory
34
44
*/
35
45
protected $ resultPageFactory ;
36
46
37
47
/**
38
- * @var \Psr\Log\ LoggerInterface
48
+ * @var LoggerInterface
39
49
*/
40
50
private $ logger ;
41
51
42
52
/**
43
- * @var \Magento\Framework\Json\Helper\ Data
53
+ * @var Data
44
54
*/
45
55
private $ jsonHelper ;
46
56
57
+ /**
58
+ * @var Design
59
+ */
60
+ private $ catalogDesign ;
61
+
62
+ /**
63
+ * @var ProductRepository
64
+ */
65
+ private $ productRepository ;
66
+
67
+ /**
68
+ * @var StoreManagerInterface
69
+ */
70
+ protected $ storeManager ;
71
+
47
72
/**
48
73
* Constructor
49
74
*
50
75
* @param Context $context
51
76
* @param \Magento\Catalog\Helper\Product\View $viewHelper
52
- * @param \Magento\Framework\Controller\Result\ ForwardFactory $resultForwardFactory
77
+ * @param ForwardFactory $resultForwardFactory
53
78
* @param PageFactory $resultPageFactory
54
- * @param \Psr\Log\LoggerInterface $logger
55
- * @param \Magento\Framework\Json\Helper\Data $jsonHelper
79
+ * @param LoggerInterface|null $logger
80
+ * @param Data|null $jsonHelper
81
+ * @param Design|null $catalogDesign
82
+ * @param ProductRepository|null $productRepository
83
+ * @param StoreManagerInterface|null $storeManager
56
84
*/
57
85
public function __construct (
58
86
Context $ context ,
59
87
\Magento \Catalog \Helper \Product \View $ viewHelper ,
60
- \ Magento \ Framework \ Controller \ Result \ ForwardFactory $ resultForwardFactory ,
88
+ ForwardFactory $ resultForwardFactory ,
61
89
PageFactory $ resultPageFactory ,
62
- \Psr \Log \LoggerInterface $ logger = null ,
63
- \Magento \Framework \Json \Helper \Data $ jsonHelper = null
90
+ ?LoggerInterface $ logger = null ,
91
+ ?Data $ jsonHelper = null ,
92
+ ?Design $ catalogDesign = null ,
93
+ ?ProductRepository $ productRepository = null ,
94
+ ?StoreManagerInterface $ storeManager = null
64
95
) {
65
96
parent ::__construct ($ context );
66
97
$ this ->viewHelper = $ viewHelper ;
67
98
$ this ->resultForwardFactory = $ resultForwardFactory ;
68
99
$ this ->resultPageFactory = $ resultPageFactory ;
69
100
$ this ->logger = $ logger ?: ObjectManager::getInstance ()
70
- ->get (\ Psr \ Log \ LoggerInterface::class);
101
+ ->get (LoggerInterface::class);
71
102
$ this ->jsonHelper = $ jsonHelper ?: ObjectManager::getInstance ()
72
- ->get (\Magento \Framework \Json \Helper \Data::class);
103
+ ->get (Data::class);
104
+ $ this ->catalogDesign = $ catalogDesign ?: ObjectManager::getInstance ()
105
+ ->get (Design::class);
106
+ $ this ->productRepository = $ productRepository ?: ObjectManager::getInstance ()
107
+ ->get (ProductRepository::class);
108
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()
109
+ ->get (StoreManagerInterface::class);
73
110
}
74
111
75
112
/**
76
113
* Redirect if product failed to load
77
114
*
78
- * @return \Magento\Framework\Controller\Result\ Redirect|\Magento\Framework\Controller\Result\ Forward
115
+ * @return Redirect|Forward
79
116
*/
80
117
protected function noProductRedirect ()
81
118
{
@@ -93,7 +130,7 @@ protected function noProductRedirect()
93
130
/**
94
131
* Product view action
95
132
*
96
- * @return \Magento\Framework\Controller\Result\ Forward|\Magento\Framework\Controller\Result\ Redirect
133
+ * @return Forward|Redirect
97
134
*/
98
135
public function execute ()
99
136
{
@@ -130,16 +167,17 @@ public function execute()
130
167
}
131
168
132
169
// Prepare helper and params
133
- $ params = new \ Magento \ Framework \ DataObject ();
170
+ $ params = new DataObject ();
134
171
$ params ->setCategoryId ($ categoryId );
135
172
$ params ->setSpecifyOptions ($ specifyOptions );
136
173
137
174
// Render page
138
175
try {
176
+ $ this ->applyCustomDesign ($ productId );
139
177
$ page = $ this ->resultPageFactory ->create ();
140
178
$ this ->viewHelper ->prepareAndRender ($ page , $ productId , $ this , $ params );
141
179
return $ page ;
142
- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ e ) {
180
+ } catch (NoSuchEntityException $ e ) {
143
181
return $ this ->noProductRedirect ();
144
182
} catch (\Exception $ e ) {
145
183
$ this ->logger ->critical ($ e );
@@ -148,4 +186,19 @@ public function execute()
148
186
return $ resultForward ;
149
187
}
150
188
}
189
+
190
+ /**
191
+ * Apply custom design from product design settings
192
+ *
193
+ * @param int $productId
194
+ * @throws NoSuchEntityException
195
+ */
196
+ private function applyCustomDesign (int $ productId ): void
197
+ {
198
+ $ 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 ());
202
+ }
203
+ }
151
204
}
0 commit comments