Skip to content

Instantly share code, notes, and snippets.

@skmezanul
Forked from bh-ref/RemoveOptions.php
Created December 9, 2020 20:28
Show Gist options
  • Save skmezanul/77732878803d7771626bf77921ac0d08 to your computer and use it in GitHub Desktop.
Save skmezanul/77732878803d7771626bf77921ac0d08 to your computer and use it in GitHub Desktop.

Revisions

  1. @bh-ref bh-ref revised this gist Mar 11, 2016. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions RemoveOptions.php
    Original 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 */
  2. @bh-ref bh-ref revised this gist Mar 11, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions RemoveOptions.php
    Original 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
    // 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();
  3. @bh-ref bh-ref created this gist Mar 11, 2016.
    69 changes: 69 additions & 0 deletions RemoveOptions.php
    Original 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);
    }
    }