4

How to give the option value for the product attribute programmatically.

I need to give the option value for manufacturer attribute drop down field.

I have created admin grid for manufacturer details.

I need to get the option value of manufacturer attribute is taken from custom admin grid.

How to do this.

2 Answers 2

2

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);
    }
}
5
  • But , Whenever i have change the custom manufacturer table name . it should be reflected. It is possible?
    – Mahi M
    Feb 27, 2019 at 4:14
  • Obviously, Magento links attribute option by their ID. You can change the attribute option label any time. Feb 27, 2019 at 4:31
  • without run setup:upgrade command?
    – Mahi M
    Feb 27, 2019 at 4:43
  • Yes, you don't need to perform any DB operation or any Magento CLI commands. Feb 27, 2019 at 4:57
  • why we use getFlatColumns(), getFlatUpdateSelect() function in upgrade data
    – Mahi M
    Feb 27, 2019 at 5:01
2

Update your Manufacturer attribute by writing custom UpgradeSchema in your module and assign a custom source model which render all options form Manufacturer collection.

Write custom source model class MyCompany\Manufacturer\Model\Attribute\Source\Manufacturer.php

<?php
namespace MyCompany\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(
        \MyCompany\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->getManufacturerName(),
                    'value' =>  $manufacturer->getId()
                ];
            }
        }
        return $this->_options;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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