Skip to content

Commit 5af5f11

Browse files
swinard-metaSean Winard
andauthored
Handle events for new shipments and updated tracking (#552)
* Handle events for new shipments and updated tracking * Reload fb order between shipping events, handle dupe tracking error * Store synced tracked on shipments * Fix up logs --------- Co-authored-by: Sean Winard <[email protected]>
1 parent c0f54be commit 5af5f11

File tree

11 files changed

+391
-78
lines changed

11 files changed

+391
-78
lines changed

app/code/Meta/BusinessExtension/Helper/GraphAPIAdapter.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ public function acknowledgeOrders($ordersRootId, array $orderIds)
707707
}
708708

709709
/**
710-
* Mark order as shipped
710+
* Mark order items as shipped
711711
*
712712
* @param mixed $fbOrderId
713713
* @param array $items
@@ -717,8 +717,13 @@ public function acknowledgeOrders($ordersRootId, array $orderIds)
717717
* @return mixed|\Psr\Http\Message\ResponseInterface
718718
* @throws GuzzleException
719719
*/
720-
public function markOrderAsShipped($fbOrderId, $magentoShipmentId, $items, $trackingInfo, $fulfillmentAddressData)
721-
{
720+
public function markOrderItemsAsShipped(
721+
$fbOrderId,
722+
$magentoShipmentId,
723+
$items,
724+
$trackingInfo,
725+
$fulfillmentAddressData
726+
) {
722727
$request = [
723728
'access_token' => $this->accessToken,
724729
'external_shipment_id' => $magentoShipmentId,
@@ -736,6 +741,28 @@ public function markOrderAsShipped($fbOrderId, $magentoShipmentId, $items, $trac
736741
return json_decode($response->getBody()->__toString(), true);
737742
}
738743

744+
/**
745+
* Update tracking info for a shipment
746+
*
747+
* @param mixed $fbOrderId
748+
* @param string $magentoShipmentId
749+
* @param array $trackingInfo
750+
* @return mixed|\Psr\Http\Message\ResponseInterface
751+
* @throws GuzzleException
752+
*/
753+
public function updateShipmentTracking($fbOrderId, $magentoShipmentId, $trackingInfo)
754+
{
755+
$request = [
756+
'access_token' => $this->accessToken,
757+
'idempotency_key' => $this->getUniqId(),
758+
'external_shipment_id' => $magentoShipmentId,
759+
'tracking_info' => json_encode($trackingInfo),
760+
];
761+
762+
$response = $this->callApi('POST', "{$fbOrderId}/update_shipment", $request);
763+
return json_decode($response->getBody()->__toString(), true);
764+
}
765+
739766
/**
740767
* Cancel order
741768
*

app/code/Meta/Sales/Api/Data/FacebookOrderInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ public function getChannel();
6767
*/
6868
public function setChannel($channel);
6969

70+
/**
71+
* Get synced shipment metadata
72+
*
73+
* @return array
74+
*/
75+
public function getSyncedShipments();
76+
77+
/**
78+
* Update synced shipment metadata
79+
*
80+
* @param mixed $magentoShipmentId
81+
* @param array $trackingInfo
82+
* @return $this
83+
*/
84+
public function updateSyncedShipment($magentoShipmentId, $trackingInfo);
85+
7086
/**
7187
* Get extra data
7288
*

app/code/Meta/Sales/Helper/OrderHelper.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
namespace Meta\Sales\Helper;
2222

23-
use Meta\Sales\Api\Data\FacebookOrderInterfaceFactory;
2423
use Magento\Sales\Api\Data\OrderExtensionFactory;
2524
use Magento\Sales\Api\Data\OrderInterface;
25+
use Meta\Sales\Api\Data\FacebookOrderInterfaceFactory;
2626

2727
class OrderHelper
2828
{
@@ -48,33 +48,44 @@ public function __construct(
4848
$this->facebookOrderFactory = $facebookOrderFactory;
4949
}
5050

51+
public function loadFacebookOrderFromMagentoId($magentoOrderId)
52+
{
53+
/** @var \Meta\Sales\Api\Data\FacebookOrderInterface $facebookOrder */
54+
$facebookOrder = $this->facebookOrderFactory->create();
55+
$facebookOrder->load($magentoOrderId, 'magento_order_id');
56+
57+
return $facebookOrder;
58+
}
59+
5160
/**
5261
* Assign Meta order's extension attributes such as facebook_order_id to a Magento order
5362
*
5463
* @param OrderInterface $order
64+
* @param bool $reload
5565
* @return void
5666
*/
57-
public function setFacebookOrderExtensionAttributes(OrderInterface $order)
67+
public function setFacebookOrderExtensionAttributes(OrderInterface $order, bool $reload = false)
5868
{
5969
// if FB order ID present, do nothing
60-
if ($order->getExtensionAttributes()->getFacebookOrderId()) {
70+
if ($order->getExtensionAttributes()->getFacebookOrderId() && !$reload) {
6171
return;
6272
}
6373

6474
/** @var \Meta\Sales\Api\Data\FacebookOrderInterface $facebookOrder */
65-
$facebookOrder = $this->facebookOrderFactory->create();
66-
$facebookOrder->load($order->getId(), 'magento_order_id');
75+
$facebookOrder = $this->loadFacebookOrderFromMagentoId($order->getId());
6776

6877
if (!$facebookOrder->getId()) {
6978
return;
7079
}
7180

7281
$emailRemarketingOption = ($facebookOrder->getExtraData()['email_remarketing_option'] ?? false) === true;
82+
$syncedShipments = $facebookOrder->getSyncedShipments();
7383

7484
$extensionAttributes = $order->getExtensionAttributes() ?: $this->orderExtensionFactory->create();
7585
$extensionAttributes->setFacebookOrderId($facebookOrder->getFacebookOrderId())
7686
->setChannel($facebookOrder->getChannel())
77-
->setEmailRemarketingOption($emailRemarketingOption);
87+
->setEmailRemarketingOption($emailRemarketingOption)
88+
->setSyncedShipments($syncedShipments);
7889
$order->setExtensionAttributes($extensionAttributes);
7990
}
8091
}

app/code/Meta/Sales/Model/FacebookOrder.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
namespace Meta\Sales\Model;
2222

23+
use Magento\Framework\Model\AbstractModel;
2324
use Meta\Sales\Api\Data\FacebookOrderInterface;
2425
use Meta\Sales\Model\ResourceModel\FacebookOrder as ResourceModel;
25-
use Magento\Framework\Model\AbstractModel;
2626

2727
class FacebookOrder extends AbstractModel implements FacebookOrderInterface
2828
{
@@ -104,6 +104,32 @@ public function setChannel($channel)
104104
return $this;
105105
}
106106

107+
/**
108+
* Get synced shipment metadata
109+
*
110+
* @return array
111+
*/
112+
public function getSyncedShipments()
113+
{
114+
return json_decode($this->getData('synced_shipments') ?? '{}', true);
115+
}
116+
117+
/**
118+
* Update synced shipment metadata
119+
*
120+
* @param mixed $magentoShipmentId
121+
* @param array $trackingInfo
122+
* @return $this
123+
*/
124+
public function updateSyncedShipment($magentoShipmentId, $trackingInfo)
125+
{
126+
$shipments = $this->getSyncedShipments();
127+
$shipments[$magentoShipmentId] = self::encodeTrackingInfo($trackingInfo);
128+
$this->setData('synced_shipments', json_encode($shipments));
129+
130+
return $this;
131+
}
132+
107133
/**
108134
* Get extra data
109135
*
@@ -125,4 +151,34 @@ public function setExtraData(array $extraData)
125151
$this->setData('extra_data', json_encode($extraData));
126152
return $this;
127153
}
154+
155+
/**
156+
* Determine if the given shipment's tracking is not yet synced
157+
*
158+
* @param Order $order
159+
* @param mixed $magentoShipmentId
160+
* @param array $trackingInfo
161+
* @return bool
162+
*/
163+
public static function isSyncedShipmentOutOfSync($order, $magentoShipmentId, $trackingInfo)
164+
{
165+
$syncedShipments = $order->getExtensionAttributes()->getSyncedShipments();
166+
if (!array_key_exists($magentoShipmentId, $syncedShipments)) {
167+
return true;
168+
}
169+
170+
$syncedShipment = $syncedShipments[$magentoShipmentId];
171+
return $syncedShipment !== FacebookOrder::encodeTrackingInfo($trackingInfo);
172+
}
173+
174+
/**
175+
* Encoding the given tracking info for storage as metadata on a synced Shipment
176+
*
177+
* @param array $trackingInfo
178+
* @return string
179+
*/
180+
private static function encodeTrackingInfo($trackingInfo)
181+
{
182+
return $trackingInfo['carrier'] . '|' . $trackingInfo['tracking_number'];
183+
}
128184
}

0 commit comments

Comments
 (0)