Skip to main content
deleted 6 characters in body
Source Link
<?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 schlafgutfoo 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;
    }

}
<?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 schlafgut 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;
    }

}
<?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;
    }

}
Source Link

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 schlafgut 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;
    }

}