Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Bundle.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  protected $_productRelation;
20 
24  protected $quoteResource;
25 
32  public function __construct(
33  \Magento\Framework\Model\ResourceModel\Db\Context $context,
34  \Magento\Catalog\Model\ResourceModel\Product\Relation $productRelation,
36  $connectionName = null
37  ) {
38  parent::__construct($context, $connectionName);
39  $this->_productRelation = $productRelation;
40  $this->quoteResource = $quoteResource;
41  }
42 
48  protected function _construct()
49  {
50  $this->_init('catalog_product_entity', 'entity_id');
51  }
52 
61  protected function _getSelect($productId, $columns = [])
62  {
63  return $this->getConnection()->select()->from(
64  ["bo" => $this->getTable('catalog_product_bundle_option')],
65  ['type', 'option_id']
66  )->where(
67  "bo.parent_id = ?",
69  )->where(
70  "bo.required = 1"
71  )->joinLeft(
72  ["bs" => $this->getTable('catalog_product_bundle_selection')],
73  "bs.option_id = bo.option_id AND bs.parent_product_id = bo.parent_id",
74  $columns
75  );
76  }
77 
84  public function getSelectionsData($productId)
85  {
86  return $this->getConnection()->fetchAll($this->_getSelect($productId, ["*"]));
87  }
88 
96  {
97  $connection = $this->quoteResource->getConnection();
98  $select = $connection->select();
99  $quoteItemIds = $connection->fetchCol(
100  $select->from(
101  $this->getTable('quote_item'),
102  ['item_id']
103  )->where(
104  'product_id = :product_id'
105  ),
106  ['product_id' => $productId]
107  );
108 
109  if ($quoteItemIds) {
110  $connection->delete(
111  $this->getTable('quote_item'),
112  ['parent_item_id IN(?)' => $quoteItemIds]
113  );
114  }
115  }
116 
124  public function dropAllUnneededSelections($productId, $ids)
125  {
126  $where = ['parent_product_id = ?' => $productId];
127  if (!empty($ids)) {
128  $where['selection_id NOT IN (?) '] = $ids;
129  }
130  $this->getConnection()->delete($this->getTable('catalog_product_bundle_selection'), $where);
131  }
132 
140  public function saveProductRelations($parentId, $childIds)
141  {
142  $this->_productRelation->processRelations($parentId, $childIds);
143 
144  return $this;
145  }
146 
155  public function addProductRelation($parentId, $childId)
156  {
157  $this->_productRelation->addRelation($parentId, $childId);
158  return $this;
159  }
160 
168  public function addProductRelations($parentId, $childIds)
169  {
170  $this->_productRelation->addRelations($parentId, $childIds);
171  return $this;
172  }
173 
181  public function removeProductRelations($parentId, $childIds)
182  {
183  $this->_productRelation->removeRelations($parentId, $childIds);
184  return $this;
185  }
186 }
addProductRelation($parentId, $childId)
Definition: Bundle.php:155
$columns
Definition: default.phtml:15
addProductRelations($parentId, $childIds)
Definition: Bundle.php:168
saveProductRelations($parentId, $childIds)
Definition: Bundle.php:140
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Catalog\Model\ResourceModel\Product\Relation $productRelation, \Magento\Quote\Model\ResourceModel\Quote $quoteResource, $connectionName=null)
Definition: Bundle.php:32
dropAllUnneededSelections($productId, $ids)
Definition: Bundle.php:124
removeProductRelations($parentId, $childIds)
Definition: Bundle.php:181
_getSelect($productId, $columns=[])
Definition: Bundle.php:61
$connection
Definition: bulk.php:13