/** * @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->metadataConverter->createDataFromModel($selection, $product); } } return $childrenList; }
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->metadataConverter->expects($this->once())->method('createDataFromModel')->with($this->equalTo($this->product), $this->equalTo($this->product))->will($this->returnValue($this->metadata)); $this->assertEquals([$this->metadata], $this->model->getChildren($productSku)); }