Magento – Magento 2 – How to add custom values from custom table to product custom attribute

custom-attributesmagento2.2.2

I want to get the locations from custom_location table values to custom product attribute multi-select box.

custom_location values like:

1 – location_1

2 – location_2

3 – location_3

4 – location_4

How can i get these values to magento 2 product attribute multiple select box?Thanks for helping

Best Answer

You need to create model for custom_table(Ignore if already made) by following link - https://www.mage-world.com/blog/how-to-use-model-and-collection-in-magento-2.html

Now make new mmodule say: Vendor_Module

Create InstallData.php under app/code/Vendor/Module/Setup/InstallData.php and copy below code & make changes as per your naming convention:-

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;

class InstallData implements InstallDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * @var Config
     */
    private $eavConfig;

    /**
     * @param EavSetupFactory $setup
     * @param Config $context
     */
    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }

    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

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

Create Options.php under Vendor/Module/Model/Config/Source and paste Below Code & make changes as per your naming convention:

<?php 
namespace Vendor\Module\Model\Config\Source;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{ 

    /**
     * @var \Vendor\Module\Model\CustomFactory
     */
    private $customFactory;

    /**
     * @param \Magento\Customer\Model\CustomerFactory $customerFactory
     */
    public function __construct(\Vendor\Module\Model\CustomFactory $customFactory)
    {
        $this->customFactory = $customFactory;
    }

    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        $customCollection = $this->customFactory->create()->getCollection();
        $this->_options = [['label'=>'Please select', 'value'=>'']];
        foreach($customCollection as $custom)
        {
            $this->_options[] = ['label'=> $custom->getFieldName(), 'value' => $custom->getId()];
        }
        return $this->_options;
    }

    /**
     * Get a text for option value
     *
     * @param string|integer $value
     * @return string|bool
     */
    public function getOptionText($value)
    {
        foreach ($this->getAllOptions() as $option) {
            if ($option['value'] == $value) {
                return $option['label'];
            }
        }
        return false;
    }
}
Related Topic