UpdateManufacturerAttribute.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\ConfigurableProduct\Setup\Patch\Data;
  8. use Magento\Eav\Setup\EavSetup;
  9. use Magento\Eav\Setup\EavSetupFactory;
  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. * Update manufacturer attribute if it's presented in system.
  16. */
  17. class UpdateManufacturerAttribute implements DataPatchInterface, PatchVersionInterface
  18. {
  19. /**
  20. * @var ModuleDataSetupInterface
  21. */
  22. private $moduleDataSetup;
  23. /**
  24. * @var EavSetupFactory
  25. */
  26. private $eavSetupFactory;
  27. /**
  28. * @param ModuleDataSetupInterface $moduleDataSetup
  29. * @param EavSetupFactory $eavSetupFactory
  30. */
  31. public function __construct(
  32. ModuleDataSetupInterface $moduleDataSetup,
  33. EavSetupFactory $eavSetupFactory
  34. ) {
  35. $this->moduleDataSetup = $moduleDataSetup;
  36. $this->eavSetupFactory = $eavSetupFactory;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function apply()
  42. {
  43. /** @var EavSetup $eavSetup */
  44. $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
  45. if ($manufacturer = $eavSetup->getAttribute(
  46. \Magento\Catalog\Model\Product::ENTITY,
  47. 'manufacturer',
  48. 'apply_to'
  49. )) {
  50. $relatedProductTypes = explode(
  51. ',',
  52. $manufacturer
  53. );
  54. if (!in_array(Configurable::TYPE_CODE, $relatedProductTypes)) {
  55. $relatedProductTypes[] = Configurable::TYPE_CODE;
  56. $eavSetup->updateAttribute(
  57. \Magento\Catalog\Model\Product::ENTITY,
  58. 'manufacturer',
  59. 'apply_to',
  60. implode(',', $relatedProductTypes)
  61. );
  62. }
  63. }
  64. }
  65. /**
  66. * @inheritdoc
  67. */
  68. public static function getDependencies()
  69. {
  70. return [
  71. InstallInitialConfigurableAttributes::class,
  72. ];
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public static function getVersion()
  78. {
  79. return '2.2.1';
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public function getAliases()
  85. {
  86. return [];
  87. }
  88. }