I have a working module with CRUD in admin, now I want to add a custom attribute for products, but options of that attribute should be a values from existing DB table, but every tutorial on the web is teaching how to hardcode those attribute options, so how can I do that?
Thanks everyone!
I have found in Magento 2 documentation, how to create a custom attribute, created InstallData with the attribute, but need to correct the provided code of the Source model, so that will get values from my DB table
namespace Learning\ClothingMaterial\Model\Attribute\Source;
class Material extends
\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* Get all options
* @return array
*/
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = [
['label' => __('Cotton'), 'value' => 'cotton'],
['label' => __('Leather'), 'value' => 'leather'],
['label' => __('Silk'), 'value' => 'silk'],
['label' => __('Denim'), 'value' => 'denim'],
['label' => __('Fur'), 'value' => 'fur'],
['label' => __('Wool'), 'value' => 'wool'],
];
}
return $this->_options;
}
}