11
11
use Magento \Store \Model \StoreFactory ;
12
12
use Psr \Log \LoggerInterface as Logger ;
13
13
use Magento \Framework \Registry ;
14
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
15
+ use Magento \Catalog \Model \Product ;
16
+ use Magento \Catalog \Model \Product \Type as ProductTypes ;
14
17
15
18
class Builder
16
19
{
@@ -39,6 +42,11 @@ class Builder
39
42
*/
40
43
protected $ storeFactory ;
41
44
45
+ /**
46
+ * @var ProductRepositoryInterface
47
+ */
48
+ private $ productRepository ;
49
+
42
50
/**
43
51
* Constructor
44
52
*
@@ -47,61 +55,87 @@ class Builder
47
55
* @param Registry $registry
48
56
* @param WysiwygModel\Config $wysiwygConfig
49
57
* @param StoreFactory|null $storeFactory
58
+ * @param ProductRepositoryInterface|null $productRepository
50
59
*/
51
60
public function __construct (
52
61
ProductFactory $ productFactory ,
53
62
Logger $ logger ,
54
63
Registry $ registry ,
55
64
WysiwygModel \Config $ wysiwygConfig ,
56
- StoreFactory $ storeFactory = null
65
+ StoreFactory $ storeFactory = null ,
66
+ ProductRepositoryInterface $ productRepository = null
57
67
) {
58
68
$ this ->productFactory = $ productFactory ;
59
69
$ this ->logger = $ logger ;
60
70
$ this ->registry = $ registry ;
61
71
$ this ->wysiwygConfig = $ wysiwygConfig ;
62
72
$ this ->storeFactory = $ storeFactory ?: \Magento \Framework \App \ObjectManager::getInstance ()
63
73
->get (\Magento \Store \Model \StoreFactory::class);
74
+ $ this ->productRepository = $ productRepository ?: \Magento \Framework \App \ObjectManager::getInstance ()
75
+ ->get (ProductRepositoryInterface::class);
64
76
}
65
77
66
78
/**
67
79
* Build product based on user request
68
80
*
69
81
* @param RequestInterface $request
70
82
* @return \Magento\Catalog\Model\Product
83
+ * @throws \RuntimeException
71
84
*/
72
85
public function build (RequestInterface $ request )
73
86
{
74
- $ productId = (int )$ request ->getParam ('id ' );
75
- /** @var $product \Magento\Catalog\Model\Product */
76
- $ product = $ this ->productFactory ->create ();
77
- $ product ->setStoreId ($ request ->getParam ('store ' , 0 ));
78
- $ store = $ this ->storeFactory ->create ();
79
- $ store ->load ($ request ->getParam ('store ' , 0 ));
80
-
87
+ $ productId = (int ) $ request ->getParam ('id ' );
88
+ $ storeId = $ request ->getParam ('store ' , 0 );
89
+ $ attributeSetId = (int ) $ request ->getParam ('set ' );
81
90
$ typeId = $ request ->getParam ('type ' );
82
- if (!$ productId && $ typeId ) {
83
- $ product ->setTypeId ($ typeId );
84
- }
85
91
86
- $ product ->setData ('_edit_mode ' , true );
87
92
if ($ productId ) {
88
93
try {
89
- $ product-> load ($ productId );
94
+ $ product = $ this -> productRepository -> getById ($ productId, true , $ storeId );
90
95
} catch (\Exception $ e ) {
91
- $ product-> setTypeId (\ Magento \ Catalog \ Model \ Product \Type ::DEFAULT_TYPE );
96
+ $ product = $ this -> createEmptyProduct (ProductTypes ::DEFAULT_TYPE , $ attributeSetId , $ storeId );
92
97
$ this ->logger ->critical ($ e );
93
98
}
99
+ } else {
100
+ $ product = $ this ->createEmptyProduct ($ typeId , $ attributeSetId , $ storeId );
94
101
}
95
102
96
- $ setId = (int )$ request ->getParam ('set ' );
97
- if ($ setId ) {
98
- $ product ->setAttributeSetId ($ setId );
99
- }
103
+ $ store = $ this ->storeFactory ->create ();
104
+ $ store ->load ($ storeId );
100
105
101
106
$ this ->registry ->register ('product ' , $ product );
102
107
$ this ->registry ->register ('current_product ' , $ product );
103
108
$ this ->registry ->register ('current_store ' , $ store );
104
- $ this ->wysiwygConfig ->setStoreId ($ request ->getParam ('store ' ));
109
+
110
+ $ this ->wysiwygConfig ->setStoreId ($ storeId );
111
+
112
+ return $ product ;
113
+ }
114
+
115
+ /**
116
+ * @param int $typeId
117
+ * @param int $attributeSetId
118
+ * @param int $storeId
119
+ * @return \Magento\Catalog\Model\Product
120
+ */
121
+ private function createEmptyProduct ($ typeId , $ attributeSetId , $ storeId ): Product
122
+ {
123
+ /** @var $product \Magento\Catalog\Model\Product */
124
+ $ product = $ this ->productFactory ->create ();
125
+ $ product ->setData ('_edit_mode ' , true );
126
+
127
+ if ($ typeId !== null ) {
128
+ $ product ->setTypeId ($ typeId );
129
+ }
130
+
131
+ if ($ storeId !== null ) {
132
+ $ product ->setStoreId ($ storeId );
133
+ }
134
+
135
+ if ($ attributeSetId ) {
136
+ $ product ->setAttributeSetId ($ attributeSetId );
137
+ }
138
+
105
139
return $ product ;
106
140
}
107
141
}
0 commit comments