Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LastOrderedItems.php
Go to the documentation of this file.
1 <?php
7 
11 use Psr\Log\LoggerInterface;
12 
19 {
24 
29 
33  protected $_orderConfig;
34 
38  protected $_customerSession;
39 
43  protected $httpContext;
44 
48  protected $orders;
49 
53  protected $stockRegistry;
54 
58  private $_storeManager;
59 
63  private $productRepository;
64 
68  private $logger;
69 
79  public function __construct(
80  \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
81  \Magento\Sales\Model\Order\Config $orderConfig,
82  \Magento\Customer\Model\Session $customerSession,
83  \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
84  \Magento\Store\Model\StoreManagerInterface $storeManager,
85  ProductRepositoryInterface $productRepository,
86  LoggerInterface $logger
87  ) {
88  $this->_orderCollectionFactory = $orderCollectionFactory;
89  $this->_orderConfig = $orderConfig;
90  $this->_customerSession = $customerSession;
91  $this->stockRegistry = $stockRegistry;
92  $this->_storeManager = $storeManager;
93  $this->productRepository = $productRepository;
94  $this->logger = $logger;
95  }
96 
102  protected function initOrders()
103  {
104  $customerId = $this->_customerSession->getCustomerId();
105 
106  $orders = $this->_orderCollectionFactory->create()
107  ->addAttributeToFilter('customer_id', $customerId)
108  ->addAttributeToFilter('status', ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()])
109  ->addAttributeToSort('created_at', 'desc')
110  ->setPage(1, 1);
111  //TODO: add filter by current website
112  $this->orders = $orders;
113  }
114 
120  protected function getItems()
121  {
122  $items = [];
123  $order = $this->getLastOrder();
124  $limit = self::SIDEBAR_ORDER_LIMIT;
125 
126  if ($order) {
127  $website = $this->_storeManager->getStore()->getWebsiteId();
129  foreach ($order->getParentItemsRandomCollection($limit) as $item) {
131  try {
132  $product = $this->productRepository->getById(
133  $item->getProductId(),
134  false,
135  $this->_storeManager->getStore()->getId()
136  );
137  } catch (NoSuchEntityException $noEntityException) {
138  $this->logger->critical($noEntityException);
139  continue;
140  }
141  if (isset($product) && in_array($website, $product->getWebsiteIds())) {
142  $url = $product->isVisibleInSiteVisibility() ? $product->getProductUrl() : null;
143  $items[] = [
144  'id' => $item->getId(),
145  'name' => $item->getName(),
146  'url' => $url,
147  'is_saleable' => $this->isItemAvailableForReorder($item),
148  ];
149  }
150  }
151  }
152 
153  return $items;
154  }
155 
162  protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $orderItem)
163  {
164  try {
165  $stockItem = $this->stockRegistry->getStockItem(
166  $orderItem->getProduct()->getId(),
167  $orderItem->getStore()->getWebsiteId()
168  );
169  return $stockItem->getIsInStock();
170  } catch (NoSuchEntityException $noEntityException) {
171  return false;
172  }
173  }
174 
180  protected function getLastOrder()
181  {
182  if (!$this->orders) {
183  $this->initOrders();
184  }
185  foreach ($this->orders as $order) {
186  return $order;
187  }
188  }
189 
193  public function getSectionData()
194  {
195  return ['items' => $this->getItems()];
196  }
197 }
__construct(\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Sales\Model\Order\Config $orderConfig, \Magento\Customer\Model\Session $customerSession, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\Store\Model\StoreManagerInterface $storeManager, ProductRepositoryInterface $productRepository, LoggerInterface $logger)
$orderItem
Definition: order.php:30
$order
Definition: order.php:55
$storeManager
$logger
isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $orderItem)
$items