Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
History.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\App\ObjectManager;
9 use \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
10 
18 {
22  protected $_template = 'Magento_Sales::order/history.phtml';
23 
28 
32  protected $_customerSession;
33 
37  protected $_orderConfig;
38 
42  protected $orders;
43 
47  private $orderCollectionFactory;
48 
56  public function __construct(
57  \Magento\Framework\View\Element\Template\Context $context,
58  \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
59  \Magento\Customer\Model\Session $customerSession,
60  \Magento\Sales\Model\Order\Config $orderConfig,
61  array $data = []
62  ) {
63  $this->_orderCollectionFactory = $orderCollectionFactory;
64  $this->_customerSession = $customerSession;
65  $this->_orderConfig = $orderConfig;
66  parent::__construct($context, $data);
67  }
68 
72  protected function _construct()
73  {
74  parent::_construct();
75  $this->pageConfig->getTitle()->set(__('My Orders'));
76  }
77 
83  private function getOrderCollectionFactory()
84  {
85  if ($this->orderCollectionFactory === null) {
86  $this->orderCollectionFactory = ObjectManager::getInstance()->get(CollectionFactoryInterface::class);
87  }
88  return $this->orderCollectionFactory;
89  }
90 
94  public function getOrders()
95  {
96  if (!($customerId = $this->_customerSession->getCustomerId())) {
97  return false;
98  }
99  if (!$this->orders) {
100  $this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
101  '*'
102  )->addFieldToFilter(
103  'status',
104  ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
105  )->setOrder(
106  'created_at',
107  'desc'
108  );
109  }
110  return $this->orders;
111  }
112 
116  protected function _prepareLayout()
117  {
118  parent::_prepareLayout();
119  if ($this->getOrders()) {
120  $pager = $this->getLayout()->createBlock(
121  \Magento\Theme\Block\Html\Pager::class,
122  'sales.order.history.pager'
123  )->setCollection(
124  $this->getOrders()
125  );
126  $this->setChild('pager', $pager);
127  $this->getOrders()->load();
128  }
129  return $this;
130  }
131 
135  public function getPagerHtml()
136  {
137  return $this->getChildHtml('pager');
138  }
139 
144  public function getViewUrl($order)
145  {
146  return $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
147  }
148 
153  public function getTrackUrl($order)
154  {
155  return $this->getUrl('sales/order/track', ['order_id' => $order->getId()]);
156  }
157 
162  public function getReorderUrl($order)
163  {
164  return $this->getUrl('sales/order/reorder', ['order_id' => $order->getId()]);
165  }
166 
170  public function getBackUrl()
171  {
172  return $this->getUrl('customer/account/');
173  }
174 }
$order
Definition: order.php:55
__()
Definition: __.php:13
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Sales\Model\Order\Config $orderConfig, array $data=[])
Definition: History.php:56