Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\CollectionFactory;
14 use Magento\Catalog\Model\ProductFactory;
18 
23 {
27  private $childCollectionFactory;
28 
32  private $productFactory;
33 
37  private $searchCriteriaBuilder;
38 
42  private $productDataProvider;
43 
47  private $metadataPool;
48 
52  private $parentIds = [];
53 
57  private $childrenMap = [];
58 
62  private $attributeCodes = [];
63 
71  public function __construct(
72  CollectionFactory $childCollectionFactory,
73  ProductFactory $productFactory,
75  DataProvider $productDataProvider,
76  MetadataPool $metadataPool
77  ) {
78  $this->childCollectionFactory = $childCollectionFactory;
79  $this->productFactory = $productFactory;
80  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
81  $this->productDataProvider = $productDataProvider;
82  $this->metadataPool = $metadataPool;
83  }
84 
91  public function addParentId(int $id) : void
92  {
93  if (!in_array($id, $this->parentIds) && !empty($this->childrenMap)) {
94  $this->childrenMap = [];
95  $this->parentIds[] = $id;
96  } elseif (!in_array($id, $this->parentIds)) {
97  $this->parentIds[] = $id;
98  }
99  }
100 
107  public function addEavAttributes(array $attributeCodes) : void
108  {
109  $this->attributeCodes = array_replace($this->attributeCodes, $attributeCodes);
110  }
111 
118  public function getChildProductsByParentId(int $id) : array
119  {
120  $childrenMap = $this->fetch();
121 
122  if (!isset($childrenMap[$id])) {
123  return [];
124  }
125 
126  return $childrenMap[$id];
127  }
128 
134  private function fetch() : array
135  {
136  if (empty($this->parentIds) || !empty($this->childrenMap)) {
137  return $this->childrenMap;
138  }
139 
140  $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
141  foreach ($this->parentIds as $id) {
143  $childCollection = $this->childCollectionFactory->create();
145  $product = $this->productFactory->create();
146  $product->setData($linkField, $id);
147  $childCollection->setProductFilter($product);
148 
150  foreach ($childCollection->getItems() as $childProduct) {
151  $formattedChild = ['model' => $childProduct, 'sku' => $childProduct->getSku()];
152  $parentId = (int)$childProduct->getParentId();
153  if (!isset($this->childrenMap[$parentId])) {
154  $this->childrenMap[$parentId] = [];
155  }
156 
157  $this->childrenMap[$parentId][] = $formattedChild;
158  }
159  }
160 
161  return $this->childrenMap;
162  }
163 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
__construct(CollectionFactory $childCollectionFactory, ProductFactory $productFactory, SearchCriteriaBuilder $searchCriteriaBuilder, DataProvider $productDataProvider, MetadataPool $metadataPool)
Definition: Collection.php:71
$searchCriteriaBuilder