/** * {@inheritdoc} */ public function removeChild($sku, $optionId, $childSku) { $product = $this->productRepository->get($sku, true); if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { throw new InputException(__('Product with specified sku: %1 is not a bundle product', $sku)); } $excludeSelectionIds = []; $usedProductIds = []; $removeSelectionIds = []; foreach ($this->getOptions($product) as $option) { /** @var \Magento\Bundle\Model\Selection $selection */ foreach ($option->getSelections() as $selection) { if (strcasecmp($selection->getSku(), $childSku) == 0 && $selection->getOptionId() == $optionId) { $removeSelectionIds[] = $selection->getSelectionId(); $usedProductIds[] = $selection->getProductId(); continue; } $excludeSelectionIds[] = $selection->getSelectionId(); $usedProductIds[] = $selection->getProductId(); } } if (empty($removeSelectionIds)) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Requested bundle option product doesn\'t exist')); } $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); /* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */ $resource = $this->bundleFactory->create(); $resource->dropAllUnneededSelections($product->getData($linkField), $excludeSelectionIds); $resource->removeProductRelations($product->getData($linkField), array_unique($usedProductIds)); return true; }
/** * Save type related data * * @param \Magento\Catalog\Model\Product $product * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function save($product) { parent::save($product); /* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */ $resource = $this->_bundleFactory->create(); $options = $product->getBundleOptionsData(); if ($options) { $product->setIsRelationsChanged(true); foreach ($options as $key => $option) { if (isset($option['option_id']) && $option['option_id'] == '') { unset($option['option_id']); } $optionModel = $this->_bundleOption->create()->setData($option)->setParentId($product->getId())->setStoreId($product->getStoreId()); $optionModel->isDeleted((bool) $option['delete']); $optionModel->save(); $options[$key]['option_id'] = $optionModel->getOptionId(); } $usedProductIds = []; $excludeSelectionIds = []; $selections = $product->getBundleSelectionsData(); if ($selections) { foreach ($selections as $index => $group) { foreach ($group as $selection) { if (isset($selection['selection_id']) && $selection['selection_id'] == '') { unset($selection['selection_id']); } if (!isset($selection['is_default'])) { $selection['is_default'] = 0; } $selectionModel = $this->_bundleModelSelection->create()->setData($selection)->setOptionId($options[$index]['option_id'])->setWebsiteId($this->_storeManager->getStore($product->getStoreId())->getWebsiteId())->setParentProductId($product->getId()); $selectionModel->isDeleted((bool) $selection['delete']); $selectionModel->save(); $selection['selection_id'] = $selectionModel->getSelectionId(); if ($selectionModel->getSelectionId()) { $excludeSelectionIds[] = $selectionModel->getSelectionId(); $usedProductIds[] = $selectionModel->getProductId(); } } } $resource->dropAllUnneededSelections($product->getId(), $excludeSelectionIds); $resource->saveProductRelations($product->getId(), array_unique($usedProductIds)); } if ($product->getData('price_type') != $product->getOrigData('price_type')) { $resource->dropAllQuoteChildItems($product->getId()); } } return $this; }