Collection.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\ResourceModel\Quote\Item\Option;
  7. use Magento\Quote\Model\Quote\Item;
  8. /**
  9. * Item option collection
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * Array of option ids grouped by item id
  17. *
  18. * @var array
  19. */
  20. protected $_optionsByItem = [];
  21. /**
  22. * Array of option ids grouped by product id
  23. *
  24. * @var array
  25. */
  26. protected $_optionsByProduct = [];
  27. /**
  28. * Define resource model for collection
  29. *
  30. * @return void
  31. */
  32. protected function _construct()
  33. {
  34. $this->_init(
  35. \Magento\Quote\Model\Quote\Item\Option::class,
  36. \Magento\Quote\Model\ResourceModel\Quote\Item\Option::class
  37. );
  38. }
  39. /**
  40. * Fill array of options by item and product
  41. *
  42. * @return $this
  43. */
  44. protected function _afterLoad()
  45. {
  46. parent::_afterLoad();
  47. foreach ($this as $option) {
  48. $optionId = $option->getId();
  49. $itemId = $option->getItemId();
  50. $productId = $option->getProductId();
  51. if (isset($this->_optionsByItem[$itemId])) {
  52. $this->_optionsByItem[$itemId][] = $optionId;
  53. } else {
  54. $this->_optionsByItem[$itemId] = [$optionId];
  55. }
  56. if (isset($this->_optionsByProduct[$productId])) {
  57. $this->_optionsByProduct[$productId][] = $optionId;
  58. } else {
  59. $this->_optionsByProduct[$productId] = [$optionId];
  60. }
  61. }
  62. return $this;
  63. }
  64. /**
  65. * Apply quote item(s) filter to collection
  66. *
  67. * @param int|array|Item $item
  68. * @return $this
  69. */
  70. public function addItemFilter($item)
  71. {
  72. if (empty($item)) {
  73. $this->_totalRecords = 0;
  74. $this->_setIsLoaded(true);
  75. //$this->addFieldToFilter('item_id', '');
  76. } elseif (is_array($item)) {
  77. $this->addFieldToFilter('item_id', ['in' => $item]);
  78. } elseif ($item instanceof Item) {
  79. $this->addFieldToFilter('item_id', $item->getId());
  80. } else {
  81. $this->addFieldToFilter('item_id', $item);
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Get array of all product ids
  87. *
  88. * @return array
  89. */
  90. public function getProductIds()
  91. {
  92. $this->load();
  93. return array_keys($this->_optionsByProduct);
  94. }
  95. /**
  96. * Get all option for item
  97. *
  98. * @param mixed $item
  99. * @return array
  100. */
  101. public function getOptionsByItem($item)
  102. {
  103. if ($item instanceof Item) {
  104. $itemId = $item->getId();
  105. } else {
  106. $itemId = $item;
  107. }
  108. $this->load();
  109. $options = [];
  110. if (isset($this->_optionsByItem[$itemId])) {
  111. foreach ($this->_optionsByItem[$itemId] as $optionId) {
  112. $options[] = $this->_items[$optionId];
  113. }
  114. }
  115. return $options;
  116. }
  117. /**
  118. * Get all option for item
  119. *
  120. * @param int | \Magento\Catalog\Model\Product $product
  121. * @return array
  122. */
  123. public function getOptionsByProduct($product)
  124. {
  125. if ($product instanceof \Magento\Catalog\Model\Product) {
  126. $productId = $product->getId();
  127. } else {
  128. $productId = $product;
  129. }
  130. $this->load();
  131. $options = [];
  132. if (isset($this->_optionsByProduct[$productId])) {
  133. foreach ($this->_optionsByProduct[$productId] as $optionId) {
  134. $options[] = $this->_items[$optionId];
  135. }
  136. }
  137. return $options;
  138. }
  139. }