Recent.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\View\Element\Template\Context;
  8. use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
  9. use Magento\Customer\Model\Session;
  10. use Magento\Sales\Model\Order\Config;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Magento\Framework\App\ObjectManager;
  13. /**
  14. * Sales order history block
  15. *
  16. * @api
  17. * @since 100.0.2
  18. */
  19. class Recent extends \Magento\Framework\View\Element\Template
  20. {
  21. /**
  22. * Limit of orders
  23. */
  24. const ORDER_LIMIT = 5;
  25. /**
  26. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
  27. */
  28. protected $_orderCollectionFactory;
  29. /**
  30. * @var \Magento\Customer\Model\Session
  31. */
  32. protected $_customerSession;
  33. /**
  34. * @var \Magento\Sales\Model\Order\Config
  35. */
  36. protected $_orderConfig;
  37. /**
  38. * @var \Magento\Store\Model\StoreManagerInterface
  39. */
  40. private $storeManager;
  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. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  48. */
  49. public function __construct(
  50. Context $context,
  51. CollectionFactory $orderCollectionFactory,
  52. Session $customerSession,
  53. Config $orderConfig,
  54. array $data = [],
  55. StoreManagerInterface $storeManager = null
  56. ) {
  57. $this->_orderCollectionFactory = $orderCollectionFactory;
  58. $this->_customerSession = $customerSession;
  59. $this->_orderConfig = $orderConfig;
  60. $this->_isScopePrivate = true;
  61. $this->storeManager = $storeManager ?: ObjectManager::getInstance()
  62. ->get(StoreManagerInterface::class);
  63. parent::__construct($context, $data);
  64. }
  65. /**
  66. * @return void
  67. */
  68. protected function _construct()
  69. {
  70. parent::_construct();
  71. $this->getRecentOrders();
  72. }
  73. /**
  74. * Get recently placed orders. By default they will be limited by 5.
  75. */
  76. private function getRecentOrders()
  77. {
  78. $orders = $this->_orderCollectionFactory->create()->addAttributeToSelect(
  79. '*'
  80. )->addAttributeToFilter(
  81. 'customer_id',
  82. $this->_customerSession->getCustomerId()
  83. )->addAttributeToFilter(
  84. 'store_id',
  85. $this->storeManager->getStore()->getId()
  86. )->addAttributeToFilter(
  87. 'status',
  88. ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
  89. )->addAttributeToSort(
  90. 'created_at',
  91. 'desc'
  92. )->setPageSize(
  93. self::ORDER_LIMIT
  94. )->load();
  95. $this->setOrders($orders);
  96. }
  97. /**
  98. * @param object $order
  99. * @return string
  100. */
  101. public function getViewUrl($order)
  102. {
  103. return $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
  104. }
  105. /**
  106. * @param object $order
  107. * @return string
  108. */
  109. public function getTrackUrl($order)
  110. {
  111. return $this->getUrl('sales/order/track', ['order_id' => $order->getId()]);
  112. }
  113. /**
  114. * @return string
  115. */
  116. protected function _toHtml()
  117. {
  118. if ($this->getOrders()->getSize() > 0) {
  119. return parent::_toHtml();
  120. }
  121. return '';
  122. }
  123. /**
  124. * @param object $order
  125. * @return string
  126. */
  127. public function getReorderUrl($order)
  128. {
  129. return $this->getUrl('sales/order/reorder', ['order_id' => $order->getId()]);
  130. }
  131. }