/** * {@inheritdoc} */ public function load($id, \Magento\Catalog\Service\V1\Data\ProductBuilder $productBuilder) { /** @var \Magento\Catalog\Model\Product */ $product = $this->productRepository->get($id); if ($product->getTypeId() != ProductType::TYPE_BUNDLE) { return; } $productBuilder->setCustomAttribute('bundle_product_options', $this->optionReadService->getList($product->getSku())); }
public function testLoadBundleProduct() { $productId = 'test_id'; $productSku = 'test_sku'; $this->productRepository->expects($this->once())->method('get')->with($productId)->will($this->returnValue($this->product)); $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)); $this->product->expects($this->once())->method('getSku')->will($this->returnValue($productSku)); $optionCustomAttributeValue = ['a', 'b']; $this->optionReadService->expects($this->once())->method('getList')->with($productSku)->will($this->returnValue($optionCustomAttributeValue)); $this->productBuilder->expects($this->at(0))->method('setCustomAttribute')->with('bundle_product_options', $optionCustomAttributeValue); $this->model->load($productId, $this->productBuilder); }
/** * @expectedException \Magento\Webapi\Exception * @expectedExceptionCode 403 */ public function testGetListWebApiException() { $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_SIMPLE)); $this->model->getList($productSku); }
public function testUpdate() { $product1Sku = 'sku1'; $productOption1Sku = 'productOption1Sku'; $productOption2Sku = 'productOption2Sku'; $product1Options = [$this->productOption1, $this->productOption2]; $product2Options = [$this->productOption1]; $this->productRepository->expects($this->once())->method('get')->with($product1Sku, true)->will($this->returnValue($this->product)); $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue(ProductType::TYPE_BUNDLE)); $this->optionReadService->expects($this->once())->method('getList')->with($product1Sku)->will($this->returnValue($product1Options)); $this->productData->expects($this->once())->method('getCustomAttribute')->with('bundle_product_options')->will($this->returnValue($this->attributeValue)); $this->attributeValue->expects($this->any())->method('getValue')->will($this->returnValue($product2Options)); $this->productOption1->expects($this->any())->method('getId')->will($this->returnValue($productOption1Sku)); $this->productOption2->expects($this->any())->method('getId')->will($this->returnValue($productOption2Sku)); $this->optionWriteService->expects($this->once())->method('remove')->with($product1Sku, $productOption2Sku)->will($this->returnValue(1)); $this->assertEquals($product1Sku, $this->saveProcessor->update($product1Sku, $this->productData)); }
/** * Update bundle-related attributes of product. * * @param string $sku * @param Product $updatedProduct * @return string */ public function update($sku, Product $updatedProduct) { /** * @var Product $existingProduct */ $existingProduct = $this->productRepository->get($sku, true); if ($existingProduct->getTypeId() != ProductType::TYPE_BUNDLE) { return $sku; } /** * @var Option[] $existingProductOptions */ $existingProductOptions = $this->optionReadService->getList($sku); /** * @var Option[] $newProductOptions */ /** * @var AttributeValue $newProductOptionsAttrValue */ $newProductOptionsAttrValue = $updatedProduct->getCustomAttribute('bundle_product_options'); if (is_null($newProductOptionsAttrValue) || !is_array($newProductOptionsAttrValue->getValue())) { $newProductOptions = array(); } else { $newProductOptions = $newProductOptionsAttrValue->getValue(); } /** * @var Option[] $optionsToDelete */ $optionsToDelete = array_udiff($existingProductOptions, $newProductOptions, array($this, 'compareOptions')); foreach ($optionsToDelete as $option) { $this->optionWriteService->remove($sku, $option->getId()); } /** @var Option[] $optionsToUpdate */ $optionsToUpdate = array_uintersect($existingProductOptions, $newProductOptions, array($this, 'compareOptions')); foreach ($optionsToUpdate as $option) { $this->optionWriteService->update($sku, $option->getId(), $option); } /** * @var Option[] $optionsToAdd */ $optionsToAdd = array_udiff($newProductOptions, $existingProductOptions, array($this, 'compareOptions')); foreach ($optionsToAdd as $option) { $this->optionWriteService->add($sku, $option); } return $sku; }