public function testSave() { $options = ['some_option' => ['option_id' => '', 'delete' => false]]; $selections = ['some_option' => [123 => ['selection_id' => '', 'delete' => false]]]; $resource = $this->getMockBuilder('Magento\\Bundle\\Model\\ResourceModel\\Bundle')->disableOriginalConstructor()->getMock(); $this->bundleFactory->expects($this->once())->method('create')->willReturn($resource); $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getStoreId', 'getOrigData', 'getData', 'getBundleOptionsData', 'getBundleSelectionsData'])->disableOriginalConstructor()->getMock(); $product->expects($this->once())->method('getBundleOptionsData')->willReturn($options); $product->expects($this->once())->method('getBundleSelectionsData')->willReturn($selections); $option = $this->getMockBuilder('Magento\\Bundle\\Model\\ResourceModel\\Option\\Collection')->setMethods(['setData', 'setParentId', 'setStoreId', 'isDeleted', 'save', 'getOptionId'])->disableOriginalConstructor()->getMock(); $option->expects($this->once())->method('setData')->willReturnSelf(); $option->expects($this->once())->method('setParentId')->willReturnSelf(); $option->expects($this->once())->method('setStoreId')->willReturnSelf(); $this->bundleOptionFactory->expects($this->once())->method('create')->will($this->returnValue($option)); $selection = $this->getMockBuilder('Magento\\Bundle\\Model\\Selection')->setMethods(['setData', 'setOptionId', 'setParentProductId', 'setWebsiteId', 'save'])->disableOriginalConstructor()->getMock(); $selection->expects($this->once())->method('setData')->willReturnSelf(); $selection->expects($this->once())->method('setOptionId')->willReturnSelf(); $selection->expects($this->once())->method('setParentProductId')->willReturnSelf(); $selection->expects($this->once())->method('setWebsiteId')->willReturnSelf(); $selection->expects($this->once())->method('setParentProductId')->willReturnSelf(); $this->bundleModelSelection->expects($this->once())->method('create')->willReturn($selection); $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->setMethods(['getWebsiteId', '__wakeup'])->disableOriginalConstructor()->getMock(); $this->storeManager->expects($this->once())->method('getStore')->will($this->returnValue($store)); $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue(10)); $this->model->save($product); }
/** * {@inheritdoc} */ public function addChild($productSku, $optionId, Data\ProductLink $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->getQuantity())->setSelectionPriceType($linkedProduct->getPriceType())->setSelectionPriceValue($linkedProduct->getPriceValue())->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(); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function addChild(\Magento\Catalog\Api\Data\ProductInterface $product, $optionId, \Magento\Bundle\Api\Data\LinkInterface $linkedProduct) { if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { throw new InputException(__('Product with specified sku: "%1" is not a bundle product', $product->getSku())); } $options = $this->optionCollection->create(); $options->setIdFilter($optionId); $existingOption = $options->getFirstItem(); if (!$existingOption->getId()) { throw new InputException(__('Product with specified sku: "%1" does not contain option: "%2"', [$product->getSku(), $optionId])); } $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField(); /* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */ $resource = $this->bundleFactory->create(); $selections = $resource->getSelectionsData($product->getData($linkField)); /** @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->getEntityId()) { if (!$product->getCopyFromView()) { throw new CouldNotSaveException(__('Child with specified sku: "%1" already assigned to product: "%2"', [$linkedProduct->getSku(), $product->getSku()])); } else { return $this->bundleSelection->create()->load($linkProductModel->getEntityId()); } } } } $selectionModel = $this->bundleSelection->create(); $selectionModel = $this->mapProductLinkToSelectionModel($selectionModel, $linkedProduct, $linkProductModel->getEntityId(), $product->getData($linkField)); $selectionModel->setOptionId($optionId); try { $selectionModel->save(); $resource->addProductRelation($product->getData($linkField), $linkProductModel->getEntityId()); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e); } return $selectionModel->getId(); }
/** * 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; }