Skip to content

Instantly share code, notes, and snippets.

@GideonBabu
Last active July 5, 2018 12:37
Show Gist options
  • Save GideonBabu/87e3244779103cbc1608349d6dd03295 to your computer and use it in GitHub Desktop.
Save GideonBabu/87e3244779103cbc1608349d6dd03295 to your computer and use it in GitHub Desktop.
Magento 2 remove attribute options/values
<?php
namespace Gideon\Returns\Setup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use \Magento\Framework\App\ObjectManager;
class UpgradeData implements UpgradeDataInterface
{
private $eavSetupFactory;
private $objectManager;
public function __construct(EavSetupFactory $eavSetupFactory,
ObjectManager $objectManager)
{
$this->eavSetupFactory = $eavSetupFactory;
$this->objectManager = $objectManager;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if ($context->getVersion() && version_compare($context->getVersion(), '1.0.1') < 0) {
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'is_cat_returnable',
[
'type' => 'int',
'label' => 'Enable RMA',
'input' => 'select',
'required' => false,
'sort_order' => 10,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'group' => 'General Information',
],
'cat_returnable_days',
[
'type' => 'int',
'label' => 'Returnable in Days',
'input' => 'text',
'required' => false,
'sort_order' => 20,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'returnable_days',
[
'group' => 'general',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Returnable in Days',
'input' => 'text',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => '',
'note' => 'Enter the maximum days a product can have return. For example, 10'
]
);
}
if ($context->getVersion() && version_compare($context->getVersion(), '1.0.2') < 0) {
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Category::ENTITY,
'cat_returnable_days',
[
'type' => 'int',
'label' => 'Returnable in Days',
'input' => 'text',
'required' => false,
'sort_order' => 20,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
]
);
}
// remove reason for return attribute options
if ($context->getVersion() && version_compare($context->getVersion(), '1.0.3') < 0) {
$objectManager = $this->objectManager::getInstance();
$eavAttribute = $objectManager->create('Magento\Eav\Model\Config');
/** @var \Magento\Eav\Model\Config $attribute */
$attribute = $eavAttribute->getAttribute('rma_item', 'reason');
//$attributeId = $eavAttribute->getAttributeId('rma_item', 'reason'); // to get attribute ID
/* to add new options
$options = [
'values' => [
'sort_order1' => 'title1',
'sort_order2' => 'title2',
'sort_order3' => 'title3',
],
'attribute_id' => 'some_id',
];
*/
$options = $attribute->getSource()->getAllOptions();
foreach ($options as $optionId => $value) {
$options['value'][$optionId] = true;
$options['delete'][$optionId] = true;
}
$setupObject = $objectManager->create('Magento\Eav\Setup\EavSetup');
$setupObject->addAttributeOption($options);
}
$setup->endSetup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment