Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace SelfPortrait\Migrations\Setup\Patch\Data;
- use Magento\Eav\Api\Data\AttributeOptionInterfaceFactory;
- use Magento\Eav\Model\AttributeSetManagement;
- use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;
- use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
- use Magento\Eav\Setup\EavSetupFactory;
- use Magento\Eav\Setup\EavSetup;
- use Magento\Framework\Setup\Patch\DataPatchInterface;
- use Magento\Framework\Setup\ModuleDataSetupInterface;
- use Magento\Catalog\Model\Product;
- use Magento\Catalog\Model\Product\Attribute\OptionManagement;
- use Magento\Framework\ObjectManagerInterface;
- class AddDressComposition implements DataPatchInterface
- {
- private $moduleDataSetup;
- private $eavSetupFactory;
- private $attributeSetManagement;
- private $attributeSetCollection;
- private $optionManagement;
- private $objectManager;
- private $optionFactory;
- /**
- * AddDressComposition constructor.
- * @param ModuleDataSetupInterface $moduleDataSetup
- * @param AttributeSetManagement $attributeSetManagement
- * @param CollectionFactory $attributeSetCollection
- * @param EavSetupFactory $eavSetupFactory
- * @param OptionManagement $optionManagement
- * @param AttributeOptionInterfaceFactory $optionFactory
- * @param ObjectManagerInterface $objectManager
- */
- public function __construct(
- ModuleDataSetupInterface $moduleDataSetup,
- AttributeSetManagement $attributeSetManagement,
- CollectionFactory $attributeSetCollection,
- EavSetupFactory $eavSetupFactory,
- OptionManagement $optionManagement,
- AttributeOptionInterfaceFactory $optionFactory,
- ObjectManagerInterface $objectManager
- ) {
- $this->moduleDataSetup = $moduleDataSetup;
- $this->eavSetupFactory = $eavSetupFactory;
- $this->attributeSetCollection = $attributeSetCollection;
- $this->attributeSetManagement = $attributeSetManagement;
- $this->optionManagement = $optionManagement;
- $this->optionFactory = $optionFactory;
- $this->objectManager = $objectManager;
- }
- /**
- * {@inheritdoc}
- */
- public function apply()
- {
- $this->moduleDataSetup->getConnection()->startSetup();
- /** @var EavSetup $eavSetup */
- $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
- /**
- * Install eav entity types to the eav/entity_type table
- */
- $eavSetup->removeAttribute('catalog_product', 'dress_composition');
- $eavSetup->addAttribute(
- 'catalog_product',
- 'dress_composition',
- [
- 'type' => 'text',
- 'label' => 'Composition',
- 'input' => 'textarea',
- 'user_defined' => true,
- 'used_in_product_listing' => true,
- 'required' => false,
- 'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
- ]
- );
- //add attribute to group Dress
- $entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY);
- $attributeId = $eavSetup->getAttributeId($entityTypeId, 'dress_composition');
- $attributeSet = $this->attributeSetCollection->create()
- ->addFieldToSelect('*')
- ->addFieldToFilter(
- 'attribute_set_name',
- array(
- 'eq' => 'Ready To Wear'
- )
- );
- $attributeSetId = $attributeSet->getFirstItem()->getData('attribute_set_id');
- $attributeGroupId = (int)$eavSetup->getAttributeGroupByCode(
- Product::ENTITY,
- $attributeSetId,
- 'Dress',
- 'attribute_group_id'
- );
- $eavSetup->addAttributeToGroup(Product::ENTITY, $attributeSetId, $attributeGroupId, $attributeId);
- $this->moduleDataSetup->getConnection()->endSetup();
- }
- /**
- * {@inheritdoc}
- */
- public static function getDependencies()
- {
- return [
- AddReadyToWearAttributeSet::class
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function getAliases()
- {
- return [];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement