Collection.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate;
  7. /**
  8. * Shipping table rates collection
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * Directory/country table name
  17. *
  18. * @var string
  19. */
  20. protected $_countryTable;
  21. /**
  22. * Directory/country_region table name
  23. *
  24. * @var string
  25. */
  26. protected $_regionTable;
  27. /**
  28. * Define resource model and item
  29. *
  30. * @return void
  31. */
  32. protected function _construct()
  33. {
  34. $this->_init(
  35. \Magento\OfflineShipping\Model\Carrier\Tablerate::class,
  36. \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate::class
  37. );
  38. $this->_countryTable = $this->getTable('directory_country');
  39. $this->_regionTable = $this->getTable('directory_country_region');
  40. }
  41. /**
  42. * Initialize select, add country iso3 code and region name
  43. *
  44. * @return void
  45. */
  46. public function _initSelect()
  47. {
  48. parent::_initSelect();
  49. $this->_select->joinLeft(
  50. ['country_table' => $this->_countryTable],
  51. 'country_table.country_id = main_table.dest_country_id',
  52. ['dest_country' => 'iso3_code']
  53. )->joinLeft(
  54. ['region_table' => $this->_regionTable],
  55. 'region_table.region_id = main_table.dest_region_id',
  56. ['dest_region' => 'code']
  57. );
  58. $this->addOrder('dest_country', self::SORT_ORDER_ASC);
  59. $this->addOrder('dest_region', self::SORT_ORDER_ASC);
  60. $this->addOrder('dest_zip', self::SORT_ORDER_ASC);
  61. $this->addOrder('condition_value', self::SORT_ORDER_ASC);
  62. }
  63. /**
  64. * Add website filter to collection
  65. *
  66. * @param int $websiteId
  67. * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Collection
  68. */
  69. public function setWebsiteFilter($websiteId)
  70. {
  71. return $this->addFieldToFilter('website_id', $websiteId);
  72. }
  73. /**
  74. * Add condition name (code) filter to collection
  75. *
  76. * @param string $conditionName
  77. * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Collection
  78. */
  79. public function setConditionFilter($conditionName)
  80. {
  81. return $this->addFieldToFilter('condition_name', $conditionName);
  82. }
  83. /**
  84. * Add country filter to collection
  85. *
  86. * @param string $countryId
  87. * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Collection
  88. */
  89. public function setCountryFilter($countryId)
  90. {
  91. return $this->addFieldToFilter('dest_country_id', $countryId);
  92. }
  93. }