7
7
8
8
use Magento \Customer \CustomerData \SectionSourceInterface ;
9
9
use Magento \Catalog \Api \ProductRepositoryInterface ;
10
+ use Psr \Log \LoggerInterface ;
11
+ use Magento \Framework \App \ObjectManager ;
10
12
11
13
/**
12
14
* Returns information for "Recently Ordered" widget.
@@ -60,6 +62,11 @@ class LastOrderedItems implements SectionSourceInterface
60
62
*/
61
63
private $ productRepository ;
62
64
65
+ /**
66
+ * @var LoggerInterface
67
+ */
68
+ private $ logger ;
69
+
63
70
/**
64
71
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
65
72
* @param \Magento\Sales\Model\Order\Config $orderConfig
@@ -74,14 +81,16 @@ public function __construct(
74
81
\Magento \Customer \Model \Session $ customerSession ,
75
82
\Magento \CatalogInventory \Api \StockRegistryInterface $ stockRegistry ,
76
83
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
77
- ProductRepositoryInterface $ productRepository
84
+ ProductRepositoryInterface $ productRepository ,
85
+ LoggerInterface $ logger = null
78
86
) {
79
87
$ this ->_orderCollectionFactory = $ orderCollectionFactory ;
80
88
$ this ->_orderConfig = $ orderConfig ;
81
89
$ this ->_customerSession = $ customerSession ;
82
90
$ this ->stockRegistry = $ stockRegistry ;
83
91
$ this ->_storeManager = $ storeManager ;
84
92
$ this ->productRepository = $ productRepository ;
93
+ $ this ->logger = $ logger ?: ObjectManager::getInstance ()->get (LoggerInterface::class);
85
94
}
86
95
87
96
/**
@@ -118,12 +127,17 @@ protected function getItems()
118
127
/** @var \Magento\Sales\Model\Order\Item $item */
119
128
foreach ($ order ->getParentItemsRandomCollection ($ limit ) as $ item ) {
120
129
/** @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 ())) {
130
+ try {
131
+ $ product = $ this ->productRepository ->getById (
132
+ $ item ->getProductId (),
133
+ false ,
134
+ $ this ->_storeManager ->getStore ()->getId ()
135
+ );
136
+ }catch (\Magento \Framework \Exception \NoSuchEntityException $ e ){
137
+ $ this ->logger ->critical ($ e );
138
+ break ;
139
+ }
140
+ if (isset ($ product ) && in_array ($ website , $ product ->getWebsiteIds ())) {
127
141
$ url = $ product ->isVisibleInSiteVisibility () ? $ product ->getProductUrl () : null ;
128
142
$ items [] = [
129
143
'id ' => $ item ->getId (),
0 commit comments