@@ -113,14 +113,15 @@ public function getPayload(array $params): array
113
113
try {
114
114
$ orderId = $ params ['lastOrder ' ];
115
115
$ order = $ this ->orderRepository ->get ($ orderId );
116
+ $ contentData = $ this ->prepareData ($ order );
116
117
$ customData = [
117
118
'currency ' => $ this ->magentoDataHelper ->getCurrency (),
118
119
'value ' => $ this ->getOrderTotal ($ order ),
119
120
'content_type ' => 'product ' ,
120
121
'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 ,
124
125
'order_id ' => (string )$ this ->getOrderId ($ order ),
125
126
'userDataFromOrder ' => $ this ->getUserDataFromOrder ($ order )
126
127
];
@@ -130,6 +131,29 @@ public function getPayload(array $params): array
130
131
return $ customData ;
131
132
}
132
133
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
+
133
157
/**
134
158
* Return all the match keys that can be extracted from order information
135
159
*
@@ -198,51 +222,6 @@ private function getOrderId(OrderInterface $order)
198
222
}
199
223
}
200
224
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
-
246
225
/**
247
226
* Return the last order total value
248
227
*
@@ -275,23 +254,4 @@ private function getNumItems(OrderInterface $order)
275
254
}
276
255
return $ order ->getTotalQtyOrdered ();
277
256
}
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
- }
297
257
}
0 commit comments