0

I have the following setup file in my custom module on Vendor\Module\Setup\InstallData.php:

<?php

namespace Vendor\Module\Setup;

use \Magento\Catalog\Model\Product;
use \Magento\Eav\Setup\EavSetupFactory;
use \Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use \Magento\Framework\Setup\InstallDataInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    protected $setup;

    public function __construct(
        AttributeSetFactory $attributeSetFactory,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->attributeSetFactory  = $attributeSetFactory;
        $this->setup = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $new_product_attrs = [
            "field_1" => [
                "label" => "Field 1",
                "type" => "int",
                "input" => "select",
                "source" => "\Vendor\Module\Model\Config\Source\Option"
            ],
            "field_2 " => [
                "label" => "Field 2",
                "type" => "int",
                "input" => "text"
            ],
        ];
        //Create product attributes
        foreach ($new_product_attrs as $code => $attr){
            $this->createProductAttribute($setup, $code, $attr);
        }
        $installer->endSetup();
    }

    private function createProductAttribute($setup, $attr_code, $attr_details){
        $setup = $this->setup->create(['setup' => $setup]);

        $attr = [
            "type"     => $attr_details["type"],
            'group' => 'Product Details',
            "backend"  => "",
            "label"    => $attr_details["label"],
            "input"    => $attr_details["input"],
            "source"   => "",
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            "visible"  => true,
            "required" => false,
            "default"  => "",
            "frontend" => "",
            'user_defined' => true,
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
        ];
        if (isset($attr_details["backend"])){
            $attr['backend'] = $attr_details["backend"];
        }
        if (isset($attr_details["source"])){
            $attr['source'] = $attr_details["source"];
        }
        if (isset($attr_details["option_values"])){
            $attr['option'] = [
                'values' => $attr_details["option_values"]
            ];
        }
        $setup->addAttribute(Product::ENTITY, $attr_code, $attr);
    }
}

And for Vendor\Module\Model\Config\Source\Option.php:

<?php

namespace Vendor\Module\Model\Config\Source;

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

class Option extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
    * Get all options
    *
    * @return array
    */
    public function getAllOptions()
    {
        $this->_options=[
            ['label' => '--Select Options--', 'value' => ''],
        ];
        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;
    }

    /**
     * Retrieve flat column definition
     *
     * @return array
     */
    public function getFlatColumns()
    {
        $attributeCode = $this->getAttribute()->getAttributeCode();
        return [
            $attributeCode => [
                'unsigned' => false,
                'default' => null,
                'extra' => null,
                'type' => Table::TYPE_INTEGER,
                'nullable' => true,
                'comment' => 'Custom Attribute Options  ' . $attributeCode . ' column',
            ],
        ];
    }
}

I can create the attribute successfully, but on field_1, I found there are no options available even I go to Store->Attribute->Product and create new options for field_1. Which step I've done wrong? I don't mind drop the attribute and create that attribute again as the website is still testing.

0

I can see "source" => "", is empty on $arr=[] even though you have source model in $new_product_attrs so I think this is creaitng Issue just try to pass source model If you are getting in $attr_details

  • The source will be set if $new_product_attrs contains "source". Obviously field_1 contains "source" – PY Yick Mar 26 '18 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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