0

Getting this error when reload page after adding value programmatically in a custom attribute. And when i delete that value which i added programmatically then its working fine.

Error filtering template: Notice: Undefined offset: 1 in 
/home/tunxxx/public_html/xxxxxx.com/app/design/frontend/Etheme/yourstore/Nwdthemes_Revslider/templates/revslider.phtml on line 87

i am using below given code for addding attribute value programatically.

<?php
//  $attribute_model = $this->_objectManager->create('Magento\Eav\Model\Entity\Attribute');

    $objectManager1 = \Magento\Framework\App\ObjectManager::getInstance();

    $attribute_model = $objectManager1->create('Magento\Eav\Model\Entity\Attribute');


    $attribute_code = $attribute_model->getIdByCode('catalog_product', 'ymmk_model');
    $attribute = $attribute_model->load($attribute_code);
    $value[0] = array('rrrrrrrrrfrrr','');
    $result = array('value' => $value);
    $attribute->setData('option',$result);
    $attribute->save();
?>
0

You probably want to use \Magento\Catalog\Api\ProductAttributeOptionManagementInterface::add

Keep in mind that Magento typically wants you to use dependency injection to instantiate your objects.

You should be using a InstallData class to insert those options. You should inject into your composer the ProductAttributeOptionManagementInterface and Magento\Eav\Model\Entity\Attribute\OptionFactory. It's a bad idea to do this kind of a thing in a template file...

This is what the install should look like (Replace \Vendor\Module with your vendor name and the module name):

<?php
// Location: <module_root_directory>/Setup/InstallData.php
namespace \Vendor\Module\Setup;

use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\OptionFactory;

class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    /**
     * @var OptionFactory|null
     */
    protected $optionFactory;

    /**
     * @var ProductAttributeOptionManagementInterface|null
     */
    protected $attributeOptionManagementInterface;

    /**
     * InstallData constructor.
     *
     * @param ProductAttributeOptionManagementInterface $attributeOptionManagementInterface
     * @param OptionFactory $optionFactory
     * @param array $data
     */
    public function __construct(
        ProductAttributeOptionManagementInterface $attributeOptionManagementInterface,
        OptionFactory $optionFactory,
        array $data = []
    ) {
        $this->attributeOptionManagementInterface = $attributeOptionManagementInterface;
        $this->optionFactory = $optionFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $attributeCode = 'ymmk_model';
        $sortOrder = 10;
        $optionLabel = 'rrrrrrrrrfrrr';

        $option = $this->optionFactory->create();
        $option->setLabel($optionLabel);
        $option->setSortOrder($sortOrder);
        $this->attributeOptionManagementInterface->add(
            $attributeCode,
            $option
        );
    }
}
  • can you please tell me how i will call this function? in which file ? – user3443146 Jan 16 '17 at 23:01
  • i think i got it i have to run this command php magento setup:upgrade can i run it multiple times ? like i want to test few think. but confuse if it require to uninstall module everytime for re-run it – user3443146 Jan 16 '17 at 23:22

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.