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;
}
}
Best Answer
To remove the attrbute add below code in your module
UpgradeData.php
You will need to add source model to render data from custom model here's the sample code to add dropdown attribute:
Create
Vendor/Module/Model/Config/Source/Options.php
file and add below code:Hope This Help!