InstallInitialConfigurableAttributes.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Setup\Patch\Data;
  7. use Magento\Eav\Setup\EavSetup;
  8. use Magento\Eav\Setup\EavSetupFactory;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\Setup\ModuleDataSetupInterface;
  11. use Magento\Framework\Setup\Patch\DataPatchInterface;
  12. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  13. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  14. /**
  15. * Class InstallInitialConfigurableAttributes
  16. * @package Magento\ConfigurableProduct\Setup\Patch
  17. */
  18. class InstallInitialConfigurableAttributes implements DataPatchInterface, PatchVersionInterface
  19. {
  20. /**
  21. * @var ModuleDataSetupInterface
  22. */
  23. private $moduleDataSetup;
  24. /**
  25. * @var EavSetupFactory
  26. */
  27. private $eavSetupFactory;
  28. /**
  29. * InstallInitialConfigurableAttributes constructor.
  30. * @param ModuleDataSetupInterface $moduleDataSetup
  31. * @param EavSetupFactory $eavSetupFactory
  32. */
  33. public function __construct(
  34. ModuleDataSetupInterface $moduleDataSetup,
  35. EavSetupFactory $eavSetupFactory
  36. ) {
  37. $this->moduleDataSetup = $moduleDataSetup;
  38. $this->eavSetupFactory = $eavSetupFactory;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function apply()
  44. {
  45. /** @var EavSetup $eavSetup */
  46. $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
  47. $attributes = [
  48. 'country_of_manufacture',
  49. 'manufacturer',
  50. 'minimal_price',
  51. 'msrp',
  52. 'msrp_display_actual_price_type',
  53. 'price',
  54. 'special_price',
  55. 'special_from_date',
  56. 'special_to_date',
  57. 'tier_price',
  58. 'weight',
  59. 'color'
  60. ];
  61. foreach ($attributes as $attributeCode) {
  62. $relatedProductTypes = explode(
  63. ',',
  64. $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, 'apply_to')
  65. );
  66. if (!in_array(Configurable::TYPE_CODE, $relatedProductTypes)) {
  67. $relatedProductTypes[] = Configurable::TYPE_CODE;
  68. $eavSetup->updateAttribute(
  69. \Magento\Catalog\Model\Product::ENTITY,
  70. $attributeCode,
  71. 'apply_to',
  72. implode(',', $relatedProductTypes)
  73. );
  74. }
  75. }
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public static function getDependencies()
  81. {
  82. return [];
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public static function getVersion()
  88. {
  89. return '2.0.0';
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getAliases()
  95. {
  96. return [];
  97. }
  98. }