@@ -176,7 +176,8 @@ public function __construct(
176
176
DateTime $ dateTime ,
177
177
CartRepositoryInterface $ quoteRepository ,
178
178
UrlInterface $ urlBuilder
179
- ) {
179
+ )
180
+ {
180
181
$ this ->urlBuilder = $ urlBuilder ;
181
182
$ this ->customerRepository = $ customerRepository ;
182
183
$ this ->addressRepository = $ addressRepository ;
@@ -210,7 +211,7 @@ public function __construct(
210
211
public function sendAbandonedCartData ($ quoteId = null ): array
211
212
{
212
213
$ result = [];
213
- $ numberOfAbandonedCart = (int ) $ this ->abandonedCartHelper ->getNumberOfAbandonedCart ();
214
+ $ numberOfAbandonedCart = (int )$ this ->abandonedCartHelper ->getNumberOfAbandonedCart ();
214
215
$ abandonedCarts = $ this ->quoteResourceCollectionFactory ->create ()
215
216
->addFieldToSelect ('* ' )
216
217
->addFieldToFilter ('ac_synced_date ' , [
@@ -230,7 +231,7 @@ public function sendAbandonedCartData($quoteId = null): array
230
231
$ abandonedCarts ->addFieldToFilter ('entity_id ' , ['eq ' => $ quoteId ]);
231
232
}
232
233
$ abandonedCarts ->setPageSize ($ numberOfAbandonedCart );
233
- $ abandonedCarts ->getSelect ()->join (array ('address ' => $ abandonedCarts ->getResource ()->getTable ('quote_address ' )),'main_table.entity_id = address.quote_id ' )
234
+ $ abandonedCarts ->getSelect ()->join (array ('address ' => $ abandonedCarts ->getResource ()->getTable ('quote_address ' )), 'main_table.entity_id = address.quote_id ' )
234
235
->where ("address.address_type='billing' and (main_table.customer_email is not null or address.email is not null) " );
235
236
foreach ($ abandonedCarts as $ abandonedCart ) {
236
237
$ connectionId = $ this ->coreHelper ->getConnectionId ($ abandonedCart ->getStoreId ());
@@ -296,7 +297,7 @@ public function sendAbandonedCartData($quoteId = null): array
296
297
$ customerModel = $ this ->customerFactory ->create ();
297
298
$ this ->customerResource ->load ($ customerModel , $ customerId );
298
299
if ($ customerModel ->getAcCustomerId ()) {
299
- $ this ->customerId = $ customerModel ->getAcCustomerId ();
300
+ $ this ->customerId = $ customerModel ->getAcCustomerId ();
300
301
}
301
302
}
302
303
$ abandonedCart ->collectTotals ();
@@ -311,7 +312,7 @@ public function sendAbandonedCartData($quoteId = null): array
311
312
"orderDiscounts " => [
312
313
"discountAmount " => $ this ->coreHelper ->priceToCents ($ abandonedCart ->getDiscountAmount ())
313
314
],
314
- "orderUrl " => $ this ->urlBuilder ->getDirectUrl ('checkout/cart ' ),
315
+ "orderUrl " => $ this ->urlBuilder ->getDirectUrl ('checkout/cart ' ),
315
316
"abandonedDate " => $ abandonedCart ->getCreatedAt (),
316
317
"externalCreatedDate " => $ abandonedCart ->getCreatedAt (),
317
318
"externalUpdatedDate " => $ abandonedCart ->getUpdatedAt (),
@@ -328,6 +329,7 @@ public function sendAbandonedCartData($quoteId = null): array
328
329
];
329
330
330
331
try {
332
+
331
333
if (is_null ($ abandonedCart ->getAcSyncedDate ())) {
332
334
$ abandonedCartResult = $ this ->curl ->sendRequestAbandonedCart (
333
335
self ::METHOD ,
@@ -337,7 +339,7 @@ public function sendAbandonedCartData($quoteId = null): array
337
339
} else {
338
340
$ abandonedCartResult = $ this ->curl ->sendRequestAbandonedCart (
339
341
self ::UPDATE_METHOD ,
340
- self ::ABANDONED_CART_URL_ENDPOINT . "/ " . (int ) $ abandonedCart ->getAcOrderSyncId (),
342
+ self ::ABANDONED_CART_URL_ENDPOINT . "/ " . (int )$ abandonedCart ->getAcOrderSyncId (),
341
343
$ abandonedCartData
342
344
);
343
345
}
@@ -407,7 +409,8 @@ private function getQuoteItems($quoteId): Collection
407
409
$ quoteItemCollection = $ this ->quoteItemCollectionFactory ->create ();
408
410
return $ quoteItemCollection
409
411
->addFieldToSelect ('* ' )
410
- ->addFieldToFilter ('quote_id ' , [$ quoteId ]);
412
+ ->addFieldToFilter ('quote_id ' , [$ quoteId ])
413
+ ->addFieldToFilter ('parent_item_id ' , ['null ' => true ]);
411
414
}
412
415
413
416
/**
@@ -461,7 +464,8 @@ private function getFieldValues($customerId): array
461
464
return $ fieldValues ;
462
465
}
463
466
464
- public function isGuest ($ quote ): bool {
467
+ public function isGuest ($ quote ): bool
468
+ {
465
469
return is_null ($ quote ->getCustomerId ());
466
470
}
467
471
@@ -510,6 +514,7 @@ private function createEcomCustomer($customerId, $quote): void
510
514
$ ecomCustomer ['connectionid ' ] = $ connectionid ;
511
515
$ ecomCustomer ['externalid ' ] = $ customerId ;
512
516
$ ecomCustomer ['email ' ] = $ customerEmail ;
517
+ $ ecomCustomer ['acceptsMarketing ' ] = 1 ;
513
518
$ ecomCustomerData ['ecomCustomer ' ] = $ ecomCustomer ;
514
519
$ AcCustomer = $ this ->curl ->listAllCustomers (
515
520
self ::GET_METHOD ,
@@ -538,22 +543,35 @@ private function createEcomCustomer($customerId, $quote): void
538
543
}
539
544
540
545
if ($ this ->isGuest ($ quote )) {
541
- $ ecomCustomerData = [
542
- "ecomCustomer " => [
543
- "connection " => $ connectionid ,
544
- 'externals ' => $ quote ->getBillingAddress ()->getEmail (),
545
- 'email ' => $ quote ->getBillingAddress ()->getEmail (),
546
- ]
547
- ];
548
- $ ecomCustomerResult = $ this ->curl ->createContacts (
549
- self ::METHOD ,
546
+
547
+ $ ecomCustomerResult = $ this ->curl ->listAllCustomers (
548
+ self ::GET_METHOD ,
550
549
self ::ECOM_CUSTOMER_ENDPOINT ,
551
- $ ecomCustomerData
550
+ $ quote -> getBillingAddress ()-> getEmail ()
552
551
);
553
- $ ecomCustomerId = $ ecomCustomerResult ['data ' ]['ecomCustomer ' ]['id ' ] ?? null ;
552
+ if (isset ($ ecomCustomerResult ['data ' ]['ecomCustomers ' ][0 ])) {
553
+ foreach ($ ecomCustomerResult ['data ' ]['ecomCustomers ' ] as $ Ac ) {
554
+ if ($ Ac ['connectionid ' ] === $ connectionid ) {
555
+ $ ecomCustomerId = $ Ac ['id ' ];
556
+ }
557
+ }
558
+ } else {
559
+ $ ecomCustomerData = [
560
+ "ecomCustomer " => [
561
+ "connection " => $ connectionid ,
562
+ 'externals ' => $ quote ->getBillingAddress ()->getEmail (),
563
+ 'email ' => $ quote ->getBillingAddress ()->getEmail (),
564
+ ]
565
+ ];
566
+ $ ecomCustomerResult = $ this ->curl ->createContacts (
567
+ self ::METHOD ,
568
+ self ::ECOM_CUSTOMER_ENDPOINT ,
569
+ $ ecomCustomerData
570
+ );
571
+ $ ecomCustomerId = $ ecomCustomerResult ['data ' ]['ecomCustomer ' ]['id ' ] ?? null ;
572
+ }
554
573
}
555
-
556
- if ($ ecomCustomerId !== 0 ) {
574
+ if ($ ecomCustomerId !== 0 ) {
557
575
$ syncStatus = CronConfig::SYNCED ;
558
576
} else {
559
577
$ syncStatus = CronConfig::FAIL_SYNCED ;
@@ -564,7 +582,8 @@ private function createEcomCustomer($customerId, $quote): void
564
582
} else {
565
583
$ this ->saveCustomerResult ($ customerId , $ syncStatus , $ contactId , $ ecomCustomerId );
566
584
}
567
- } catch (\Exception $ e ) {
585
+ } catch
586
+ (\Exception $ e ) {
568
587
$ this ->logger ->critical ("MODULE AbandonedCart: " . $ e ->getMessage ());
569
588
} catch (GuzzleException $ e ) {
570
589
$ this ->logger ->critical ("MODULE AbandonedCart: " . $ e ->getMessage ());
0 commit comments