/** * {@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; }
public function testGetList() { $productSku = 'oneSku'; $this->productRepository->expects($this->once())->method('get')->with($this->equalTo($productSku))->will($this->returnValue($this->product)); $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)); $this->productType->expects($this->once())->method('getOptionsCollection')->with($this->equalTo($this->product))->will($this->returnValue([$this->optionModel])); $this->optionConverter->expects($this->once())->method('createDataFromModel')->with($this->equalTo($this->optionModel), $this->equalTo($this->product))->will($this->returnValue($this->option)); $this->assertEquals([$this->option], $this->model->getList($productSku)); }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testUpdateNoSuchEntityException() { $productSku = 'oneSku'; $optionId = 3; $this->productRepository->expects($this->once())->method('get')->with($this->equalTo($productSku))->will($this->returnValue($this->product)); $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)); $this->productType->expects($this->once())->method('getOptionsCollection')->with($this->equalTo($this->product))->will($this->returnValue($this->optionCollection)); $this->optionCollection->expects($this->once())->method('setIdFilter')->with($this->equalTo($optionId)); $this->optionCollection->expects($this->once())->method('getFirstItem')->will($this->returnValue($this->optionModel)); $this->optionConverter->expects($this->once())->method('getModelFromData')->with($this->equalTo($this->option), $this->equalTo($this->optionModel))->will($this->returnValue($this->optionModel)); $this->optionModel->expects($this->once())->method('getId'); $this->model->update($productSku, $optionId, $this->option); }
/** * {@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; }