|
7 | 7 |
|
8 | 8 | use Magento\Customer\CustomerData\SectionSourceInterface;
|
9 | 9 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
| 10 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 11 | +use Psr\Log\LoggerInterface; |
| 12 | +use Magento\Framework\App\ObjectManager; |
10 | 13 |
|
11 | 14 | /**
|
12 | 15 | * Returns information for "Recently Ordered" widget.
|
13 | 16 | * It contains list of 5 salable products from the last placed order.
|
14 | 17 | * Qty of products to display is limited by LastOrderedItems::SIDEBAR_ORDER_LIMIT constant.
|
| 18 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
15 | 19 | */
|
16 | 20 | class LastOrderedItems implements SectionSourceInterface
|
17 | 21 | {
|
@@ -60,28 +64,36 @@ class LastOrderedItems implements SectionSourceInterface
|
60 | 64 | */
|
61 | 65 | private $productRepository;
|
62 | 66 |
|
| 67 | + /** |
| 68 | + * @var LoggerInterface |
| 69 | + */ |
| 70 | + private $logger; |
| 71 | + |
63 | 72 | /**
|
64 | 73 | * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
|
65 | 74 | * @param \Magento\Sales\Model\Order\Config $orderConfig
|
66 | 75 | * @param \Magento\Customer\Model\Session $customerSession
|
67 | 76 | * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
|
68 | 77 | * @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
69 | 78 | * @param ProductRepositoryInterface $productRepository
|
| 79 | + * @param LoggerInterface|null $logger |
70 | 80 | */
|
71 | 81 | public function __construct(
|
72 | 82 | \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
|
73 | 83 | \Magento\Sales\Model\Order\Config $orderConfig,
|
74 | 84 | \Magento\Customer\Model\Session $customerSession,
|
75 | 85 | \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
|
76 | 86 | \Magento\Store\Model\StoreManagerInterface $storeManager,
|
77 |
| - ProductRepositoryInterface $productRepository |
| 87 | + ProductRepositoryInterface $productRepository, |
| 88 | + LoggerInterface $logger = null |
78 | 89 | ) {
|
79 | 90 | $this->_orderCollectionFactory = $orderCollectionFactory;
|
80 | 91 | $this->_orderConfig = $orderConfig;
|
81 | 92 | $this->_customerSession = $customerSession;
|
82 | 93 | $this->stockRegistry = $stockRegistry;
|
83 | 94 | $this->_storeManager = $storeManager;
|
84 | 95 | $this->productRepository = $productRepository;
|
| 96 | + $this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class); |
85 | 97 | }
|
86 | 98 |
|
87 | 99 | /**
|
@@ -118,12 +130,17 @@ protected function getItems()
|
118 | 130 | /** @var \Magento\Sales\Model\Order\Item $item */
|
119 | 131 | foreach ($order->getParentItemsRandomCollection($limit) as $item) {
|
120 | 132 | /** @var \Magento\Catalog\Model\Product $product */
|
121 |
| - $product = $this->productRepository->getById( |
122 |
| - $item->getProductId(), |
123 |
| - false, |
124 |
| - $this->_storeManager->getStore()->getId() |
125 |
| - ); |
126 |
| - if ($product && in_array($website, $product->getWebsiteIds())) { |
| 133 | + try { |
| 134 | + $product = $this->productRepository->getById( |
| 135 | + $item->getProductId(), |
| 136 | + false, |
| 137 | + $this->_storeManager->getStore()->getId() |
| 138 | + ); |
| 139 | + } catch (NoSuchEntityException $noEntityException) { |
| 140 | + $this->logger->critical($noEntityException); |
| 141 | + continue; |
| 142 | + } |
| 143 | + if (isset($product) && in_array($website, $product->getWebsiteIds())) { |
127 | 144 | $url = $product->isVisibleInSiteVisibility() ? $product->getProductUrl() : null;
|
128 | 145 | $items[] = [
|
129 | 146 | 'id' => $item->getId(),
|
@@ -152,7 +169,7 @@ protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $or
|
152 | 169 | $orderItem->getStore()->getWebsiteId()
|
153 | 170 | );
|
154 | 171 | return $stockItem->getIsInStock();
|
155 |
| - } catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) { |
| 172 | + } catch (NoSuchEntityException $noEntityException) { |
156 | 173 | return false;
|
157 | 174 | }
|
158 | 175 | }
|
|
0 commit comments