3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
- namespace Magento \SalesRule \Setup ;
7
6
8
- use Magento \Framework \DB \AggregatedFieldDataConverter ;
9
- use Magento \Framework \DB \DataConverter \SerializedToJson ;
10
- use Magento \Framework \DB \FieldToConvert ;
11
- use Magento \Framework \Setup \ModuleContextInterface ;
12
- use Magento \Framework \Setup \ModuleDataSetupInterface ;
13
- use Magento \Framework \Setup \UpgradeDataInterface ;
14
- use Magento \Framework \EntityManager \MetadataPool ;
15
- use Magento \SalesRule \Api \Data \RuleInterface ;
7
+ namespace Magento \SalesRule \Setup ;
16
8
17
- class UpgradeData implements UpgradeDataInterface
9
+ /**
10
+ * Class \Magento\SalesRule\Setup\UpgradeData
11
+ */
12
+ class UpgradeData implements \Magento \Framework \Setup \UpgradeDataInterface
18
13
{
19
14
/**
20
- * @var MetadataPool
15
+ * @var \Magento\Framework\EntityManager\ MetadataPool
21
16
*/
22
17
private $ metadataPool ;
23
18
24
19
/**
25
- * @var AggregatedFieldDataConverter
20
+ * @var \Magento\Framework\DB\ AggregatedFieldDataConverter
26
21
*/
27
22
private $ aggregatedFieldConverter ;
28
23
29
24
/**
30
- * UpgradeData constructor .
25
+ * Resource Model of sales rule .
31
26
*
32
- * @param AggregatedFieldDataConverter $aggregatedFieldConverter
33
- * @param MetadataPool $metadataPool
27
+ * @var \Magento\SalesRule\Model\ResourceModel\Rule;
28
+ */
29
+ private $ resourceModelRule ;
30
+
31
+ /**
32
+ * App state.
33
+ *
34
+ * @var \Magento\Framework\App\State
35
+ */
36
+ private $ state ;
37
+
38
+ /**
39
+ * Serializer.
40
+ *
41
+ * @var \Magento\Framework\Serialize\SerializerInterface
42
+ */
43
+ private $ serializer ;
44
+
45
+ /**
46
+ * Rule Collection Factory.
47
+ *
48
+ * @var \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory
49
+ */
50
+ private $ ruleColletionFactory ;
51
+
52
+ /**
53
+ * @param \Magento\Framework\DB\AggregatedFieldDataConverter $aggregatedFieldConverter
54
+ * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
55
+ * @param \Magento\SalesRule\Model\ResourceModel\Rule $resourceModelRule
56
+ * @param \Magento\Framework\Serialize\SerializerInterface $serializer
57
+ * @param \Magento\Framework\App\State $state
58
+ * @param \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $ruleColletionFactory
34
59
*/
35
60
public function __construct (
36
- AggregatedFieldDataConverter $ aggregatedFieldConverter ,
37
- MetadataPool $ metadataPool
61
+ \Magento \Framework \DB \AggregatedFieldDataConverter $ aggregatedFieldConverter ,
62
+ \Magento \Framework \EntityManager \MetadataPool $ metadataPool ,
63
+ \Magento \SalesRule \Model \ResourceModel \Rule $ resourceModelRule ,
64
+ \Magento \Framework \Serialize \SerializerInterface $ serializer ,
65
+ \Magento \Framework \App \State $ state ,
66
+ \Magento \SalesRule \Model \ResourceModel \Rule \CollectionFactory $ ruleColletionFactory
38
67
) {
39
68
$ this ->aggregatedFieldConverter = $ aggregatedFieldConverter ;
40
69
$ this ->metadataPool = $ metadataPool ;
70
+ $ this ->resourceModelRule = $ resourceModelRule ;
71
+ $ this ->serializer = $ serializer ;
72
+ $ this ->state = $ state ;
73
+ $ this ->ruleColletionFactory = $ ruleColletionFactory ;
41
74
}
42
75
43
76
/**
44
77
* @inheritdoc
45
78
*/
46
- public function upgrade (ModuleDataSetupInterface $ setup , ModuleContextInterface $ context )
47
- {
79
+ public function upgrade (
80
+ \Magento \Framework \Setup \ModuleDataSetupInterface $ setup ,
81
+ \Magento \Framework \Setup \ModuleContextInterface $ context
82
+ ) {
48
83
$ setup ->startSetup ();
49
-
50
84
if (version_compare ($ context ->getVersion (), '2.0.2 ' , '< ' )) {
51
85
$ this ->convertSerializedDataToJson ($ setup );
52
86
}
53
-
87
+ if (version_compare ($ context ->getVersion (), '2.0.3 ' , '< ' )) {
88
+ $ this ->state ->emulateAreaCode (
89
+ \Magento \Backend \App \Area \FrontNameResolver::AREA_CODE ,
90
+ [$ this , 'fillSalesRuleProductAttributeTable ' ],
91
+ [$ setup ]
92
+ );
93
+ $ this ->fillSalesRuleProductAttributeTable ();
94
+ }
54
95
$ setup ->endSetup ();
55
96
}
56
97
57
98
/**
58
99
* Convert metadata from serialized to JSON format:
59
100
*
60
- * @param ModuleDataSetupInterface $setup
61
- *
101
+ * @param \Magento\Framework\Setup\ModuleDataSetupInterface $setup *
62
102
* @return void
63
103
*/
64
104
public function convertSerializedDataToJson ($ setup )
65
105
{
66
- $ metadata = $ this ->metadataPool ->getMetadata (RuleInterface::class);
106
+ $ metadata = $ this ->metadataPool ->getMetadata (\ Magento \ SalesRule \ Api \ Data \ RuleInterface::class);
67
107
$ this ->aggregatedFieldConverter ->convert (
68
108
[
69
- new FieldToConvert (
70
- SerializedToJson::class,
109
+ new \ Magento \ Framework \ DB \ FieldToConvert (
110
+ \ Magento \ Framework \ DB \ DataConverter \ SerializedToJson::class,
71
111
$ setup ->getTable ('salesrule ' ),
72
112
$ metadata ->getLinkField (),
73
113
'conditions_serialized '
74
114
),
75
- new FieldToConvert (
76
- SerializedToJson::class,
115
+ new \ Magento \ Framework \ DB \ FieldToConvert (
116
+ \ Magento \ Framework \ DB \ DataConverter \ SerializedToJson::class,
77
117
$ setup ->getTable ('salesrule ' ),
78
118
$ metadata ->getLinkField (),
79
119
'actions_serialized '
@@ -82,4 +122,29 @@ public function convertSerializedDataToJson($setup)
82
122
$ setup ->getConnection ()
83
123
);
84
124
}
125
+
126
+ /**
127
+ * Fills blank table salesrule_product_attribute with data.
128
+ *
129
+ * @return void
130
+ */
131
+ public function fillSalesRuleProductAttributeTable ()
132
+ {
133
+ /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $ruleCollection */
134
+ $ ruleCollection = $ this ->ruleColletionFactory ->create ();
135
+ /** @var \Magento\SalesRule\Model\Rule $rule */
136
+ foreach ($ ruleCollection as $ rule ) {
137
+ // Save product attributes used in rule
138
+ $ conditions = $ rule ->getConditions ()->asArray ();
139
+ $ actions = $ rule ->getActions ()->asArray ();
140
+ $ serializedConditions = $ this ->serializer ->serialize ($ conditions );
141
+ $ serializedActions = $ this ->serializer ->serialize ($ actions );
142
+ $ conditionAttributes = $ this ->resourceModelRule ->getProductAttributes ($ serializedConditions );
143
+ $ actionAttributes = $ this ->resourceModelRule ->getProductAttributes ($ serializedActions );
144
+ $ ruleProductAttributes = array_merge ($ conditionAttributes , $ actionAttributes );
145
+ if ($ ruleProductAttributes ) {
146
+ $ this ->resourceModelRule ->setActualProductAttributes ($ rule , $ ruleProductAttributes );
147
+ }
148
+ }
149
+ }
85
150
}
0 commit comments