3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \SalesRule \Setup ;
7
8
9
+ use Magento \Backend \App \Area \FrontNameResolver ;
10
+ use Magento \Framework \App \State ;
8
11
use Magento \Framework \DB \AggregatedFieldDataConverter ;
9
12
use Magento \Framework \DB \DataConverter \SerializedToJson ;
10
13
use Magento \Framework \DB \FieldToConvert ;
14
+ use Magento \Framework \EntityManager \MetadataPool ;
15
+ use Magento \Framework \Serialize \Serializer \Json ;
11
16
use Magento \Framework \Setup \ModuleContextInterface ;
12
17
use Magento \Framework \Setup \ModuleDataSetupInterface ;
13
18
use Magento \Framework \Setup \UpgradeDataInterface ;
14
- use Magento \Framework \EntityManager \MetadataPool ;
15
19
use Magento \SalesRule \Api \Data \RuleInterface ;
20
+ use Magento \SalesRule \Model \ResourceModel \Rule as ResourceModelRule ;
21
+ use Magento \SalesRule \Model \ResourceModel \Rule \CollectionFactory as RuleCollectionFactory ;
22
+ use Magento \SalesRule \Model \Rule as ModelRule ;
16
23
24
+ /**
25
+ * Class \Magento\SalesRule\Setup\UpgradeData
26
+ */
17
27
class UpgradeData implements UpgradeDataInterface
18
28
{
19
29
/**
@@ -27,17 +37,55 @@ class UpgradeData implements UpgradeDataInterface
27
37
private $ aggregatedFieldConverter ;
28
38
29
39
/**
30
- * UpgradeData constructor .
40
+ * Resource Model of sales rule .
31
41
*
42
+ * @var ResourceModelRule;
43
+ */
44
+ private $ resourceModelRule ;
45
+
46
+ /**
47
+ * App state.
48
+ *
49
+ * @var State
50
+ */
51
+ private $ state ;
52
+
53
+ /**
54
+ * Serializer.
55
+ *
56
+ * @var Json
57
+ */
58
+ private $ serializer ;
59
+
60
+ /**
61
+ * Rule Collection Factory.
62
+ *
63
+ * @var RuleColletionFactory
64
+ */
65
+ private $ ruleColletionFactory ;
66
+
67
+ /**
32
68
* @param AggregatedFieldDataConverter $aggregatedFieldConverter
33
69
* @param MetadataPool $metadataPool
70
+ * @param ResourceModelRule $resourceModelRule
71
+ * @param Json $serializer
72
+ * @param State $state
73
+ * @param RuleCollectionFactory $ruleColletionFactory
34
74
*/
35
75
public function __construct (
36
76
AggregatedFieldDataConverter $ aggregatedFieldConverter ,
37
- MetadataPool $ metadataPool
77
+ MetadataPool $ metadataPool ,
78
+ ResourceModelRule $ resourceModelRule ,
79
+ Json $ serializer ,
80
+ State $ state ,
81
+ RuleCollectionFactory $ ruleColletionFactory
38
82
) {
39
83
$ this ->aggregatedFieldConverter = $ aggregatedFieldConverter ;
40
84
$ this ->metadataPool = $ metadataPool ;
85
+ $ this ->resourceModelRule = $ resourceModelRule ;
86
+ $ this ->serializer = $ serializer ;
87
+ $ this ->state = $ state ;
88
+ $ this ->ruleColletionFactory = $ ruleColletionFactory ;
41
89
}
42
90
43
91
/**
@@ -46,11 +94,17 @@ public function __construct(
46
94
public function upgrade (ModuleDataSetupInterface $ setup , ModuleContextInterface $ context )
47
95
{
48
96
$ setup ->startSetup ();
49
-
50
97
if (version_compare ($ context ->getVersion (), '2.0.2 ' , '< ' )) {
51
98
$ this ->convertSerializedDataToJson ($ setup );
52
99
}
53
-
100
+ if (version_compare ($ context ->getVersion (), '2.0.3 ' , '< ' )) {
101
+ $ this ->state ->emulateAreaCode (
102
+ FrontNameResolver::AREA_CODE ,
103
+ [$ this , 'fillSalesRuleProductAttributeTable ' ],
104
+ [$ setup ]
105
+ );
106
+ $ this ->fillSalesRuleProductAttributeTable ();
107
+ }
54
108
$ setup ->endSetup ();
55
109
}
56
110
@@ -82,4 +136,40 @@ public function convertSerializedDataToJson($setup)
82
136
$ setup ->getConnection ()
83
137
);
84
138
}
139
+
140
+ /**
141
+ * Fills blank table salesrule_product_attribute with data.
142
+ *
143
+ * @return void
144
+ */
145
+ public function fillSalesRuleProductAttributeTable ()
146
+ {
147
+ $ ruleCollection = $ this ->getRuleColletion ();
148
+ /** @var ModelRule $rule */
149
+ foreach ($ ruleCollection as $ rule ) {
150
+ // Save product attributes used in rule
151
+ $ ruleProductAttributes = array_merge (
152
+ $ this ->resourceModelRule ->getProductAttributes (
153
+ $ this ->serializer ->serialize ($ rule ->getConditions ()->asArray ())
154
+ ),
155
+ $ this ->resourceModelRule ->getProductAttributes (
156
+ $ this ->serializer ->serialize ($ rule ->getActions ()->asArray ())
157
+ )
158
+ );
159
+ if (count ($ ruleProductAttributes )) {
160
+ $ this ->resourceModelRule ->setActualProductAttributes ($ rule , $ ruleProductAttributes );
161
+ }
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Get sales rule collection.
167
+ *
168
+ * @deprecated
169
+ * @return ResourceModelRule\Collection
170
+ */
171
+ private function getRuleColletion ()
172
+ {
173
+ return $ this ->ruleColletionFactory ->create ();
174
+ }
85
175
}
0 commit comments