0

I need to delete the existing attribute called Manufacturer if this attribute exists.

And then Create a new attribute called Manufacturer with the same properties as that of the existing one.its created programatically while installing the module.

And how to give the option value for this attribute from custom table .

How to do this in magento2.

Thanks.

Update:

<?php
namespace XXX\Manufacturer\Model\Config\Source;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    public $manufacturer;
    public function __construct(
        \XXX\Manufacturer\Model\ResourceModel\Manufacturer\Collection $manufacturer
    ) {
        $this->manufacturer = $manufacturer;
    }

    public function getAllOptions()
    {

        // $this->_options = [ ['label' => __('Test'), 'value'=>'0'] ];
        if (null === $this->_options) {
            foreach($this->manufacturer as $manufacturer) {
                $this->_options[] = [
                    'label' => $manufacturer->getName(),
                    'value' =>  $manufacturer->getId()
                ];
            }
        }
        return $this->_options;
    }
}
2

To remove the attrbute add below code in your module UpgradeData.php

 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->removeAttribute(
 \Magento\Catalog\Model\Product::ENTITY,
 'your_attribute_code');

You will need to add source model to render data from custom model here's the sample code to add dropdown attribute:

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'your_attribute_code',
    [
        'group' => 'General',
        'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Product Attribute Label',
        'input' => 'boolean',
        'class' => '',
        'source' => 'Vendor\Module\Model\Config\Source\Options',
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => false,
        'default' => '1',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => false,
        'unique' => false,
        'apply_to' => ''
    ]
);

Create Vendor/Module/Model/Config/Source/Options.php file and add below code:

namespace Vendor\Module\Model\Config\Source;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    public $manufacturer;
    public function __construct(
        \Vendor\Module\Model\ResourceModel\Manufacturer\Collection $manufacturer
    ) {
        $this->manufacturer = $manufacturer;
    }

    public function getAllOptions()
    {

        // $this->_options = [ ['label' => __('Test'), 'value'=>'0'] ];
        $arr = [];
        if (null === $this->_options) {
            foreach($this->manufacturer as $manufacturer) {
                $arr[] = [
                    'label' => __($manufacturer->getName()),
                    'value' =>  $manufacturer->getId()
                ];
            }
            $this->_options = $arr;
        }
        return $this->_options;
    }
}

Hope This Help!

8
  • updated my post
    – Mahi M
    Feb 27 '19 at 10:56
  • @ManoM I have updated my answer please review. Please accept as answer so it will helpful to others. Feb 27 '19 at 11:10
  • It does not get option value from custom table ?
    – Mahi M
    Feb 27 '19 at 11:47
  • load your custom model in Options.php and set the array in $this->_options. Feb 27 '19 at 11:56
  • update my code .......
    – Mahi M
    Feb 27 '19 at 12:09
0

You can inject Magento\Eav\Model\Config object and use the getAttribute('catalog_product', $attributecode) function to check if your custom eav attribute with code $attributecode already exist or not.

Before Create Attribute Check Attribute Already Exist Or Not If Exit Then Remove Attribute

$attribute = $this->eavConfig->getAttribute($entityTypeCode, $attributeCode);
if (!$attribute || !$attribute->getAttributeId()) 
{
    $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'my_attribute');
}

$eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'your_attribute',
                [
                    'group' => 'General',
                    'type' => 'int',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Attribute_Label',
                    'input' => 'boolean',
                    'class' => '',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '1',
                    'apply_to' => ''
                ]
            );
1
  • updated my post
    – Mahi M
    Feb 27 '19 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.