Grid.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate;
  7. /**
  8. * Shipping carrier table rate grid block
  9. * WARNING: This grid used for export table rates
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
  14. {
  15. /**
  16. * Website filter
  17. *
  18. * @var int
  19. */
  20. protected $_websiteId;
  21. /**
  22. * Condition filter
  23. *
  24. * @var string
  25. */
  26. protected $_conditionName;
  27. /**
  28. * @var \Magento\OfflineShipping\Model\Carrier\Tablerate
  29. */
  30. protected $_tablerate;
  31. /**
  32. * @var \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\CollectionFactory
  33. */
  34. protected $_collectionFactory;
  35. /**
  36. * @param \Magento\Backend\Block\Template\Context $context
  37. * @param \Magento\Backend\Helper\Data $backendHelper
  38. * @param \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\CollectionFactory $collectionFactory
  39. * @param \Magento\OfflineShipping\Model\Carrier\Tablerate $tablerate
  40. * @param array $data
  41. */
  42. public function __construct(
  43. \Magento\Backend\Block\Template\Context $context,
  44. \Magento\Backend\Helper\Data $backendHelper,
  45. \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\CollectionFactory $collectionFactory,
  46. \Magento\OfflineShipping\Model\Carrier\Tablerate $tablerate,
  47. array $data = []
  48. ) {
  49. $this->_collectionFactory = $collectionFactory;
  50. $this->_tablerate = $tablerate;
  51. parent::__construct($context, $backendHelper, $data);
  52. }
  53. /**
  54. * Define grid properties
  55. *
  56. * @return void
  57. */
  58. protected function _construct()
  59. {
  60. parent::_construct();
  61. $this->setId('shippingTablerateGrid');
  62. $this->_exportPageSize = 10000;
  63. }
  64. /**
  65. * Set current website
  66. *
  67. * @param int $websiteId
  68. * @return $this
  69. */
  70. public function setWebsiteId($websiteId)
  71. {
  72. $this->_websiteId = $this->_storeManager->getWebsite($websiteId)->getId();
  73. return $this;
  74. }
  75. /**
  76. * Retrieve current website id
  77. *
  78. * @return int
  79. */
  80. public function getWebsiteId()
  81. {
  82. if ($this->_websiteId === null) {
  83. $this->_websiteId = $this->_storeManager->getWebsite()->getId();
  84. }
  85. return $this->_websiteId;
  86. }
  87. /**
  88. * Set current website
  89. *
  90. * @param string $name
  91. * @return $this
  92. */
  93. public function setConditionName($name)
  94. {
  95. $this->_conditionName = $name;
  96. return $this;
  97. }
  98. /**
  99. * Retrieve current website id
  100. *
  101. * @return int
  102. */
  103. public function getConditionName()
  104. {
  105. return $this->_conditionName;
  106. }
  107. /**
  108. * Prepare shipping table rate collection
  109. *
  110. * @return \Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid
  111. */
  112. protected function _prepareCollection()
  113. {
  114. /** @var $collection \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Collection */
  115. $collection = $this->_collectionFactory->create();
  116. $collection->setConditionFilter($this->getConditionName())->setWebsiteFilter($this->getWebsiteId());
  117. $this->setCollection($collection);
  118. return parent::_prepareCollection();
  119. }
  120. /**
  121. * Prepare table columns
  122. *
  123. * @return \Magento\Backend\Block\Widget\Grid\Extended
  124. */
  125. protected function _prepareColumns()
  126. {
  127. $this->addColumn(
  128. 'dest_country',
  129. ['header' => __('Country'), 'index' => 'dest_country', 'default' => '*']
  130. );
  131. $this->addColumn(
  132. 'dest_region',
  133. ['header' => __('Region/State'), 'index' => 'dest_region', 'default' => '*']
  134. );
  135. $this->addColumn(
  136. 'dest_zip',
  137. ['header' => __('Zip/Postal Code'), 'index' => 'dest_zip', 'default' => '*']
  138. );
  139. $label = $this->_tablerate->getCode('condition_name_short', $this->getConditionName());
  140. $this->addColumn('condition_value', ['header' => $label, 'index' => 'condition_value']);
  141. $this->addColumn('price', ['header' => __('Shipping Price'), 'index' => 'price']);
  142. return parent::_prepareColumns();
  143. }
  144. }