Cart.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Model\ResourceModel;
  7. /**
  8. * Resource model for Checkout Cart
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Cart extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * Model initialization
  16. *
  17. * @return void
  18. * @codeCoverageIgnore
  19. */
  20. protected function _construct()
  21. {
  22. $this->_init('quote', 'entity_id');
  23. }
  24. /**
  25. * Fetch items summary
  26. *
  27. * @param int $quoteId
  28. * @return array
  29. */
  30. public function fetchItemsSummary($quoteId)
  31. {
  32. $connection = $this->getConnection();
  33. $select = $connection->select()->from(
  34. ['q' => $this->getTable('quote')],
  35. ['items_qty', 'items_count']
  36. )->where(
  37. 'q.entity_id = :quote_id'
  38. );
  39. $result = $connection->fetchRow($select, [':quote_id' => $quoteId]);
  40. return $result ? $result : ['items_qty' => 0, 'items_count' => 0];
  41. }
  42. /**
  43. * Fetch items
  44. *
  45. * @param int $quoteId
  46. * @return array
  47. */
  48. public function fetchItems($quoteId)
  49. {
  50. $connection = $this->getConnection();
  51. $select = $connection->select()->from(
  52. ['qi' => $this->getTable('quote_item')],
  53. ['id' => 'item_id', 'product_id', 'super_product_id', 'qty', 'created_at']
  54. )->where(
  55. 'qi.quote_id = :quote_id'
  56. );
  57. return $connection->fetchAll($select, [':quote_id' => $quoteId]);
  58. }
  59. /**
  60. * Make collection not to load products that are in specified quote
  61. *
  62. * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
  63. * @param int $quoteId
  64. * @return $this
  65. */
  66. public function addExcludeProductFilter($collection, $quoteId)
  67. {
  68. $connection = $this->getConnection();
  69. $exclusionSelect = $connection->select()->from(
  70. $this->getTable('quote_item'),
  71. ['product_id']
  72. )->where(
  73. 'quote_id = ?',
  74. $quoteId
  75. );
  76. $condition = $connection->prepareSqlCondition('e.entity_id', ['nin' => $exclusionSelect]);
  77. $collection->getSelect()->where($condition);
  78. return $this;
  79. }
  80. }