55 private $metadataPool;
89 $product = $this->productRepository->get($productSku,
true);
90 if (
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
91 throw new InputException(
__(
'This is implemented for bundle products only.'));
100 foreach (
$option->getSelections() as $selection) {
101 $childrenList[] = $this->buildLink($selection,
$product);
104 return $childrenList;
113 $product = $this->productRepository->get($sku,
true);
126 $product = $this->productRepository->get($sku,
true);
127 if (
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
128 throw new InputException(
129 __(
'The product with the "%1" SKU isn\'t a bundle product.', [
$product->getSku()])
134 $linkProductModel = $this->productRepository->get(
$linkedProduct->getSku());
135 if ($linkProductModel->isComposite()) {
136 throw new InputException(
__(
'The bundle product can\'t contain another composite product.'));
140 throw new InputException(
__(
'The product link needs an ID field entered. Enter and try again.'));
144 $selectionModel = $this->bundleSelection->create();
146 if (!$selectionModel->getId()) {
147 throw new InputException(
149 'The product link with the "%1" ID field wasn\'t found. Verify the ID and try again.',
154 $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
158 $linkProductModel->getId(),
163 $selectionModel->save();
164 }
catch (\Exception $e) {
165 throw new CouldNotSaveException(
__(
'Could not save child: "%1"', $e->getMessage()), $e);
186 $selectionModel->setProductId($linkedProductId);
187 $selectionModel->setParentProductId($parentProductId);
189 $selectionModel->setSelectionId(
$productLink->getSelectionId());
192 $selectionModel->setOptionId(
$productLink->getOptionId());
195 $selectionModel->setPosition(
$productLink->getPosition());
198 $selectionModel->setSelectionQty(
$productLink->getQty());
201 $selectionModel->setSelectionPriceType(
$productLink->getPriceType());
204 $selectionModel->setSelectionPriceValue(
$productLink->getPrice());
207 $selectionModel->setSelectionCanChangeQty(
$productLink->getCanChangeQuantity());
210 $selectionModel->setIsDefault(
$productLink->getIsDefault());
213 return $selectionModel;
225 if (
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
227 __(
'The product with the "%1" SKU isn\'t a bundle product.',
$product->getSku())
231 $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
233 $options = $this->optionCollection->create();
238 $existingOption =
$options->getFirstItem();
240 if (!$existingOption->getId()) {
241 throw new InputException(
243 'Product with specified sku: "%1" does not contain option: "%2"',
250 $resource = $this->bundleFactory->create();
253 $linkProductModel = $this->productRepository->get(
$linkedProduct->getSku());
254 if ($linkProductModel->isComposite()) {
255 throw new InputException(
__(
'The bundle product can\'t contain another composite product.'));
259 foreach ($selections as $selection) {
260 if ($selection[
'option_id'] ==
$optionId &&
261 $selection[
'product_id'] == $linkProductModel->getEntityId() &&
262 $selection[
'parent_product_id'] ==
$product->getData($linkField)) {
264 throw new CouldNotSaveException(
266 'Child with specified sku: "%1" already assigned to product: "%2"',
271 return $this->bundleSelection->create()->load($linkProductModel->getEntityId());
277 $selectionModel = $this->bundleSelection->create();
281 $linkProductModel->getEntityId(),
287 $selectionModel->save();
288 $resource->addProductRelation(
$product->getData($linkField), $linkProductModel->getEntityId());
289 }
catch (\Exception $e) {
290 throw new CouldNotSaveException(
__(
'Could not save child: "%1"', $e->getMessage()), $e);
293 return $selectionModel->getId();
301 $product = $this->productRepository->get($sku,
true);
303 if (
$product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
304 throw new InputException(
__(
'The product with the "%1" SKU isn\'t a bundle product.', $sku));
307 $excludeSelectionIds = [];
308 $usedProductIds = [];
309 $removeSelectionIds = [];
312 foreach (
$option->getSelections() as $selection) {
313 if ((strcasecmp($selection->getSku(), $childSku) == 0) && ($selection->getOptionId() ==
$optionId)) {
314 $removeSelectionIds[] = $selection->getSelectionId();
315 $usedProductIds[] = $selection->getProductId();
318 $excludeSelectionIds[] = $selection->getSelectionId();
321 if (empty($removeSelectionIds)) {
322 throw new \Magento\Framework\Exception\NoSuchEntityException(
323 __(
"The bundle product doesn't exist. Review your request and try again.")
326 $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
328 $resource = $this->bundleFactory->create();
329 $resource->dropAllUnneededSelections(
$product->getData($linkField), $excludeSelectionIds);
330 $resource->removeProductRelations(
$product->getData($linkField), array_unique($usedProductIds));
340 private function buildLink(\
Magento\Catalog\Model\Product $selection, \
Magento\Catalog\Model\Product
$product)
342 $selectionPriceType = $selectionPrice =
null;
346 $selectionPriceType = $selection->getSelectionPriceType();
347 $selectionPrice = $selection->getSelectionPriceValue();
351 $link = $this->linkFactory->create();
352 $this->dataObjectHelper->populateWithArray(
354 $selection->getData(),
355 \Magento\Bundle\Api\Data\LinkInterface::class
357 $link->setIsDefault($selection->getIsDefault())
358 ->setId($selection->getSelectionId())
359 ->setQty($selection->getSelectionQty())
360 ->setCanChangeQuantity($selection->getSelectionCanChangeQty())
361 ->setPrice($selectionPrice)
362 ->setPriceType($selectionPriceType);
370 private function getOptions(\
Magento\Catalog\Api\Data\ProductInterface
$product)
373 $productTypeInstance =
$product->getTypeInstance();
374 $productTypeInstance->setStoreFilter(
382 $productTypeInstance->getOptionsIds(
$product),
394 private function getMetadataPool()
396 if (!$this->metadataPool) {
399 return $this->metadataPool;
removeChild($sku, $optionId, $childSku)
getChildren($productSku, $optionId=null)
mapProductLinkToSelectionModel(\Magento\Bundle\Model\Selection $selectionModel, \Magento\Bundle\Api\Data\LinkInterface $productLink, $linkedProductId, $parentProductId)
addChild(\Magento\Catalog\Api\Data\ProductInterface $product, $optionId, \Magento\Bundle\Api\Data\LinkInterface $linkedProduct)
addChildByProductSku($sku, $optionId, \Magento\Bundle\Api\Data\LinkInterface $linkedProduct)
saveChild( $sku, \Magento\Bundle\Api\Data\LinkInterface $linkedProduct)
__construct(ProductRepositoryInterface $productRepository, \Magento\Bundle\Api\Data\LinkInterfaceFactory $linkFactory, \Magento\Bundle\Model\SelectionFactory $bundleSelection, \Magento\Bundle\Model\ResourceModel\BundleFactory $bundleFactory, \Magento\Bundle\Model\ResourceModel\Option\CollectionFactory $optionCollection, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper)