/** * {@inheritdoc} */ public function getList($productSku) { $product = $this->getProduct($productSku); $optionCollection = $this->type->getOptionsCollection($product); /** @var \Magento\Bundle\Service\V1\Data\Product\Option[] $optionDtoList */ $optionDtoList = []; /** @var \Magento\Bundle\Model\Option $option */ foreach ($optionCollection as $option) { $optionDtoList[] = $this->optionConverter->createDataFromModel($option, $product); } return $optionDtoList; }
/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option) { $option->setStoreId($this->storeManager->getStore()->getId()); $option->setParentId($product->getId()); $optionId = $option->getOptionId(); $linksToAdd = []; if (!$optionId) { $option->setDefaultTitle($option->getTitle()); if (is_array($option->getProductLinks())) { $linksToAdd = $option->getProductLinks(); } } else { $optionCollection = $this->type->getOptionsCollection($product); /** @var \Magento\Bundle\Model\Option $existingOption */ $existingOption = $optionCollection->getItemById($option->getOptionId()); if (!isset($existingOption) || !$existingOption->getOptionId()) { throw new NoSuchEntityException(__('Requested option doesn\'t exist')); } $option->setData(array_merge($existingOption->getData(), $option->getData())); $this->updateOptionSelection($product, $option); } try { $this->optionResource->save($option); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save option'), $e); } /** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */ foreach ($linksToAdd as $linkedProduct) { $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct); } return $option->getOptionId(); }
/** * {@inheritdoc} */ public function deleteById($sku, $optionId) { $product = $this->getProduct($sku); $optionCollection = $this->type->getOptionsCollection($product); $optionCollection->setIdFilter($optionId); return $this->delete($optionCollection->getFirstItem()); }
/** * {@inheritdoc} */ public function update($productSku, $optionId, \Magento\Bundle\Service\V1\Data\Product\Option $option) { $product = $this->getProduct($productSku); $optionCollection = $this->type->getOptionsCollection($product); $optionCollection->setIdFilter($optionId); /** @var \Magento\Bundle\Model\Option $optionModel */ $optionModel = $optionCollection->getFirstItem(); $updateOption = $this->optionConverter->getModelFromData($option, $optionModel); if (!$updateOption->getId()) { throw new NoSuchEntityException('Requested option doesn\'t exist'); } $updateOption->setStoreId($this->storeManager->getStore()->getId()); /** * @var Link[] $existingProductLinks */ $existingProductLinks = $optionModel->getProductLinks(); if (!is_array($existingProductLinks)) { $existingProductLinks = array(); } /** * @var Link[] $newProductLinks */ $newProductLinks = $option->getProductLinks(); if (is_null($newProductLinks)) { $newProductLinks = array(); } /** * @var Link[] $linksToDelete */ $linksToDelete = array_udiff($existingProductLinks, $newProductLinks, array($this, 'compareLinks')); foreach ($linksToDelete as $link) { $this->linkWriteService->removeChild($productSku, $option->getId(), $link->getSku()); } /** * @var Link[] $linksToAdd */ $linksToAdd = array_udiff($newProductLinks, $existingProductLinks, array($this, 'compareLinks')); foreach ($linksToAdd as $link) { $this->linkWriteService->addChild($productSku, $option->getId(), $link); } try { $updateOption->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not save option', [], $e); } return true; }
/** * {@inheritdoc} */ public function update($productSku, $optionId, \Magento\Bundle\Service\V1\Data\Product\Option $option) { $product = $this->getProduct($productSku); $optionCollection = $this->type->getOptionsCollection($product); $optionCollection->setIdFilter($optionId); /** @var \Magento\Bundle\Model\Option $optionModel */ $optionModel = $optionCollection->getFirstItem(); $updateOption = $this->optionConverter->getModelFromData($option, $optionModel); if (!$updateOption->getId()) { throw new NoSuchEntityException('Requested option doesn\'t exist'); } $updateOption->setStoreId($this->storeManager->getStore()->getId()); try { $updateOption->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not save option', [], $e); } return true; }
public function testGetOptionsCollection() { $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['_wakeup', 'getStoreId', 'getData', 'hasData', 'setData', 'getId'])->getMock(); $option = $this->getMockBuilder('\\Magento\\Bundle\\Model\\Option')->disableOriginalConstructor()->getMock(); $dbResourceMock = $this->getMockBuilder('Magento\\Framework\\Model\\Resource\\Db\\Collection\\AbstractCollection')->setMethods(['setProductIdFilter', 'setPositionOrder', 'joinValues'])->disableOriginalConstructor()->getMock(); $store = $this->getMockBuilder('\\Magento\\Store\\Model\\Store')->disableOriginalConstructor()->setMethods(['getId'])->getMock(); $product->expects($this->once())->method('hasData')->with('_cache_instance_options_collection')->willReturn(false); $this->bundleOptionFactory->expects($this->once())->method('create')->willReturn($option); $option->expects($this->once())->method('getResourceCollection')->willReturn($dbResourceMock); $product->expects($this->once())->method('getId')->willReturn('prod_id'); $dbResourceMock->expects($this->once())->method('setProductIdFilter')->with('prod_id')->willReturnSelf(); $product->expects($this->once())->method('getStoreId')->willReturn('store_id'); $product->expects($this->at(3))->method('setData')->willReturnSelf(); $dbResourceMock->expects($this->once())->method('setPositionOrder')->willReturnSelf(); $product->expects($this->at(4))->method('getData')->with('_cache_instance_store_filter')->willReturn($store); $store->expects($this->once())->method('getId')->willReturn('store_id'); $dbResourceMock->expects($this->once())->method('joinValues')->with('store_id')->willReturnSelf(); $product->expects($this->at(5))->method('setData')->with('_cache_instance_options_collection', $dbResourceMock)->willReturnSelf(); $product->expects($this->at(6))->method('getData')->with('_cache_instance_options_collection')->willReturn('result_data'); $this->assertEquals('result_data', $this->model->getOptionsCollection($product)); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option) { $option->setStoreId($this->storeManager->getStore()->getId()); $option->setParentId($product->getId()); if (!$option->getOptionId()) { $option->setDefaultTitle($option->getTitle()); $linksToAdd = is_array($option->getProductLinks()) ? $option->getProductLinks() : []; } else { $optionCollection = $this->type->getOptionsCollection($product); $optionCollection->setIdFilter($option->getOptionId()); /** @var \Magento\Bundle\Model\Option $existingOption */ $existingOption = $optionCollection->getFirstItem(); if (!$existingOption->getOptionId()) { throw new NoSuchEntityException(__('Requested option doesn\'t exist')); } $option->setData(array_merge($existingOption->getData(), $option->getData())); /** @var \Magento\Bundle\Api\Data\LinkInterface[] $existingLinks */ $existingLinks = is_array($existingOption->getProductLinks()) ? $existingOption->getProductLinks() : []; /** @var \Magento\Bundle\Api\Data\LinkInterface[] $newProductLinks */ $newProductLinks = is_array($option->getProductLinks()) ? $option->getProductLinks() : []; /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */ $linksToDelete = array_udiff($existingLinks, $newProductLinks, [$this, 'compareLinks']); foreach ($linksToDelete as $link) { $this->linkManagement->removeChild($product->getSku(), $option->getOptionId(), $link->getSku()); } /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToAdd */ $linksToAdd = array_udiff($newProductLinks, $existingLinks, [$this, 'compareLinks']); } try { $this->optionResource->save($option); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not save option'), $e); } /** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */ foreach ($linksToAdd as $linkedProduct) { $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct); } return $option->getOptionId(); }