Selection.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model\ResourceModel;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\Framework\EntityManager\EntityManager;
  11. use Magento\Framework\Model\ResourceModel\Db\Context;
  12. /**
  13. * Bundle Selection Resource Model
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Selection extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  19. {
  20. /**
  21. * @var MetadataPool
  22. * @since 100.1.0
  23. */
  24. protected $metadataPool;
  25. /**
  26. * @var EntityManager
  27. */
  28. private $entityManager;
  29. /**
  30. * Selection constructor.
  31. *
  32. * @param Context $context
  33. * @param MetadataPool $metadataPool
  34. * @param null|string $connectionName
  35. * @param EntityManager|null $entityManager
  36. */
  37. public function __construct(
  38. Context $context,
  39. MetadataPool $metadataPool,
  40. $connectionName = null,
  41. EntityManager $entityManager = null
  42. ) {
  43. parent::__construct(
  44. $context,
  45. $connectionName
  46. );
  47. $this->metadataPool = $metadataPool;
  48. $this->entityManager = $entityManager
  49. ?: ObjectManager::getInstance()->get(EntityManager::class);
  50. }
  51. /**
  52. * Define main table and id field
  53. *
  54. * @return void
  55. */
  56. protected function _construct()
  57. {
  58. $this->_init('catalog_product_bundle_selection', 'selection_id');
  59. }
  60. /**
  61. * Retrieve Required children ids
  62. * Return grouped array, ex array(
  63. * group => array(ids)
  64. * )
  65. *
  66. * @param int $parentId
  67. * @param bool $required
  68. * @return array
  69. */
  70. public function getChildrenIds($parentId, $required = true)
  71. {
  72. $childrenIds = [];
  73. $notRequired = [];
  74. $connection = $this->getConnection();
  75. $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
  76. $select = $connection->select()->from(
  77. ['tbl_selection' => $this->getMainTable()],
  78. ['product_id', 'parent_product_id', 'option_id']
  79. )->join(
  80. ['e' => $this->getTable('catalog_product_entity')],
  81. 'e.entity_id = tbl_selection.product_id AND e.required_options=0',
  82. []
  83. )->join(
  84. ['parent' => $this->getTable('catalog_product_entity')],
  85. 'tbl_selection.parent_product_id = parent.' . $linkField
  86. )->join(
  87. ['tbl_option' => $this->getTable('catalog_product_bundle_option')],
  88. 'tbl_option.option_id = tbl_selection.option_id',
  89. ['required']
  90. )->where(
  91. 'parent.entity_id = :parent_id'
  92. );
  93. foreach ($connection->fetchAll($select, ['parent_id' => $parentId]) as $row) {
  94. if ($row['required']) {
  95. $childrenIds[$row['option_id']][$row['product_id']] = $row['product_id'];
  96. } else {
  97. $notRequired[$row['option_id']][$row['product_id']] = $row['product_id'];
  98. }
  99. }
  100. if (!$required) {
  101. $childrenIds = array_merge($childrenIds, $notRequired);
  102. } else {
  103. if (!$childrenIds) {
  104. foreach ($notRequired as $groupedChildrenIds) {
  105. foreach ($groupedChildrenIds as $childId) {
  106. $childrenIds[0][$childId] = $childId;
  107. }
  108. }
  109. }
  110. if (!$childrenIds) {
  111. $childrenIds = [[]];
  112. }
  113. }
  114. return $childrenIds;
  115. }
  116. /**
  117. * Retrieve array of related bundle product ids by selection product id(s)
  118. *
  119. * @param int|array $childId
  120. * @return array
  121. */
  122. public function getParentIdsByChild($childId)
  123. {
  124. $connection = $this->getConnection();
  125. $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
  126. $select = $connection->select()->distinct(
  127. true
  128. )->from(
  129. $this->getMainTable(),
  130. ''
  131. )->join(
  132. ['e' => $this->metadataPool->getMetadata(ProductInterface::class)->getEntityTable()],
  133. 'e.' . $metadata->getLinkField() . ' = ' . $this->getMainTable() . '.parent_product_id',
  134. ['e.entity_id as parent_product_id']
  135. )->where(
  136. $this->getMainTable() . '.product_id IN(?)',
  137. $childId
  138. );
  139. return $connection->fetchCol($select);
  140. }
  141. /**
  142. * Save bundle item price per website
  143. *
  144. * @param \Magento\Bundle\Model\Selection $item
  145. * @return void
  146. */
  147. public function saveSelectionPrice($item)
  148. {
  149. $connection = $this->getConnection();
  150. if ($item->getDefaultPriceScope()) {
  151. $connection->delete(
  152. $this->getTable('catalog_product_bundle_selection_price'),
  153. [
  154. 'selection_id = ?' => $item->getSelectionId(),
  155. 'website_id = ?' => $item->getWebsiteId(),
  156. 'parent_product_id = ?' => $item->getParentProductId(),
  157. ]
  158. );
  159. } else {
  160. $values = [
  161. 'selection_id' => $item->getSelectionId(),
  162. 'website_id' => $item->getWebsiteId(),
  163. 'selection_price_type' => $item->getSelectionPriceType(),
  164. 'selection_price_value' => $item->getSelectionPriceValue(),
  165. 'parent_product_id' => $item->getParentProductId(),
  166. ];
  167. $connection->insertOnDuplicate(
  168. $this->getTable('catalog_product_bundle_selection_price'),
  169. $values,
  170. ['selection_price_type', 'selection_price_value']
  171. );
  172. }
  173. }
  174. /**
  175. * {@inheritdoc}
  176. * @since 100.2.0
  177. */
  178. public function save(\Magento\Framework\Model\AbstractModel $object)
  179. {
  180. $this->entityManager->save($object);
  181. return $this;
  182. }
  183. }