Создайте параметры атрибута (несколько параметров) внутри цикла в модуле magento 2 - PullRequest
0 голосов
/ 17 октября 2019

Я попробовал как фабричный шаблон, так и диспетчер объектов, чтобы создать опции для существующего пользовательского атрибута. Но ничего не работает.

Метод 1 (с помощью диспетчера объектов):

$optionId = $this->getOptionId($attributeCode, $label);
        if (!$optionId) {
            // If no, add it.
            $this->_logger->log('ERROR', 'Here : ', array(1));
            /** @var \Magento\Eav\Model\Entity\Attribute\OptionLabel $optionLabel */
            $optionLabel = $this->optionLabelFactory->create();
            $optionLabel->setStoreId(0);
            $optionLabel->setLabel($label);

            $option = $this->optionFactory->create();
            $option->setLabel($optionLabel);
            $option->setStoreLabels([$optionLabel]);
            $option->setSortOrder($sortOrder);
            $option->setIsDefault(false);
            $optionId = $this->getOptionId($attributeCode, $label);

            $this->attributeOptionManagement->add(
                \Magento\Catalog\Model\Product::ENTITY,
                $this->getAttribute($attributeCode)->getAttributeId(),
                $option
            );
            // Get the inserted ID. Should be returned from the installer, but it isn't.
            $optionId = $this->getOptionId($attributeCode, $label, true);
        }

Метод 2 (с помощью диспетчера объектов):

$optionId = $this->getOptionId($atributeCode,$optionValue);
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        if(!$optionId ){
            $attributeRepository = $objectManager->create('\Magento\Eav\Model\AttributeRepository');
            $attributeId = $attributeRepository->get('catalog_product', $atributeCode)->getAttributeId();
            $option = $objectManager->create('\Magento\Eav\Model\Entity\Attribute\Option');
            $attributeOptionLabel = $objectManager->create('\Magento\Eav\Api\Data\AttributeOptionLabelInterface');
            $attributeOptionManagement = $objectManager->create('\Magento\Eav\Api\AttributeOptionManagementInterface');

            $attributeOptionLabel->setStoreId(0);
            $attributeOptionLabel->setLabel($optionValue);
            $option->setLabel($attributeOptionLabel);
            $option->setValue($optionValue);
            $option->setStoreLabels([$attributeOptionLabel]);
            $option->setSortOrder(rand(100, 1000));
            $option->setIsDefault(false);
            $attributeOptionManagement->add('catalog_product', $attributeId, $option);
            return $optionId = $this->getOptionId($atributeCode,$optionValue);
        }else{
          return $optionId;
        }

$attrName = 'diameter_mm';$val = '7';$sortOrder = 300;
    $optionId = createOrGetId($attrName, $val, $sortOrder);
    var_dump($optionId);
    $sortOrder++;$val = '8';
    $optionId = createOrGetId($attrName, $val, $sortOrder);

Но опция не создана, и я вижу исключение.

See the exceotopn in attachment

...