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 
8 
10 
11 use Magento\Bundle\Model\OptionFactory;
14 
19 {
23  private $bundleOptionFactory;
24 
28  private $extensionAttributesJoinProcessor;
29 
33  private $storeManager;
34 
38  private $skuMap = [];
39 
43  private $optionMap = [];
44 
50  public function __construct(
51  OptionFactory $bundleOptionFactory,
52  JoinProcessorInterface $extensionAttributesJoinProcessor,
53  StoreManagerInterface $storeManager
54  ) {
55  $this->bundleOptionFactory = $bundleOptionFactory;
56  $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
57  $this->storeManager = $storeManager;
58  }
59 
66  public function addParentFilterData(int $parentId, int $parentEntityId, string $sku) : void
67  {
68  $this->skuMap[$parentId] = ['sku' => $sku, 'entity_id' => $parentEntityId];
69  }
70 
77  public function getOptionsByParentId(int $parentId) : array
78  {
79  $options = $this->fetch();
80  if (!isset($options[$parentId])) {
81  return [];
82  }
83 
84  return $options[$parentId];
85  }
86 
92  private function fetch() : array
93  {
94  if (empty($this->skuMap) || !empty($this->optionMap)) {
95  return $this->optionMap;
96  }
97 
99  $optionsCollection = $this->bundleOptionFactory->create()->getResourceCollection();
100  // All products in collection will have same store id.
101  $optionsCollection->joinValues($this->storeManager->getStore()->getId());
102 
103  $productTable = $optionsCollection->getTable('catalog_product_entity');
104  $linkField = $optionsCollection->getConnection()->getAutoIncrementField($productTable);
105  $optionsCollection->getSelect()->join(
106  ['cpe' => $productTable],
107  'cpe.'.$linkField.' = main_table.parent_id',
108  []
109  )->where(
110  "cpe.entity_id IN (?)",
111  $this->skuMap
112  );
113  $optionsCollection->setPositionOrder();
114 
115  $this->extensionAttributesJoinProcessor->process($optionsCollection);
116  if (empty($optionsCollection->getData())) {
117  return null;
118  }
119 
121  foreach ($optionsCollection as $option) {
122  if (!isset($this->optionMap[$option->getParentId()])) {
123  $this->optionMap[$option->getParentId()] = [];
124  }
125  $this->optionMap[$option->getParentId()][$option->getId()] = $option->getData();
126  $this->optionMap[$option->getParentId()][$option->getId()]['title']
127  = $option->getTitle() === null ? $option->getDefaultTitle() : $option->getTitle();
128  $this->optionMap[$option->getParentId()][$option->getId()]['sku']
129  = $this->skuMap[$option->getParentId()]['sku'];
130  }
131 
132  return $this->optionMap;
133  }
134 }
$bundleOptionFactory
$storeManager
addParentFilterData(int $parentId, int $parentEntityId, string $sku)
Definition: Collection.php:66
__construct(OptionFactory $bundleOptionFactory, JoinProcessorInterface $extensionAttributesJoinProcessor, StoreManagerInterface $storeManager)
Definition: Collection.php:50