I want delete the existing attribute called Manufacturer. Check if this attribute exists before deleting.
How to do this.
Thanks.
magento2.3php-7.2
I want delete the existing attribute called Manufacturer. Check if this attribute exists before deleting.
How to do this.
Thanks.
I guess you would like to render the Manufacturer options in the attribute dropdown from a custom Manufacturer table?
If yes, You have to write the UpgradeData schema for your module and update the existing Manufacturer source model to your custom source model class. Finally, Your source model class will look like:
<?php
namespace CompanyNamespace\Manufacturer\Model\Attribute\Source;
use Magento\Framework\DB\Ddl\Table;
class Manufacturer extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
public $manufacturer;
public function __construct(
\CompanyNamespace\Manufacturer\Model\ResourceModel\Manufacturer\Collection $manufacturer
) {
$this->manufacturer = $manufacturer;
}
public function getAllOptions()
{
if (null === $this->_options) {
foreach($this->manufacturer as $manufacturer) {
$this->_options[] = [
'label' => $manufacturer->getContactName(),
'value' => $manufacturer->getId()
];
}
}
return $this->_options;
}
public function getOptionText($value)
{
foreach ($this->getAllOptions() as $option) {
if ($option['value'] == $value) {
return $option['label'];
}
}
return false;
}
public function getFlatColumns()
{
$attributeCode = $this->getAttribute()->getAttributeCode();
return [
$attributeCode => [
'unsigned' => false,
'default' => null,
'extra' => null,
'type' => Table::TYPE_INTEGER,
'nullable' => true,
],
];
}
public function getFlatUpdateSelect($store)
{
return $this->optionFactory->create()->getFlatUpdateSelect($this->getAttribute(), $store, false);
}
}
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!
Best Answer
Try this,
In order to check and remove attribute
Complete code on InstallData.php
Hope this helps :)