/** * {@inheritdoc} */ public function addChild($productSku, $optionId, \Magento\Bundle\Service\V1\Data\Product\Link $linkedProduct) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productRepository->get($productSku); if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { throw new InputException('Product with specified sku: "%1" is not a bundle product', [$productSku]); } $options = $this->optionCollection->create(); $options->setProductIdFilter($product->getId())->joinValues($this->storeManager->getStore()->getId()); $isNewOption = true; /** @var \Magento\Bundle\Model\Option $option */ foreach ($options as $option) { if ($option->getOptionId() == $optionId) { $isNewOption = false; break; } } if ($isNewOption) { throw new InputException('Product with specified sku: "%1" does not contain option: "%2"', [$productSku, $optionId]); } /* @var $resource \Magento\Bundle\Model\Resource\Bundle */ $resource = $this->bundleFactory->create(); $selections = $resource->getSelectionsData($product->getId()); /** @var \Magento\Catalog\Model\Product $linkProductModel */ $linkProductModel = $this->productRepository->get($linkedProduct->getSku()); if ($linkProductModel->isComposite()) { throw new InputException('Bundle product could not contain another composite product'); } if ($selections) { foreach ($selections as $selection) { if ($selection['option_id'] = $optionId && $selection['product_id'] == $linkProductModel->getId()) { throw new CouldNotSaveException('Child with specified sku: "%1" already assigned to product: "%2"', [$linkedProduct->getSku(), $productSku]); } } } $selectionModel = $this->bundleSelection->create(); $selectionModel->setOptionId($optionId)->setPosition($linkedProduct->getPosition())->setSelectionQty($linkedProduct->getQty())->setSelectionPriceType($linkedProduct->getPriceType())->setSelectionPriceValue($linkedProduct->getPrice())->setSelectionCanChangeQty($linkedProduct->getCanChangeQuantity())->setProductId($linkProductModel->getId())->setParentProductId($product->getId())->setIsDefault($linkedProduct->isDefault())->setWebsiteId($this->storeManager->getStore()->getWebsiteId()); try { $selectionModel->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not save child: "%1"', [$e->getMessage()], $e); } return $selectionModel->getId(); }
/** * Compare two links and determine if they are equal * * @param Link $firstLink * @param Link $secondLink * @return int */ private function compareLinks(Link $firstLink, Link $secondLink) { if ($firstLink->getSku() == $secondLink->getSku()) { return 0; } else { return 1; } }