Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Quote.php
Go to the documentation of this file.
1 <?php
8 
13 
17 class Quote extends AbstractDb
18 {
22  protected $sequenceManager;
23 
31  public function __construct(
32  \Magento\Framework\Model\ResourceModel\Db\Context $context,
36  $connectionName = null
37  ) {
38  parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
39  $this->sequenceManager = $sequenceManager;
40  }
41 
47  protected function _construct()
48  {
49  $this->_init('quote', 'entity_id');
50  }
51 
60  protected function _getLoadSelect($field, $value, $object)
61  {
62  $select = parent::_getLoadSelect($field, $value, $object);
63  $storeIds = $object->getSharedStoreIds();
64  if ($storeIds) {
65  if ($storeIds != ['*']) {
66  $select->where('store_id IN (?)', $storeIds);
67  }
68  } else {
72  $select->where('store_id < ?', 0);
73  }
74 
75  return $select;
76  }
77 
86  {
87  $connection = $this->getConnection();
88  $select = $this->_getLoadSelect(
89  'customer_id',
91  $quote
92  )->where(
93  'is_active = ?',
94  1
95  )->order(
96  'updated_at ' . \Magento\Framework\DB\Select::SQL_DESC
97  )->limit(
98  1
99  );
100 
101  $data = $connection->fetchRow($select);
102 
103  if ($data) {
104  $quote->setData($data);
105  }
106 
107  $this->_afterLoad($quote);
108 
109  return $this;
110  }
111 
119  public function loadActive($quote, $quoteId)
120  {
121  $connection = $this->getConnection();
122  $select = $this->_getLoadSelect('entity_id', $quoteId, $quote)->where('is_active = ?', 1);
123 
124  $data = $connection->fetchRow($select);
125  if ($data) {
126  $quote->setData($data);
127  }
128 
129  $this->_afterLoad($quote);
130 
131  return $this;
132  }
133 
142  {
143  $connection = $this->getConnection();
144  if ($connection) {
145  $select = parent::_getLoadSelect('entity_id', $quoteId, $quote);
146 
147  $data = $connection->fetchRow($select);
148 
149  if ($data) {
150  $quote->setData($data);
151  }
152  }
153 
154  $this->_afterLoad($quote);
155  return $this;
156  }
157 
164  public function getReservedOrderId($quote)
165  {
166  return $this->sequenceManager->getSequence(
167  \Magento\Sales\Model\Order::ENTITY,
168  $quote->getStoreId()
169  )
170  ->getNextValue();
171  }
172 
179  {
180  $tableQuote = $this->getTable('quote');
181  $subSelect = $this->getConnection()->select()->from(
182  ['t2' => $this->getTable('quote_item')],
183  ['entity_id' => 'quote_id']
184  )->from(
185  ['t3' => $this->getTable('catalogrule_product_price')],
186  []
187  )->where(
188  't2.product_id = t3.product_id'
189  )->group(
190  'quote_id'
191  );
192 
193  $select = $this->getConnection()->select()->join(
194  ['t2' => $subSelect],
195  't1.entity_id = t2.entity_id',
196  ['trigger_recollect' => new \Zend_Db_Expr('1')]
197  );
198 
199  $updateQuery = $select->crossUpdateFromSelect(['t1' => $tableQuote]);
200 
201  $this->getConnection()->query($updateQuery);
202 
203  return $this;
204  }
205 
213  {
214  $productId = (int)$product->getId();
215  if (!$productId) {
216  return $this;
217  }
218  $connection = $this->getConnection();
219  $subSelect = $connection->select();
220  $conditionCheck = $connection->quoteIdentifier('q.items_count') . " > 0";
221  $conditionTrue = $connection->quoteIdentifier('q.items_count') . ' - 1';
222  $ifSql = "IF (" . $conditionCheck . "," . $conditionTrue . ", 0)";
223 
224  $subSelect->from(
225  false,
226  [
227  'items_qty' => new \Zend_Db_Expr(
228  $connection->quoteIdentifier('q.items_qty') . ' - ' . $connection->quoteIdentifier('qi.qty')
229  ),
230  'items_count' => new \Zend_Db_Expr($ifSql)
231  ]
232  )->join(
233  ['qi' => $this->getTable('quote_item')],
234  implode(
235  ' AND ',
236  [
237  'q.entity_id = qi.quote_id',
238  'qi.parent_item_id IS NULL',
239  $connection->quoteInto('qi.product_id = ?', $productId)
240  ]
241  ),
242  []
243  );
244 
245  $updateQuery = $connection->updateFromSelect($subSelect, ['q' => $this->getTable('quote')]);
246 
247  $connection->query($updateQuery);
248 
249  return $this;
250  }
251 
263  {
264  return $this->subtractProductFromQuotes($product);
265  }
266 
274  {
275  $tableQuote = $this->getTable('quote');
276  $tableItem = $this->getTable('quote_item');
277  $subSelect = $this->getConnection()->select()->from(
278  $tableItem,
279  ['entity_id' => 'quote_id']
280  )->where(
281  'product_id IN ( ? )',
283  )->group(
284  'quote_id'
285  );
286 
287  $select = $this->getConnection()->select()->join(
288  ['t2' => $subSelect],
289  't1.entity_id = t2.entity_id',
290  ['trigger_recollect' => new \Zend_Db_Expr('1')]
291  );
292  $updateQuery = $select->crossUpdateFromSelect(['t1' => $tableQuote]);
293  $this->getConnection()->query($updateQuery);
294 
295  return $this;
296  }
297 
301  public function save(\Magento\Framework\Model\AbstractModel $object)
302  {
303  if (!$object->isPreventSaving()) {
304  return parent::save($object);
305  }
306  }
307 }
$quote
loadByIdWithoutStore($quote, $quoteId)
Definition: Quote.php:141
save(\Magento\Framework\Model\AbstractModel $object)
Definition: Quote.php:301
loadByCustomerId($quote, $customerId)
Definition: Quote.php:85
$value
Definition: gender.phtml:16
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:43
const SQL_DESC
Definition: Select.php:82
_getLoadSelect($field, $value, $object)
Definition: Quote.php:60
$connection
Definition: bulk.php:13
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, Snapshot $entitySnapshot, RelationComposite $entityRelationComposite, Manager $sequenceManager, $connectionName=null)
Definition: Quote.php:31