Skip to content

Commit b748982

Browse files
committed
ADO-313: Modifies the purchase event payload
1 parent e8c739c commit b748982

File tree

1 file changed

+27
-67
lines changed

1 file changed

+27
-67
lines changed

app/code/Meta/Conversion/Model/Tracker/Purchase.php

Lines changed: 27 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,15 @@ public function getPayload(array $params): array
113113
try {
114114
$orderId = $params['lastOrder'];
115115
$order = $this->orderRepository->get($orderId);
116+
$contentData = $this->prepareData($order);
116117
$customData = [
117118
'currency' => $this->magentoDataHelper->getCurrency(),
118119
'value' => $this->getOrderTotal($order),
119120
'content_type' => 'product',
120121
'num_items' => $this->getNumItems($order),
121-
'content_ids' => $this->getOrderContentIds($order),
122-
'contents' => $this->getOrderContents($order),
123-
'content_name' => $this->getContentName($order),
122+
'content_ids' => $contentData['content_ids'],
123+
'contents' => $contentData['contents'],
124+
'content_name' => $contentData['content_name'] ?? null,
124125
'order_id' => (string)$this->getOrderId($order),
125126
'userDataFromOrder' => $this->getUserDataFromOrder($order)
126127
];
@@ -130,6 +131,29 @@ public function getPayload(array $params): array
130131
return $customData;
131132
}
132133

134+
/**
135+
* @param OrderInterface $order
136+
* @return array[]
137+
*/
138+
private function prepareData(OrderInterface $order): array
139+
{
140+
$contents = [];
141+
$contentIds = [];
142+
$contentName = [];
143+
144+
$items = $order->getAllVisibleItems();
145+
foreach ($items as $item) {
146+
$contents[] = [
147+
'product_id' => $item->getSku(),
148+
'quantity' => (int)$item->getQtyOrdered(),
149+
'item_price' => $item->getPrice()
150+
];
151+
$contentIds[] = $item->getSku();
152+
$contentName[] = $item->getName();
153+
}
154+
return ['contents' => $contents, 'content_ids' => $contentIds, 'content_name' => json_encode($contentName)];
155+
}
156+
133157
/**
134158
* Return all the match keys that can be extracted from order information
135159
*
@@ -198,51 +222,6 @@ private function getOrderId(OrderInterface $order)
198222
}
199223
}
200224

201-
/**
202-
* Return information about the last order items
203-
*
204-
* @param OrderInterface $order
205-
* @link https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/custom-data/#contents
206-
*
207-
* @return array
208-
*/
209-
private function getOrderContents(OrderInterface $order): array
210-
{
211-
if (!$order) {
212-
return [];
213-
}
214-
$contents = [];
215-
/** @var Item[] $items */
216-
$items = $order->getAllVisibleItems();
217-
foreach ($items as $item) {
218-
$contents[] = [
219-
'product_id' => $item->getSku(),
220-
'quantity' => (int)$item->getQtyOrdered(),
221-
'item_price' => $item->getPrice()
222-
];
223-
}
224-
return $contents;
225-
}
226-
227-
/**
228-
* Return the ids of the items by last order
229-
*
230-
* @param OrderInterface $order
231-
* @return array
232-
*/
233-
private function getOrderContentIds(OrderInterface $order): array
234-
{
235-
if (!$order) {
236-
return [];
237-
}
238-
$contentIds = [];
239-
$items = $order->getAllVisibleItems();
240-
foreach ($items as $item) {
241-
$contentIds[] = $item->getSku();
242-
}
243-
return $contentIds;
244-
}
245-
246225
/**
247226
* Return the last order total value
248227
*
@@ -275,23 +254,4 @@ private function getNumItems(OrderInterface $order)
275254
}
276255
return $order->getTotalQtyOrdered();
277256
}
278-
279-
/**
280-
* Get Product Name
281-
*
282-
* @param OrderInterface $order
283-
* @return string
284-
*/
285-
private function getContentName(OrderInterface $order)
286-
{
287-
$productName = [];
288-
if ($order) {
289-
$items = $order->getAllVisibleItems();
290-
foreach ($items as $item) {
291-
/** @var Product $item */
292-
$productName[] = $item->getName();
293-
}
294-
}
295-
return json_encode($productName);
296-
}
297257
}

0 commit comments

Comments
 (0)