Collection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Address\Rate;
  7. /**
  8. * Quote addresses shipping rates collection
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Collection extends \Magento\Framework\Model\ResourceModel\Db\VersionControl\Collection
  13. {
  14. /**
  15. * Whether to load fixed items only
  16. *
  17. * @var bool
  18. */
  19. protected $_allowFixedOnly = false;
  20. /**
  21. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  22. * @param \Psr\Log\LoggerInterface $logger
  23. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  24. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  25. * @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
  26. * @param \Magento\Shipping\Model\CarrierFactoryInterface $carrierFactory
  27. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  28. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  29. */
  30. public function __construct(
  31. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  32. \Psr\Log\LoggerInterface $logger,
  33. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  34. \Magento\Framework\Event\ManagerInterface $eventManager,
  35. \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
  36. \Magento\Shipping\Model\CarrierFactoryInterface $carrierFactory,
  37. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  38. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  39. ) {
  40. parent::__construct(
  41. $entityFactory,
  42. $logger,
  43. $fetchStrategy,
  44. $eventManager,
  45. $entitySnapshot,
  46. $connection,
  47. $resource
  48. );
  49. $this->_carrierFactory = $carrierFactory;
  50. }
  51. /**
  52. * Resource initialization
  53. *
  54. * @return void
  55. */
  56. protected function _construct()
  57. {
  58. $this->_init(
  59. \Magento\Quote\Model\Quote\Address\Rate::class,
  60. \Magento\Quote\Model\ResourceModel\Quote\Address\Rate::class
  61. );
  62. }
  63. /**
  64. * Set filter by address id
  65. *
  66. * @param int $addressId
  67. * @return $this
  68. */
  69. public function setAddressFilter($addressId)
  70. {
  71. if ($addressId) {
  72. $this->addFieldToFilter('address_id', $addressId);
  73. } else {
  74. $this->_totalRecords = 0;
  75. $this->_setIsLoaded(true);
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Setter for loading fixed items only
  81. *
  82. * @param bool $value
  83. * @return $this
  84. */
  85. public function setFixedOnlyFilter($value)
  86. {
  87. $this->_allowFixedOnly = (bool)$value;
  88. return $this;
  89. }
  90. /**
  91. * Don't add item to the collection if only fixed are allowed and its carrier is not fixed
  92. *
  93. * @param \Magento\Quote\Model\Quote\Address\Rate $rate
  94. * @return $this
  95. */
  96. public function addItem(\Magento\Framework\DataObject $rate)
  97. {
  98. $carrier = $this->_carrierFactory->get($rate->getCarrier());
  99. if ($this->_allowFixedOnly && (!$carrier || !$carrier->isFixed())) {
  100. return $this;
  101. }
  102. return parent::addItem($rate);
  103. }
  104. }