Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SelfPortrait\Migrations\Setup\Patch\Data;
  4.  
  5. use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
  6. use Magento\Eav\Model\AttributeSetManagement;
  7. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;
  8. use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
  9. use Magento\Eav\Setup\EavSetupFactory;
  10. use Magento\Eav\Setup\EavSetup;
  11. use Magento\Framework\Setup\Patch\DataPatchInterface;
  12. use Magento\Framework\Setup\ModuleDataSetupInterface;
  13. use Magento\Catalog\Model\Product;
  14. use Magento\Catalog\Model\Product\Attribute\OptionManagement;
  15. use Magento\Framework\ObjectManagerInterface;
  16.  
  17. class AddDressComposition implements DataPatchInterface
  18. {
  19. private $moduleDataSetup;
  20.  
  21. private $eavSetupFactory;
  22.  
  23. private $attributeSetManagement;
  24.  
  25. private $attributeSetCollection;
  26.  
  27. private $optionManagement;
  28.  
  29. private $objectManager;
  30.  
  31. private $optionFactory;
  32.  
  33.  
  34. /**
  35. * AddDressComposition constructor.
  36. * @param ModuleDataSetupInterface $moduleDataSetup
  37. * @param AttributeSetManagement $attributeSetManagement
  38. * @param CollectionFactory $attributeSetCollection
  39. * @param EavSetupFactory $eavSetupFactory
  40. * @param OptionManagement $optionManagement
  41. * @param AttributeOptionInterfaceFactory $optionFactory
  42. * @param ObjectManagerInterface $objectManager
  43. */
  44. public function __construct(
  45. ModuleDataSetupInterface $moduleDataSetup,
  46. AttributeSetManagement $attributeSetManagement,
  47. CollectionFactory $attributeSetCollection,
  48. EavSetupFactory $eavSetupFactory,
  49. OptionManagement $optionManagement,
  50. AttributeOptionInterfaceFactory $optionFactory,
  51. ObjectManagerInterface $objectManager
  52. ) {
  53. $this->moduleDataSetup = $moduleDataSetup;
  54. $this->eavSetupFactory = $eavSetupFactory;
  55. $this->attributeSetCollection = $attributeSetCollection;
  56. $this->attributeSetManagement = $attributeSetManagement;
  57. $this->optionManagement = $optionManagement;
  58. $this->optionFactory = $optionFactory;
  59. $this->objectManager = $objectManager;
  60. }
  61.  
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function apply()
  66. {
  67. $this->moduleDataSetup->getConnection()->startSetup();
  68. /** @var EavSetup $eavSetup */
  69. $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
  70.  
  71. /**
  72. * Install eav entity types to the eav/entity_type table
  73. */
  74. $eavSetup->removeAttribute('catalog_product', 'dress_composition');
  75. $eavSetup->addAttribute(
  76. 'catalog_product',
  77. 'dress_composition',
  78. [
  79. 'type' => 'text',
  80. 'label' => 'Composition',
  81. 'input' => 'textarea',
  82. 'user_defined' => true,
  83. 'used_in_product_listing' => true,
  84. 'required' => false,
  85. 'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
  86. ]
  87. );
  88.  
  89. //add attribute to group Dress
  90. $entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY);
  91. $attributeId = $eavSetup->getAttributeId($entityTypeId, 'dress_composition');
  92. $attributeSet = $this->attributeSetCollection->create()
  93. ->addFieldToSelect('*')
  94. ->addFieldToFilter(
  95. 'attribute_set_name',
  96. array(
  97. 'eq' => 'Ready To Wear'
  98. )
  99. );
  100.  
  101. $attributeSetId = $attributeSet->getFirstItem()->getData('attribute_set_id');
  102. $attributeGroupId = (int)$eavSetup->getAttributeGroupByCode(
  103. Product::ENTITY,
  104. $attributeSetId,
  105. 'Dress',
  106. 'attribute_group_id'
  107. );
  108. $eavSetup->addAttributeToGroup(Product::ENTITY, $attributeSetId, $attributeGroupId, $attributeId);
  109. $this->moduleDataSetup->getConnection()->endSetup();
  110. }
  111.  
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public static function getDependencies()
  116. {
  117. return [
  118. AddReadyToWearAttributeSet::class
  119. ];
  120. }
  121.  
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function getAliases()
  126. {
  127. return [];
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement