FillSalesRuleProductAttributeTable.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Setup\Patch\Data;
  7. use Magento\Framework\App\State;
  8. use Magento\Framework\Setup\Patch\DataPatchInterface;
  9. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  10. /**
  11. * Class FillSalesRuleProductAttributeTable
  12. *
  13. * @package Magento\SalesRule\Setup\Patch
  14. */
  15. class FillSalesRuleProductAttributeTable implements DataPatchInterface, PatchVersionInterface
  16. {
  17. /**
  18. * @param \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $ruleColletionFactory
  19. */
  20. private $ruleColletionFactory;
  21. /**
  22. * @param \Magento\Framework\Serialize\SerializerInterface $serializer
  23. */
  24. private $serializer;
  25. /**
  26. * @param \Magento\SalesRule\Model\ResourceModel\Rule $resourceModelRule
  27. */
  28. private $resourceModelRule;
  29. /**
  30. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  31. */
  32. private $moduleDataSetup;
  33. /**
  34. * @var State
  35. */
  36. private $appState;
  37. /**
  38. * FillSalesRuleProductAttributeTable constructor.
  39. * @param \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $ruleColletionFactory
  40. * @param \Magento\Framework\Serialize\SerializerInterface $serializer
  41. * @param \Magento\SalesRule\Model\ResourceModel\Rule $resourceModelRule
  42. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  43. * @param State $appState
  44. */
  45. public function __construct(
  46. \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $ruleColletionFactory,
  47. \Magento\Framework\Serialize\SerializerInterface $serializer,
  48. \Magento\SalesRule\Model\ResourceModel\Rule $resourceModelRule,
  49. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  50. State $appState
  51. ) {
  52. $this->ruleColletionFactory = $ruleColletionFactory;
  53. $this->serializer = $serializer;
  54. $this->resourceModelRule = $resourceModelRule;
  55. $this->moduleDataSetup = $moduleDataSetup;
  56. $this->appState = $appState;
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function apply()
  62. {
  63. $this->moduleDataSetup->getConnection()->startSetup();
  64. $this->appState->emulateAreaCode(
  65. \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
  66. [$this, 'fillSalesRuleProductAttributeTable']
  67. );
  68. $this->fillSalesRuleProductAttributeTable();
  69. $this->moduleDataSetup->getConnection()->endSetup();
  70. }
  71. /**
  72. * Fill attribute table for sales rule
  73. */
  74. public function fillSalesRuleProductAttributeTable()
  75. {
  76. /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $ruleCollection */
  77. $ruleCollection = $this->ruleColletionFactory->create();
  78. /** @var \Magento\SalesRule\Model\Rule $rule */
  79. foreach ($ruleCollection as $rule) {
  80. // Save product attributes used in rule
  81. $conditions = $rule->getConditions()->asArray();
  82. $actions = $rule->getActions()->asArray();
  83. $serializedConditions = $this->serializer->serialize($conditions);
  84. $serializedActions = $this->serializer->serialize($actions);
  85. $conditionAttributes = $this->resourceModelRule->getProductAttributes($serializedConditions);
  86. $actionAttributes = $this->resourceModelRule->getProductAttributes($serializedActions);
  87. $ruleProductAttributes = array_merge($conditionAttributes, $actionAttributes);
  88. if ($ruleProductAttributes) {
  89. $this->resourceModelRule->setActualProductAttributes($rule, $ruleProductAttributes);
  90. }
  91. }
  92. }
  93. /**
  94. * @inheritdoc
  95. */
  96. public static function getDependencies()
  97. {
  98. return [
  99. ConvertSerializedDataToJson::class
  100. ];
  101. }
  102. /**
  103. * @inheritdoc
  104. */
  105. public static function getVersion()
  106. {
  107. return '2.0.3';
  108. }
  109. /**
  110. * @inheritdoc
  111. */
  112. public function getAliases()
  113. {
  114. return [];
  115. }
  116. }