22
22
23
23
use GuzzleHttp \Exception \GuzzleException ;
24
24
use Magento \Framework \Exception \LocalizedException ;
25
- use Magento \Sales \Model \Order \Item as OrderItem ;
26
25
use Magento \Sales \Model \Order \Shipment ;
27
26
use Magento \Sales \Model \Order \Shipment \Item ;
28
27
use Magento \Sales \Model \Order \Shipment \Track ;
29
28
use Meta \BusinessExtension \Helper \FBEHelper ;
30
29
use Meta \BusinessExtension \Helper \GraphAPIAdapter ;
31
30
use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
32
- use Meta \Catalog \Model \Config \Source \Product \Identifier as IdentifierConfig ;
33
31
use Meta \Sales \Helper \OrderHelper ;
34
32
use Meta \Sales \Helper \ShippingHelper ;
35
33
use Meta \Sales \Model \FacebookOrder ;
@@ -79,7 +77,7 @@ public function __construct(
79
77
GraphAPIAdapter $ graphAPIAdapter ,
80
78
ShippingHelper $ shippingHelper ,
81
79
OrderHelper $ orderHelper ,
82
- FBEHelper $ fbeHelper,
80
+ FBEHelper $ fbeHelper
83
81
) {
84
82
$ this ->systemConfig = $ systemConfig ;
85
83
$ this ->graphAPIAdapter = $ graphAPIAdapter ;
@@ -99,24 +97,6 @@ public function getOrderShipEvent($storeId = null)
99
97
return $ this ->systemConfig ->getOrderShipEvent ($ storeId );
100
98
}
101
99
102
- /**
103
- * Get retailer id
104
- *
105
- * @param OrderItem $orderItem
106
- * @return string|int|bool
107
- */
108
- private function getRetailerId (OrderItem $ orderItem )
109
- {
110
- $ storeId = $ orderItem ->getStoreId ();
111
- $ productIdentifierAttr = $ this ->systemConfig ->getProductIdentifierAttr ($ storeId );
112
- if ($ productIdentifierAttr === IdentifierConfig::PRODUCT_IDENTIFIER_SKU ) {
113
- return $ orderItem ->getSku ();
114
- } elseif ($ productIdentifierAttr === IdentifierConfig::PRODUCT_IDENTIFIER_ID ) {
115
- return $ orderItem ->getProductId ();
116
- }
117
- return false ;
118
- }
119
-
120
100
/**
121
101
* Get carrier code for facebook
122
102
*
@@ -199,12 +179,17 @@ public function markAsShipped(Shipment $shipment)
199
179
200
180
$ storeId = $ order ->getStoreId ();
201
181
202
- $ itemsToShip = [];
182
+ $ itemsToShipBySku = [];
183
+ $ itemsToShipById = [];
203
184
/** @var Item $shipmentItem */
204
185
foreach ($ shipment ->getAllItems () as $ shipmentItem ) {
205
186
$ orderItem = $ shipmentItem ->getOrderItem ();
206
- $ itemsToShip [] = [
207
- 'retailer_id ' => $ this ->getRetailerId ($ orderItem ),
187
+ $ itemsToShipBySku [] = [
188
+ 'retailer_id ' => $ orderItem ->getSku (),
189
+ 'quantity ' => (int )$ shipmentItem ->getQty ()
190
+ ];
191
+ $ itemsToShipById [] = [
192
+ 'retailer_id ' => $ orderItem ->getId (),
208
193
'quantity ' => (int )$ shipmentItem ->getQty ()
209
194
];
210
195
}
@@ -216,14 +201,27 @@ public function markAsShipped(Shipment $shipment)
216
201
$ fulfillmentAddress ['state ' ] = $ this ->shippingHelper ->getRegionName ($ fulfillmentAddress ['state ' ]);
217
202
}
218
203
219
- $ this ->markOrderItemsAsShipped (
220
- (int )$ storeId ,
221
- $ fbOrderId ,
222
- $ magentoShipmentId ,
223
- $ itemsToShip ,
224
- $ trackingInfo ,
225
- $ fulfillmentAddress
226
- );
204
+ try {
205
+ $ this ->markOrderAsShipped (
206
+ (int )$ storeId ,
207
+ $ fbOrderId ,
208
+ $ shipment ->getIncrementId (),
209
+ $ itemsToShipBySku ,
210
+ $ trackingInfo ,
211
+ $ fulfillmentAddress
212
+ );
213
+ } catch (\Exception $ e ) {
214
+ // Validated the Meta API will throw if retailer ids provided are invalid
215
+ // https://fburl.com/code/p523l7gm
216
+ $ this ->markOrderAsShipped (
217
+ (int )$ storeId ,
218
+ $ fbOrderId ,
219
+ $ shipment ->getIncrementId (),
220
+ $ itemsToShipById ,
221
+ $ trackingInfo ,
222
+ $ fulfillmentAddress
223
+ );
224
+ }
227
225
228
226
if ($ track ) {
229
227
$ comment = "Order Marked as Shipped on Meta for {$ track ->getTitle ()}. Tracking #: {$ track ->getNumber ()}" ;
@@ -292,7 +290,9 @@ public function updateShipmentTracking(Shipment $shipment)
292
290
293
291
if (count ($ tracks ) == 0 ) {
294
292
// For now, we don't support removing tracking entirely
295
- $ this ->fbeHelper ->log ("[updateShipmentTracking] Shipment: {$ shipment ->getIncrementId ()} - Skipping, no tracks " );
293
+ $ this ->fbeHelper ->log (
294
+ "[updateShipmentTracking] Shipment: {$ shipment ->getIncrementId ()} - Skipping, no tracks "
295
+ );
296
296
return ;
297
297
}
298
298
@@ -307,7 +307,9 @@ public function updateShipmentTracking(Shipment $shipment)
307
307
308
308
$ magentoShipmentId = $ shipment ->getIncrementId ();
309
309
if (!FacebookOrder::isSyncedShipmentOutOfSync ($ order , $ magentoShipmentId , $ trackingInfo )) {
310
- $ this ->fbeHelper ->log ("[updateShipmentTracking] Shipment: {$ shipment ->getIncrementId ()} - Skipping, in sync " );
310
+ $ this ->fbeHelper ->log (
311
+ "[updateShipmentTracking] Shipment: {$ shipment ->getIncrementId ()} - Skipping, in sync "
312
+ );
311
313
return ;
312
314
}
313
315
@@ -318,7 +320,7 @@ public function updateShipmentTracking(Shipment $shipment)
318
320
$ trackingInfo ,
319
321
);
320
322
321
- $ comment = "Order Shipment Tracking Updated on Meta for {$ track ->getTitle ()}. Tracking #: {$ track ->getNumber ()}" ;
323
+ $ comment = "Order Shipment Tracking Updated on Meta for {$ track ->getTitle ()}. Tracking # {$ track ->getNumber ()}" ;
322
324
$ order ->addCommentToStatusHistory ($ comment )->save ();
323
325
324
326
$ fbOrder = $ this ->orderHelper ->loadFacebookOrderFromMagentoId ($ order ->getId ());
@@ -338,7 +340,7 @@ private function updateOrderShipmentTracking(
338
340
int $ storeId ,
339
341
string $ fbOrderId ,
340
342
string $ magentoShipmentId ,
341
- array $ trackingInfo,
343
+ array $ trackingInfo
342
344
) {
343
345
$ this ->graphAPIAdapter
344
346
->setDebugMode ($ this ->systemConfig ->isDebugMode ($ storeId ))
0 commit comments