Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Selection.php
Go to the documentation of this file.
1 <?php
7 
13 
21 {
26  protected $metadataPool;
27 
31  private $entityManager;
32 
41  public function __construct(
42  Context $context,
44  $connectionName = null,
45  EntityManager $entityManager = null
46  ) {
47  parent::__construct(
48  $context,
50  );
51 
52  $this->metadataPool = $metadataPool;
53 
54  $this->entityManager = $entityManager
55  ?: ObjectManager::getInstance()->get(EntityManager::class);
56  }
57 
63  protected function _construct()
64  {
65  $this->_init('catalog_product_bundle_selection', 'selection_id');
66  }
67 
78  public function getChildrenIds($parentId, $required = true)
79  {
80  $childrenIds = [];
81  $notRequired = [];
82  $connection = $this->getConnection();
83  $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
84  $select = $connection->select()->from(
85  ['tbl_selection' => $this->getMainTable()],
86  ['product_id', 'parent_product_id', 'option_id']
87  )->join(
88  ['e' => $this->getTable('catalog_product_entity')],
89  'e.entity_id = tbl_selection.product_id AND e.required_options=0',
90  []
91  )->join(
92  ['parent' => $this->getTable('catalog_product_entity')],
93  'tbl_selection.parent_product_id = parent.' . $linkField
94  )->join(
95  ['tbl_option' => $this->getTable('catalog_product_bundle_option')],
96  'tbl_option.option_id = tbl_selection.option_id',
97  ['required']
98  )->where(
99  'parent.entity_id = :parent_id'
100  );
101  foreach ($connection->fetchAll($select, ['parent_id' => $parentId]) as $row) {
102  if ($row['required']) {
103  $childrenIds[$row['option_id']][$row['product_id']] = $row['product_id'];
104  } else {
105  $notRequired[$row['option_id']][$row['product_id']] = $row['product_id'];
106  }
107  }
108 
109  if (!$required) {
110  $childrenIds = array_merge($childrenIds, $notRequired);
111  } else {
112  if (!$childrenIds) {
113  foreach ($notRequired as $groupedChildrenIds) {
114  foreach ($groupedChildrenIds as $childId) {
115  $childrenIds[0][$childId] = $childId;
116  }
117  }
118  }
119  if (!$childrenIds) {
120  $childrenIds = [[]];
121  }
122  }
123 
124  return $childrenIds;
125  }
126 
133  public function getParentIdsByChild($childId)
134  {
135  $connection = $this->getConnection();
136  $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
137  $select = $connection->select()->distinct(
138  true
139  )->from(
140  $this->getMainTable(),
141  ''
142  )->join(
143  ['e' => $this->metadataPool->getMetadata(ProductInterface::class)->getEntityTable()],
144  'e.' . $metadata->getLinkField() . ' = ' . $this->getMainTable() . '.parent_product_id',
145  ['e.entity_id as parent_product_id']
146  )->where(
147  $this->getMainTable() . '.product_id IN(?)',
148  $childId
149  );
150 
151  return $connection->fetchCol($select);
152  }
153 
160  public function saveSelectionPrice($item)
161  {
162  $connection = $this->getConnection();
163  if ($item->getDefaultPriceScope()) {
164  $connection->delete(
165  $this->getTable('catalog_product_bundle_selection_price'),
166  [
167  'selection_id = ?' => $item->getSelectionId(),
168  'website_id = ?' => $item->getWebsiteId(),
169  'parent_product_id = ?' => $item->getParentProductId(),
170  ]
171  );
172  } else {
173  $values = [
174  'selection_id' => $item->getSelectionId(),
175  'website_id' => $item->getWebsiteId(),
176  'selection_price_type' => $item->getSelectionPriceType(),
177  'selection_price_value' => $item->getSelectionPriceValue(),
178  'parent_product_id' => $item->getParentProductId(),
179  ];
180  $connection->insertOnDuplicate(
181  $this->getTable('catalog_product_bundle_selection_price'),
182  $values,
183  ['selection_price_type', 'selection_price_value']
184  );
185  }
186  }
187 
192  public function save(\Magento\Framework\Model\AbstractModel $object)
193  {
194  $this->entityManager->save($object);
195 
196  return $this;
197  }
198 }
$values
Definition: options.phtml:88
save(\Magento\Framework\Model\AbstractModel $object)
Definition: Selection.php:192
__construct(Context $context, MetadataPool $metadataPool, $connectionName=null, EntityManager $entityManager=null)
Definition: Selection.php:41
$connection
Definition: bulk.php:13
getChildrenIds($parentId, $required=true)
Definition: Selection.php:78
$required
Definition: wrapper.phtml:8