History.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Block\Order;
  7. use \Magento\Framework\App\ObjectManager;
  8. use \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
  9. /**
  10. * Sales order history block
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class History extends \Magento\Framework\View\Element\Template
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $_template = 'Magento_Sales::order/history.phtml';
  21. /**
  22. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
  23. */
  24. protected $_orderCollectionFactory;
  25. /**
  26. * @var \Magento\Customer\Model\Session
  27. */
  28. protected $_customerSession;
  29. /**
  30. * @var \Magento\Sales\Model\Order\Config
  31. */
  32. protected $_orderConfig;
  33. /**
  34. * @var \Magento\Sales\Model\ResourceModel\Order\Collection
  35. */
  36. protected $orders;
  37. /**
  38. * @var CollectionFactoryInterface
  39. */
  40. private $orderCollectionFactory;
  41. /**
  42. * @param \Magento\Framework\View\Element\Template\Context $context
  43. * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
  44. * @param \Magento\Customer\Model\Session $customerSession
  45. * @param \Magento\Sales\Model\Order\Config $orderConfig
  46. * @param array $data
  47. */
  48. public function __construct(
  49. \Magento\Framework\View\Element\Template\Context $context,
  50. \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
  51. \Magento\Customer\Model\Session $customerSession,
  52. \Magento\Sales\Model\Order\Config $orderConfig,
  53. array $data = []
  54. ) {
  55. $this->_orderCollectionFactory = $orderCollectionFactory;
  56. $this->_customerSession = $customerSession;
  57. $this->_orderConfig = $orderConfig;
  58. parent::__construct($context, $data);
  59. }
  60. /**
  61. * @return void
  62. */
  63. protected function _construct()
  64. {
  65. parent::_construct();
  66. $this->pageConfig->getTitle()->set(__('My Orders'));
  67. }
  68. /**
  69. * @return CollectionFactoryInterface
  70. *
  71. * @deprecated 100.1.1
  72. */
  73. private function getOrderCollectionFactory()
  74. {
  75. if ($this->orderCollectionFactory === null) {
  76. $this->orderCollectionFactory = ObjectManager::getInstance()->get(CollectionFactoryInterface::class);
  77. }
  78. return $this->orderCollectionFactory;
  79. }
  80. /**
  81. * @return bool|\Magento\Sales\Model\ResourceModel\Order\Collection
  82. */
  83. public function getOrders()
  84. {
  85. if (!($customerId = $this->_customerSession->getCustomerId())) {
  86. return false;
  87. }
  88. if (!$this->orders) {
  89. $this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
  90. '*'
  91. )->addFieldToFilter(
  92. 'status',
  93. ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
  94. )->setOrder(
  95. 'created_at',
  96. 'desc'
  97. );
  98. }
  99. return $this->orders;
  100. }
  101. /**
  102. * @return $this
  103. */
  104. protected function _prepareLayout()
  105. {
  106. parent::_prepareLayout();
  107. if ($this->getOrders()) {
  108. $pager = $this->getLayout()->createBlock(
  109. \Magento\Theme\Block\Html\Pager::class,
  110. 'sales.order.history.pager'
  111. )->setCollection(
  112. $this->getOrders()
  113. );
  114. $this->setChild('pager', $pager);
  115. $this->getOrders()->load();
  116. }
  117. return $this;
  118. }
  119. /**
  120. * @return string
  121. */
  122. public function getPagerHtml()
  123. {
  124. return $this->getChildHtml('pager');
  125. }
  126. /**
  127. * @param object $order
  128. * @return string
  129. */
  130. public function getViewUrl($order)
  131. {
  132. return $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
  133. }
  134. /**
  135. * @param object $order
  136. * @return string
  137. */
  138. public function getTrackUrl($order)
  139. {
  140. return $this->getUrl('sales/order/track', ['order_id' => $order->getId()]);
  141. }
  142. /**
  143. * @param object $order
  144. * @return string
  145. */
  146. public function getReorderUrl($order)
  147. {
  148. return $this->getUrl('sales/order/reorder', ['order_id' => $order->getId()]);
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function getBackUrl()
  154. {
  155. return $this->getUrl('customer/account/');
  156. }
  157. }