1

i try to add the value of an custom select and multiselect attribute in my custom product import job. But it do not work.

F.e. I have a custom SELECT attribute "pattern" with values (A,B,C), than i try to add the related product value in my import:

...
$oSimpleProduct = $this->_objectManager->create('\Magento\Catalog\Model\Product');

// ... add the min req. attributes

// HERE is my problem ... $oProductItem->getPattern() has value "A"
$oSimpleProduct->setCustomAttribute('pattern', $oProductItem->getPattern());

$oSimpleProduct->save();

...

After the save the product shows up in the magento admin .. but the field is still empty. If i load the product field it shows:

object(Magento\Framework\Api\AttributeValue)#3969 (1) {
  ["_data":protected]=>
  array(2) {
    ["attribute_code"]=>
    string(7) "pattern"
    ["value"]=>
    string(1) "0"
  }
}

Do anyone know what i am doing wrong? - How can i add SELECT AND MULTISELECT Values?

1 Answer 1

1

Ok .. i found a solution as i am oriented me at magento 1 ..

My Solution ( ONLY AN EXAMPLE )!!! :

<?php

namespace Foo\Bar\Console\Command;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Command\Command;
use Magento\Eav\Model\Config as EavConfig;

/**
 * Class Import
 * @package Foo\Bar\Console\Command
 */
class Import extends Command
{
    /**
     * @var EavConfig
     */
    protected $_eavConfig;

    /**
     * Foo constructor.
     * @param EavConfig $eavConfig
     */
    public function __construct(EavConfig $eavConfig)
    {
        $this->_eavConfig = $eavConfig;
        parent::__construct('foo:import_products');
    }

    protected function configure()
    {
        $this->setName('foo:import_products')->setDescription('Full import of foo product xml');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {

        // ....
        $sValueKey = $this->_getOptionValueId('pattern', $oProductItem->getPattern());

        if ($sValueKey) {
            $oSimpleProduct->setCustomAttribute('pattern', $sValueKey);
        } else {
            // LOG .. OR SOMETHING
        }
        // ....

    }

    /**
     * @param $attributeCode
     * @param $searchValue
     * @return bool|string
     */
    protected function _getOptionValueId($attributeCode, $searchValue)
    {
        /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $oAttribute */
        $oAttribute = $this->_loadAttribute($attributeCode);
        if (!$oAttribute) {
            return false;
        }

        foreach ($oAttribute->getSource()->getAllOptions() as $option) {
            if (strtoupper($option['label']) == strtoupper($searchValue)) {
                return $option['value'];
            }
        }
        return false;
    }

    /**
     * @param string $attributeCode
     * @return bool|Attribute
     */
    protected function _loadAttribute($attributeCode)
    {
        /** @var Attribute $attribute */
        $oAttribute = $this->_eavConfig->getAttribute('catalog_product',$attributeCode);
        if (!$oAttribute->getId()) {
            return false;
        }
        return $oAttribute;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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