0

I created a custom product attribute using installer script. Attribute was created successfully and i can view it in product adding page in admin.

If i save the product the attribute value is not saving.

My Steps are..

<?php
namespace Emedia\ProductAttribute\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;

class InstallData implements InstallDataInterface
{ 
private $eavSetupFactory;


public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    if (version_compare($context->getVersion(), '2.0.0') < 0){

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'location_id',/* Custom Attribute Code */
        [
            'group' => 'General',/* Group name in which you want 
                                          to display your custom attribute */
            'type' => 'int',/* Data type in which formate your value save in database*/
            'backend' => '',
            'frontend' => '',
            'label' => 'Location', /* lablel of your attribute*/
            'input' => 'multiselect',
            'class' => '',
            'source' => 'FME\GoogleMapsStoreLocator\Model\Config\Source\Options',
                            /* Source of your select type custom attribute options*/
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                                /*Scope of your attribute */
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => true,
            'used_in_product_listing' => true,
            'unique' => false
        ]
    );
    }
}
}

My resource file Options.php

<?php
namespace FME\GoogleMapsStoreLocator\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
{

protected $optionFactory;
protected $collectionFactory;


public function __construct(OptionFactory $optionFactory,        
    \FME\GoogleMapsStoreLocator\Model\ResourceModel\Storelocator\CollectionFactory $collectionFactory)
{
    $this->optionFactory = $optionFactory;  
    $this->collectionFactory = $collectionFactory;
    //you can use this if you want to prepare options dynamically  
}

/**
 * Get all options
 *
 * @return array
 */
public function getAllOptions()
{
     $collection = $this->collectionFactory->create()->addFieldToFilter('is_active', 1)
    ->setOrder('creation_time', 'DESC');
    $this->_options = [['label'=>'Please select', 'value'=>'']];
    foreach($collection as $custom)
    {
        $this->_options[] = ['label'=> $custom->getStoreName(), 'value' => $custom->getGmapsId()];
    }
    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,
            'comment' => 'Custom Attribute Options  ' . $attributeCode . ' column',
        ],
    ];
}
}

After upgrade command i can see the attributes in magento 2 admin back end product adding form too like this

enter image description here

If i save the product the location is not save in back end and not view in edit form.

What steps i skip, can any one help me on this?

0

Here i have missed the backend model. this answer may help some one in future.

I change my install script like below

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'location_id',/* Custom Attribute Code */
    [
        'group' => 'General',/* Group name in which you want 
                                      to display your custom attribute */
        'type' => 'int',/* Data type in which formate your value save in database*/
        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
        'frontend' => '',
        'label' => 'Location', /* lablel of your attribute*/
        'input' => 'multiselect',
        'class' => '',
        'source' => 'FME\GoogleMapsStoreLocator\Model\Config\Source\Options',
                        /* Source of your select type custom attribute options*/
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                            /*Scope of your attribute */
        'visible' => true,
        'required' => false,
        'user_defined' => false,
        'default' => '',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => true,
        'used_in_product_listing' => true,
        'unique' => false
    ]
);

only changed this line

'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',

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.