コード例 #1
0
 /**
  * Update attribute data
  *
  * @param OptionInterface $attribute
  * @param array $item
  * @return void
  */
 private function updateAttributeData(OptionInterface $attribute, array $item)
 {
     $values = [];
     foreach ($item['values'] as $value) {
         $option = $this->optionValueFactory->create();
         $option->setValueIndex($value['value_index']);
         $values[] = $option;
     }
     $attribute->setData(array_replace_recursive((array) $attribute->getData(), $item));
     $attribute->setValues($values);
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save($sku, OptionInterface $option)
 {
     $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
     if ($option->getId()) {
         /** @var Product $product */
         $product = $this->getProduct($sku);
         $data = $option->getData();
         $option->load($option->getId());
         $option->setData(array_replace_recursive($option->getData(), $data));
         if (!$option->getId() || $option->getProductId() != $product->getData($metadata->getLinkField())) {
             throw new NoSuchEntityException(__('Option with id "%1" not found', $option->getId()));
         }
     } else {
         /** @var Product $product */
         $product = $this->productRepository->get($sku);
         $this->validateNewOptionData($option);
         $allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
         if (!in_array($product->getTypeId(), $allowedTypes)) {
             throw new \InvalidArgumentException('Incompatible product type');
         }
         $option->setProductId($product->getData($metadata->getLinkField()));
     }
     try {
         $option->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Something went wrong while saving option.'));
     }
     if (!$option->getId()) {
         throw new CouldNotSaveException(__('Something went wrong while saving option.'));
     }
     return $option->getId();
 }