25
25
use Magento \Catalog \Model \ProductRepository ;
26
26
use Magento \ConfigurableProduct \Model \Product \Type \Configurable as ConfigurableType ;
27
27
use Magento \Framework \Exception \LocalizedException ;
28
+ use Magento \Framework \Exception \NoSuchEntityException ;
28
29
use Magento \Sales \Api \Data \OrderItemInterfaceFactory ;
29
30
use Magento \Sales \Model \Order \Item as OrderItem ;
30
31
use Meta \BusinessExtension \Helper \GraphAPIAdapter ;
31
32
use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
32
- use Meta \Catalog \Helper \Product \Identifier as ProductIdentifier ;
33
33
use Psr \Log \LoggerInterface ;
34
34
35
35
/**
@@ -47,11 +47,6 @@ class OrderItemMapper
47
47
*/
48
48
private GraphAPIAdapter $ graphAPIAdapter ;
49
49
50
- /**
51
- * @var ProductIdentifier
52
- */
53
- private ProductIdentifier $ productIdentifier ;
54
-
55
50
/**
56
51
* @var ProductRepository
57
52
*/
@@ -76,7 +71,6 @@ class OrderItemMapper
76
71
* @param SystemConfig $systemConfig
77
72
* @param GraphAPIAdapter $graphAPIAdapter
78
73
* @param LoggerInterface $logger
79
- * @param ProductIdentifier $productIdentifier
80
74
* @param ProductRepository $productRepository
81
75
* @param ConfigurableType $configurableType
82
76
* @param OrderItemInterfaceFactory $orderItemFactory
@@ -85,20 +79,35 @@ public function __construct(
85
79
SystemConfig $ systemConfig ,
86
80
GraphAPIAdapter $ graphAPIAdapter ,
87
81
LoggerInterface $ logger ,
88
- ProductIdentifier $ productIdentifier ,
89
82
ProductRepository $ productRepository ,
90
83
ConfigurableType $ configurableType ,
91
84
OrderItemInterfaceFactory $ orderItemFactory
92
85
) {
93
86
$ this ->systemConfig = $ systemConfig ;
94
87
$ this ->graphAPIAdapter = $ graphAPIAdapter ;
95
88
$ this ->logger = $ logger ;
96
- $ this ->productIdentifier = $ productIdentifier ;
97
89
$ this ->productRepository = $ productRepository ;
98
90
$ this ->configurableType = $ configurableType ;
99
91
$ this ->orderItemFactory = $ orderItemFactory ;
100
92
}
101
93
94
+ /**
95
+ * Fetch product by retailer id and identifier attribute
96
+ *
97
+ * @param string|int $retailerId
98
+ * @return ProductInterface|bool
99
+ * @throws NoSuchEntityException
100
+ */
101
+ private function getProductByFacebookRetailerId ($ retailerId )
102
+ {
103
+ try {
104
+ return $ this ->productRepository ->get ($ retailerId );
105
+ } catch (NoSuchEntityException $ e ) {
106
+ // Product not found
107
+ return $ this ->productRepository ->getById ($ retailerId );
108
+ }
109
+ }
110
+
102
111
/**
103
112
* Map facebook order item data to magento order item
104
113
*
@@ -109,7 +118,17 @@ public function __construct(
109
118
*/
110
119
public function map (array $ item , int $ storeId ): OrderItem
111
120
{
112
- $ product = $ this ->productIdentifier ->getProductByFacebookRetailerId ($ item ['retailer_id ' ]);
121
+ try {
122
+ $ product = $ this ->getProductByFacebookRetailerId ($ item ['retailer_id ' ]);
123
+ } catch (NoSuchEntityException $ e ) {
124
+ throw new LocalizedException (
125
+ __ (
126
+ 'Failed to map order item to Magento product: %1 ' ,
127
+ $ e ->getMessage ()
128
+ )
129
+ );
130
+ }
131
+
113
132
$ productInfo = $ this ->getProductInfo ($ item ['product_id ' ], $ storeId );
114
133
115
134
$ quantity = $ item ['quantity ' ];
0 commit comments