0

I created simple importer for attribute's values. It works really easy right now and it was necessary for import configurable products.

Importer for attribute values works fine (I'll paste code later). Importer for products works fine also.

So where is issue?

I have to import values for 2 attributes: Size and Color. Color is Visual Swatch Size is Text Swatch.

Color works fine with no problem. Size is not showing on frontend page: enter image description here (there is color swatch and I can select it).

On admin Size attribute, when I'm trying to save it as Text Swatch : enter image description here

I'm able to save it as Dropdown, and works fine: enter image description here

Snip for \project\app\code\Magento\Swatches\Model\Plugin\EavAttribute.php:173-183

protected function prepareOptionIds(array $optionsArray)
{
    if (isset($optionsArray['value']) && is_array($optionsArray['value'])) {
        foreach (array_keys($optionsArray['value']) as $optionId) {
            if (isset($optionsArray['delete'])  && $optionsArray['delete'][$optionId] == 1) {
                unset($optionsArray['value'][$optionId]);
            }
        }
    }
    return $optionsArray;
}

code for my importer: Controller:

namespace Cpny\AttributeImporter\Controller\Attr;


use Magento\Framework\App\Action\Context;


class Upload extends \Magento\Framework\App\Action\Action
{

    protected $_eavSetupFactory;
    protected $_storeManager;
    protected $_attributeFactory;
    protected $_csvImporter;
    protected $_moduleReader;

    public function __construct(
        Context $context,
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory,
        \Cpny\AttributeImporter\Model\Getcsv $_csvImporter

    ) {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_storeManager = $storeManager;
        $this->_attributeFactory = $attributeFactory;
        $this->_csvImporter = $_csvImporter;

        parent::__construct($context);
    }

    public function execute()
    {

        $attribute_arr = $this->_csvImporter->importFromCsvFile();
        $attributeInfo = $this->_attributeFactory->getCollection()
            ->addFieldToFilter('attribute_code',['eq'=>"color"])
            ->getFirstItem();

        $attribute_id = $attributeInfo->getAttributeId();

        $option=array();
        $option['attribute_id'] = $attribute_id;

        foreach($attribute_arr as $key=>$value){
            $option['value']['000a'.$value][0] = $value;

          //  $option['value'][$value][1] = $value;

        }

        $eavSetup = $this->_eavSetupFactory->create();
        $eavSetup->addAttributeOption($option);
        $eavSetup->save();
    }

}

Model/Getcsv:

namespace  Cpny\AttributeImporter\Model;

class Getcsv
{
    protected $_fileCsv;
    protected $_directory_list;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\File\Csv $csvProcessor,
        \Magento\Framework\App\Filesystem\DirectoryList $directory_list,
        array $data = []

    )
    {
        $this->_directory_list = $directory_list;
        $this->_fileCsv = $csvProcessor;
    }


    public function importFromCsvFile()
    {
        $directory = $this->_directory_list->getPath('var');
        $returnValues = [];
        $file = $directory . '/kolory.csv';

        if (file_exists($file)) {
            $data = $this->_fileCsv->getData($file);

            foreach($data as $label => $value) {
                $returnValues[] = $value[0];
            }
        }

        return $returnValues;
    }
}

~50 new values for Colors and ~500 new values for Size.

Should I import Text swatch in different way?

I had to use $option['value']['000a'.$value][0] = $value; because I want to create new ids for values. I know it isn't perfect script and I will refactor and upgrade it later but right now I just need to fast import those values.

3
  • Please add your solution as an answer.
    – Ben Crook
    Feb 21, 2017 at 13:11
  • Posted answer based on your explanation in your question. Feb 21, 2017 at 13:15
  • I see. I will do that. Mar 13, 2017 at 13:23

1 Answer 1

0

I solved the problem:

  1. max_input_vars was too small for saving 500 values with 3 inputs.
    Fix:
    set max_input_vars to 5000 (or more if necessary).

  2. There is no Size on frontend because after import, size attribute doesn't have values for 'swatch' input.
    Fix:

    1. save as Vision Swatch (set swatches automatically as empty value)
    2. set swatch for every value
    3. add to import swatch value

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.