/** * {@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())); }
/** * @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); }
/** * 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; }