-
-
Save skmezanul/77732878803d7771626bf77921ac0d08 to your computer and use it in GitHub Desktop.
Revisions
-
bh-ref revised this gist
Mar 11, 2016 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,6 +37,11 @@ public function __construct( $this->attributeFactory = $attributeFactory; } /** * delete options from an attribute * * @param ModuleDataSetupInterface $setup */ public function deleteOptions(ModuleDataSetupInterface $setup) { /** @var \Magento\Eav\Setup\EavSetup $eavSetup */ -
bh-ref revised this gist
Mar 11, 2016 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -47,10 +47,10 @@ public function deleteOptions(ModuleDataSetupInterface $setup) $attributeCode = 'my_already_existing_attribute'; // IMPORTANT: // use $this->attributeFactory->create() before loading the attribute, // or else the options you want to delete will be cached and you cannot // delete other options from a second attribute in the same request $attribute = $this->attributeFactory->create()->loadByCode($entityTypeId, $attributeCode); $options = $attribute->getOptions(); -
bh-ref created this gist
Mar 11, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ <?php namespace Vendor\ModuleName\Options; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory; class RemoveOptions { /** * EAV setup factory * * @var EavSetupFactory */ protected $eavSetupFactory; /** * EAV product attribute factory * * @var AttributeFactory */ protected $attributeFactory; /** * Init * * @param EavSetupFactory $eavSetupFactory * @param AttributeFactory $attributeFactory */ public function __construct( EavSetupFactory $eavSetupFactory, AttributeFactory $attributeFactory ) { $this->eavSetupFactory = $eavSetupFactory; $this->attributeFactory = $attributeFactory; } public function deleteOptions(ModuleDataSetupInterface $setup) { /** @var \Magento\Eav\Setup\EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE); $attributeCode = 'my_already_existing_attribute'; // IMPORTANT: // use $this->attributeFactory->create() before loading the attribute, // or else the options you want to delete will be cached and you cannot // delete other options from a second attribute in the same request $attribute = $this->attributeFactory->create()->loadByCode($entityTypeId, $attributeCode); $options = $attribute->getOptions(); $optionsToRemove = []; foreach($options as $option) { if ($option['value']) { $optionsToRemove['delete'][$option['value']] = true; $optionsToRemove['value'][$option['value']] = true; } } $eavSetup->addAttributeOption($optionsToRemove); } }