/** * {@inheritdoc} */ public function delete(OptionInterface $option) { $product = $this->getProductById($option->getProductId()); try { $this->configurableTypeResource->saveProducts($product, []); } catch (\Exception $exception) { throw new StateException(__('Cannot delete variations from product: %1', $option->getProductId())); } try { $this->optionResource->delete($option); } catch (\Exception $exception) { throw new StateException(__('Cannot delete option with id: %1', $option->getId())); } return true; }
/** * Get product entity id by product attribute * * @param OptionInterface $option * @return int */ public function getEntityIdByAttribute(OptionInterface $option) { $select = $this->getConnection()->select()->from(['e' => $this->getTable('catalog_product_entity')], ['e.entity_id'])->where('e.' . $this->getProductEntityLinkField() . '=?', $option->getProductId())->limit(1); return (int) $this->getConnection()->fetchOne($select); }
/** * {@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(); }