/** * @inheritdoc */ public function removeChild($productSku, $optionId, $childSku) { $product = $this->productRepository->get($productSku); if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { throw new Exception(sprintf('Product with specified sku: %s is not a bundle product', $productSku), Exception::HTTP_FORBIDDEN); } $excludeSelectionIds = array(); $usedProductIds = array(); $removeSelectionIds = array(); foreach ($this->getOptions($product) as $option) { foreach ($option->getSelections() as $selection) { if (strcasecmp($selection->getSku(), $childSku) == 0 && $selection->getOptionId() == $optionId) { $removeSelectionIds[] = $selection->getSelectionId(); continue; } $excludeSelectionIds[] = $selection->getSelectionId(); $usedProductIds[] = $selection->getProductId(); } } if (empty($removeSelectionIds)) { throw new NoSuchEntityException('Requested bundle option product doesn\'t exist'); } /* @var $resource \Magento\Bundle\Model\Resource\Bundle */ $resource = $this->bundleFactory->create(); $resource->dropAllUnneededSelections($product->getId(), $excludeSelectionIds); $resource->saveProductRelations($product->getId(), array_unique($usedProductIds)); return true; }
/** * Save type related data * * @param \Magento\Catalog\Model\Product $product * @return $this */ public function save($product) { parent::save($product); /* @var $resource \Magento\Bundle\Model\Resource\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 = array(); $excludeSelectionIds = array(); $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; }