Bundle.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /**
  8. * Bundle Resource Model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Bundle extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * @var \Magento\Catalog\Model\ResourceModel\Product\Relation
  17. */
  18. protected $_productRelation;
  19. /**
  20. * @var \Magento\Quote\Model\ResourceModel\Quote
  21. */
  22. protected $quoteResource;
  23. /**
  24. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  25. * @param \Magento\Catalog\Model\ResourceModel\Product\Relation $productRelation
  26. * @param \Magento\Quote\Model\ResourceModel\Quote $quoteResource
  27. * @param string $connectionName
  28. */
  29. public function __construct(
  30. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  31. \Magento\Catalog\Model\ResourceModel\Product\Relation $productRelation,
  32. \Magento\Quote\Model\ResourceModel\Quote $quoteResource,
  33. $connectionName = null
  34. ) {
  35. parent::__construct($context, $connectionName);
  36. $this->_productRelation = $productRelation;
  37. $this->quoteResource = $quoteResource;
  38. }
  39. /**
  40. * Resource initialization
  41. *
  42. * @return void
  43. */
  44. protected function _construct()
  45. {
  46. $this->_init('catalog_product_entity', 'entity_id');
  47. }
  48. /**
  49. * Preparing select for getting selection's raw data by product id
  50. * also can be specified extra parameter for limit which columns should be selected
  51. *
  52. * @param int $productId
  53. * @param array $columns
  54. * @return \Magento\Framework\DB\Select
  55. */
  56. protected function _getSelect($productId, $columns = [])
  57. {
  58. return $this->getConnection()->select()->from(
  59. ["bo" => $this->getTable('catalog_product_bundle_option')],
  60. ['type', 'option_id']
  61. )->where(
  62. "bo.parent_id = ?",
  63. $productId
  64. )->where(
  65. "bo.required = 1"
  66. )->joinLeft(
  67. ["bs" => $this->getTable('catalog_product_bundle_selection')],
  68. "bs.option_id = bo.option_id AND bs.parent_product_id = bo.parent_id",
  69. $columns
  70. );
  71. }
  72. /**
  73. * Retrieve selection data for specified product id
  74. *
  75. * @param int $productId
  76. * @return array
  77. */
  78. public function getSelectionsData($productId)
  79. {
  80. return $this->getConnection()->fetchAll($this->_getSelect($productId, ["*"]));
  81. }
  82. /**
  83. * Removing all quote items for specified product
  84. *
  85. * @param int $productId
  86. * @return void
  87. */
  88. public function dropAllQuoteChildItems($productId)
  89. {
  90. $connection = $this->quoteResource->getConnection();
  91. $select = $connection->select();
  92. $quoteItemIds = $connection->fetchCol(
  93. $select->from(
  94. $this->getTable('quote_item'),
  95. ['item_id']
  96. )->where(
  97. 'product_id = :product_id'
  98. ),
  99. ['product_id' => $productId]
  100. );
  101. if ($quoteItemIds) {
  102. $connection->delete(
  103. $this->getTable('quote_item'),
  104. ['parent_item_id IN(?)' => $quoteItemIds]
  105. );
  106. }
  107. }
  108. /**
  109. * Removes specified selections by ids for specified product id
  110. *
  111. * @param int $productId
  112. * @param array $ids
  113. * @return void
  114. */
  115. public function dropAllUnneededSelections($productId, $ids)
  116. {
  117. $where = ['parent_product_id = ?' => $productId];
  118. if (!empty($ids)) {
  119. $where['selection_id NOT IN (?) '] = $ids;
  120. }
  121. $this->getConnection()->delete($this->getTable('catalog_product_bundle_selection'), $where);
  122. }
  123. /**
  124. * Save product relations
  125. *
  126. * @param int $parentId
  127. * @param array $childIds
  128. * @return $this
  129. */
  130. public function saveProductRelations($parentId, $childIds)
  131. {
  132. $this->_productRelation->processRelations($parentId, $childIds);
  133. return $this;
  134. }
  135. /**
  136. * Add product relation (duplicate will be updated)
  137. *
  138. * @param int $parentId
  139. * @param int $childId
  140. * @return $this
  141. * @since 100.1.0
  142. */
  143. public function addProductRelation($parentId, $childId)
  144. {
  145. $this->_productRelation->addRelation($parentId, $childId);
  146. return $this;
  147. }
  148. /**
  149. * Add product relations
  150. *
  151. * @param int $parentId
  152. * @param array $childIds
  153. * @return $this
  154. */
  155. public function addProductRelations($parentId, $childIds)
  156. {
  157. $this->_productRelation->addRelations($parentId, $childIds);
  158. return $this;
  159. }
  160. /**
  161. * Remove product relations
  162. *
  163. * @param int $parentId
  164. * @param array $childIds
  165. * @return $this
  166. */
  167. public function removeProductRelations($parentId, $childIds)
  168. {
  169. $this->_productRelation->removeRelations($parentId, $childIds);
  170. return $this;
  171. }
  172. }