/** * @inheritdoc */ public function getChildren($productId) { $product = $this->productRepository->get($productId); if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { throw new Exception('Only implemented for bundle product', Exception::HTTP_FORBIDDEN); } $childrenList = []; foreach ($this->getOptions($product) as $option) { /** @var \Magento\Catalog\Model\Product $selection */ foreach ($option->getSelections() as $selection) { $childrenList[] = $this->linkConverter->createDataFromModel($selection, $product); } } return $childrenList; }
/** * @param Product $product * @param int $optionId * @return array|null */ private function getProductLinks(Product $product, $optionId) { /** @var \Magento\Bundle\Model\Product\Type $productTypeInstance */ $productTypeInstance = $product->getTypeInstance(); $productTypeInstance->setStoreFilter($product->getStoreId(), $product); $selectionCollection = $productTypeInstance->getSelectionsCollection([$optionId], $product); $productLinks = []; /** @var \Magento\Catalog\Model\Product $selection */ foreach ($selectionCollection as $selection) { $productLinks[] = $this->linkConverter->createDataFromModel($selection, $product); } return $productLinks; }
public function testGetChildren() { $productSku = 'productSku'; $this->getOptions(); $this->productRepository->expects($this->any())->method('get')->with($this->equalTo($productSku))->will($this->returnValue($this->product)); $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue('bundle')); $this->productType->expects($this->once())->method('setStoreFilter')->with($this->equalTo($this->storeId), $this->product); $this->productType->expects($this->once())->method('getSelectionsCollection')->with($this->equalTo($this->optionIds), $this->equalTo($this->product))->will($this->returnValue($this->selectionCollection)); $this->productType->expects($this->once())->method('getOptionsIds')->with($this->equalTo($this->product))->will($this->returnValue($this->optionIds)); $this->optionCollection->expects($this->once())->method('appendSelections')->with($this->equalTo($this->selectionCollection))->will($this->returnValue([$this->option])); $this->option->expects($this->any())->method('getSelections')->will($this->returnValue([$this->product])); $this->linkConverter->expects($this->once())->method('createDataFromModel')->with($this->equalTo($this->product), $this->equalTo($this->product))->will($this->returnValue($this->link)); $this->assertEquals([$this->link], $this->model->getChildren($productSku)); }