Skip to content

Commit 32a9f93

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-67092' into 2.2-develop-pr10
2 parents 7f87458 + 849d1e3 commit 32a9f93

File tree

2 files changed

+94
-29
lines changed

2 files changed

+94
-29
lines changed

app/code/Magento/SalesRule/Setup/UpgradeData.php

Lines changed: 93 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,117 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\SalesRule\Setup;
76

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;
168

17-
class UpgradeData implements UpgradeDataInterface
9+
/**
10+
* Class \Magento\SalesRule\Setup\UpgradeData
11+
*/
12+
class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
1813
{
1914
/**
20-
* @var MetadataPool
15+
* @var \Magento\Framework\EntityManager\MetadataPool
2116
*/
2217
private $metadataPool;
2318

2419
/**
25-
* @var AggregatedFieldDataConverter
20+
* @var \Magento\Framework\DB\AggregatedFieldDataConverter
2621
*/
2722
private $aggregatedFieldConverter;
2823

2924
/**
30-
* UpgradeData constructor.
25+
* Resource Model of sales rule.
3126
*
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
3459
*/
3560
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
3867
) {
3968
$this->aggregatedFieldConverter = $aggregatedFieldConverter;
4069
$this->metadataPool = $metadataPool;
70+
$this->resourceModelRule = $resourceModelRule;
71+
$this->serializer = $serializer;
72+
$this->state = $state;
73+
$this->ruleColletionFactory = $ruleColletionFactory;
4174
}
4275

4376
/**
4477
* @inheritdoc
4578
*/
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+
) {
4883
$setup->startSetup();
49-
5084
if (version_compare($context->getVersion(), '2.0.2', '<')) {
5185
$this->convertSerializedDataToJson($setup);
5286
}
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+
}
5495
$setup->endSetup();
5596
}
5697

5798
/**
5899
* Convert metadata from serialized to JSON format:
59100
*
60-
* @param ModuleDataSetupInterface $setup
61-
*
101+
* @param \Magento\Framework\Setup\ModuleDataSetupInterface $setup *
62102
* @return void
63103
*/
64104
public function convertSerializedDataToJson($setup)
65105
{
66-
$metadata = $this->metadataPool->getMetadata(RuleInterface::class);
106+
$metadata = $this->metadataPool->getMetadata(\Magento\SalesRule\Api\Data\RuleInterface::class);
67107
$this->aggregatedFieldConverter->convert(
68108
[
69-
new FieldToConvert(
70-
SerializedToJson::class,
109+
new \Magento\Framework\DB\FieldToConvert(
110+
\Magento\Framework\DB\DataConverter\SerializedToJson::class,
71111
$setup->getTable('salesrule'),
72112
$metadata->getLinkField(),
73113
'conditions_serialized'
74114
),
75-
new FieldToConvert(
76-
SerializedToJson::class,
115+
new \Magento\Framework\DB\FieldToConvert(
116+
\Magento\Framework\DB\DataConverter\SerializedToJson::class,
77117
$setup->getTable('salesrule'),
78118
$metadata->getLinkField(),
79119
'actions_serialized'
@@ -82,4 +122,29 @@ public function convertSerializedDataToJson($setup)
82122
$setup->getConnection()
83123
);
84124
}
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+
}
85150
}

app/code/Magento/SalesRule/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_SalesRule" setup_version="2.0.2">
9+
<module name="Magento_SalesRule" setup_version="2.0.3">
1010
<sequence>
1111
<module name="Magento_Rule"/>
1212
<module name="Magento_Catalog"/>

0 commit comments

Comments
 (0)