Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinksList.php
Go to the documentation of this file.
1 <?php
8 
9 class LinksList
10 {
14  protected $linkFactory;
15 
19  protected $type;
20 
24  protected $dataObjectHelper;
25 
31  public function __construct(
32  \Magento\Bundle\Api\Data\LinkInterfaceFactory $linkFactory,
33  \Magento\Bundle\Model\Product\Type $type,
34  \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
35  ) {
36  $this->linkFactory = $linkFactory;
37  $this->type = $type;
38  $this->dataObjectHelper = $dataObjectHelper;
39  }
40 
46  public function getItems(\Magento\Catalog\Api\Data\ProductInterface $product, $optionId)
47  {
48  $selectionCollection = $this->type->getSelectionsCollection([$optionId], $product);
49 
50  $productLinks = [];
52  foreach ($selectionCollection as $selection) {
53  $selectionPriceType = $product->getPriceType() ? $selection->getSelectionPriceType() : null;
54  $selectionPrice = $product->getPriceType() ? $selection->getSelectionPriceValue() : null;
55 
57  $productLink = $this->linkFactory->create();
58  $this->dataObjectHelper->populateWithArray(
60  $selection->getData(),
61  \Magento\Bundle\Api\Data\LinkInterface::class
62  );
63  $productLink->setIsDefault($selection->getIsDefault())
64  ->setId($selection->getSelectionId())
65  ->setQty($selection->getSelectionQty())
66  ->setCanChangeQuantity($selection->getSelectionCanChangeQty())
67  ->setPrice($selectionPrice)
68  ->setPriceType($selectionPriceType);
69  $productLinks[] = $productLink;
70  }
71  return $productLinks;
72  }
73 }
$selectionCollection