2

Need way to add options to already created Color attribute.

7
1

Please follow below steps

1.Create csv with color and attribute values as shown below
& keep your csv at M2root/scripts/colorvalues.csv

enter image description here

  1. Create colors.php at Magento 2 root directory and paste below code & execute script using command line

php colors.php

 <?php

    ini_set('display_errors', '1');
    ini_set('error_reporting', E_ALL);

    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/app/bootstrap.php';

    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $entityType = 'catalog_product';
    $directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
    $path  =  $directory->getRoot().'/scripts/';
    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
    $eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
    $eavSetup = $objectManager->get('\Magento\Eav\Setup\EavSetup');
    $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
    $stores = $storeManager->getStores();
    $storeArray[0] = "All Store Views";
    foreach ($stores  as $store) 
    {
       $storeArray[$store->getId()] = $store->getName();
    }

    $fname = 'colorvalues.csv';

    $file = fopen($path.$fname, "r");
    while (($data = fgetcsv($file, 100000, ",")) !== FALSE)
    {
        foreach((array)$data[0] as $attributeCode)
        {
        $option = array();
        $attribute = $eavConfig->getAttribute($entityType, $attributeCode);
        $option['attribute_id'] = $attribute->getAttributeId();
        $options = $attribute->getSource()->getAllOptions();
        if($data[1])
        {
        foreach((array)$data[1] as $key => $value)
        {
            $str = '"'.$value.'"';
            $option['value'][$str][0]=str_replace('"','', $str);
            foreach($storeArray as $storeKey => $store) 
            {
              $option['value'][$str][$storeKey] = str_replace('"','', $str);
            }   

        }

        $eavSetup->addAttributeOption($option);


        }
        }
    }
        fclose($file);

    echo "Attribute option values has been associated to brand attribute SUCCESSFULLY";
  1. do Re index from command line php bin/magento indexer:reindex

Imp Note: Please try in your default magento2 in local system first and test properly.

Please let me know still facing any issue?

1
  • ok i try and let you know
    – Sourav
    Mar 26 '19 at 7:05
0

Add Attributte Option Like This :-

public function __construct(
         \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
         \Magento\Store\Model\StoreManagerInterface $storeManager,
         \Magento\Eav\Model\Entity\AttributeFactory $eavAttribute
   ){
     $this->_eavAttribute = $eavAttribute;
     $this->_storeManager = $storeManager;
     $this->_eavSetupFactory = $eavSetupFactory;
   }

public function yourFunction()
{
           $attributeId = $this->_eavAttribute->create()->load('color','attribute_code');
           $option = [enter_option_here];
           $allStores = $this->_storeManager->getStores();
           $option['attribute_id'] = $attributeId->getAttributeId();
           foreach($option as $key=>$value){
                $option['value'][$value][0]=$value;
                    foreach($allStores as $store){
                        $option['value'][$value][$store->getId()] = $value;
                    }
                }
                $eavSetup = $this->_eavSetupFactory->create();
                $eavSetup->addAttributeOption($option);}
}

Reference Link :-

Import Attribute Options Programmatically

8
  • this is for magento 1 way i have already tried
    – Sourav
    Mar 23 '19 at 11:39
  • this used in magento 2 also i used this in my module
    – Rk Rathod
    Mar 23 '19 at 12:04
  • I used same link pearlbells.co.uk/… but in backend there is not any options showing
    – Sourav
    Mar 23 '19 at 16:21
  • I want to update options values from csv file & using code in file that is at root of magento
    – Sourav
    Mar 23 '19 at 16:22
  • pearlbells.co.uk/… I use same code but in admin new values uploaded
    – Sourav
    Mar 23 '19 at 16:28
0

Simple and easy to the understandable script here.

<?php

use Magento\Framework\App\Bootstrap;

require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');

attributeValue::addValuesforAttribute('colorAttribute');

class attributeValue{

    public static function addValuesforAttribute($attributeCode)
    {
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       $fileCsv = $objectManager->get('Magento\Framework\File\Csv');
       $attributeFactory = $objectManager->create('Magento\Eav\Setup\EavSetupFactory');
       $eavFactory = $objectManager->get('\Magento\Eav\Model\Entity\AttributeFactory');
        $option = array();
        $eavFactory = $eavFactory->create()->load($attributeCode,"attribute_code");
        $option['attribute_id'] = $eavFactory->getAttributeId();
        $values = "attributeValue.csv";
        $csvData = $fileCsv->getData($values);
        foreach ($csvData as $row => $data) {
                $color = $data[0];
                $option['value'][$color][] =  $color;
    }
        $eavSetup = $attributeFactory->create();
        $eavSetup->addAttributeOption($option);
        echo " \n Options are ";
        print_r($option);
        echo " Succesfull add Options for Attribute ".$attributeCode;
    }
}

CSV color name formate is enter image description here

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.