/**
  * Create configurable product options
  *
  * @param array $attributesData
  * @return OptionInterface[]
  * @throws \InvalidArgumentException
  */
 public function create(array $attributesData)
 {
     $options = [];
     foreach ($attributesData as $item) {
         $attribute = $this->attributeFactory->create();
         $eavAttribute = $this->productAttributeRepository->get($item[Attribute::KEY_ATTRIBUTE_ID]);
         if (!$this->productType->canUseAttribute($eavAttribute)) {
             throw new \InvalidArgumentException('Provided attribute can not be used with configurable product.');
         }
         $this->updateAttributeData($attribute, $item);
         $options[] = $attribute;
     }
     return $options;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save($sku, OptionInterface $option)
 {
     /** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */
     $configurableAttribute = $this->configurableAttributeFactory->create();
     if ($option->getId()) {
         /** @var Product $product */
         $product = $this->getProduct($sku);
         $configurableAttribute->load($option->getId());
         if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
             throw new NoSuchEntityException(__('Option with id "%1" not found', $option->getId()));
         }
         $configurableAttribute->addData($option->getData());
         $configurableAttribute->setValues($option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices());
         try {
             $configurableAttribute->save();
         } catch (\Exception $e) {
             throw new CouldNotSaveException(__('Could not update option with id "%1"', $option->getId()));
         }
     } else {
         $this->validateNewOptionData($option);
         /** @var Product $product */
         $product = $this->productRepository->get($sku);
         $allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
         if (!in_array($product->getTypeId(), $allowedTypes)) {
             throw new \InvalidArgumentException('Incompatible product type');
         }
         $eavAttribute = $this->productAttributeRepository->get($option->getAttributeId());
         $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
         if ($configurableAttribute->getId()) {
             throw new CouldNotSaveException(__('Product already has this option'));
         }
         $configurableAttributesData = ['attribute_id' => $option->getAttributeId(), 'position' => $option->getPosition(), 'use_default' => $option->getIsUseDefault(), 'label' => $option->getLabel(), 'values' => $option->getValues()];
         try {
             $product->setTypeId(ConfigurableType::TYPE_CODE);
             $product->setConfigurableAttributesData([$configurableAttributesData]);
             $product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId());
             $product->save();
         } catch (\Exception $e) {
             throw new CouldNotSaveException(__('An error occurred while saving option'));
         }
         $configurableAttribute = $this->configurableAttributeFactory->create();
         $configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
     }
     if (!$configurableAttribute->getId()) {
         throw new CouldNotSaveException(__('An error occurred while saving option'));
     }
     return $configurableAttribute->getId();
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function update($productSku, $optionId, Option $option)
 {
     $product = $this->getProduct($productSku);
     $configurableAttribute = $this->configurableAttributeFactory->create();
     $configurableAttribute->load($optionId);
     if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
         throw new NoSuchEntityException('Option with id "%1" not found', [$optionId]);
     }
     $configurableAttribute = $this->optionConverter->getModelFromData($option, $configurableAttribute);
     try {
         $configurableAttribute->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException('Could not update option with id "%1"', [$optionId]);
     }
     return true;
 }
Esempio n. 4
0
 /**
  * @param Option $option
  * @param Attribute $configurableAttribute
  * @return Attribute
  */
 public function getModelFromData(Option $option, Attribute $configurableAttribute)
 {
     /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $returnConfigurableAttribute */
     $returnConfigurableAttribute = $this->attributeFactory->create();
     $returnConfigurableAttribute->setData($configurableAttribute->getData());
     $returnConfigurableAttribute->addData($option->__toArray());
     $returnConfigurableAttribute->setId($configurableAttribute->getId());
     $returnConfigurableAttribute->setAttributeId($configurableAttribute->getAttributeId());
     $returnConfigurableAttribute->setValues($configurableAttribute->getPrices());
     $values = $option->getValues();
     if (!is_null($values)) {
         $prices = [];
         foreach ($values as $value) {
             $prices[] = $this->valueConverter->convertArrayFromData($value);
         }
         $returnConfigurableAttribute->setValues($prices);
     }
     return $returnConfigurableAttribute;
 }
Esempio n. 5
0
 /**
  * Delete data specific for Configurable product type
  *
  * @param \Magento\Catalog\Model\Product $product
  */
 public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product)
 {
     $this->_typeConfigurableFactory->create()->saveProducts($product, []);
     /** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */
     $configurableAttribute = $this->_configurableAttributeFactory->create();
     $configurableAttribute->deleteByProduct($product);
 }