Skip to content

Commit

Permalink
Merged PR 52058: Remove redundant keys from CartInsight data creation…
Browse files Browse the repository at this point in the history
… via V3

## What's being changed

This PR modifies the cart insight payload to remove the key and contact identifier.

## Why it's being changed

Previously when we posted this data via the V2 API, this data structure was required. The data inside the `json` node was used for the visible record, while the key and contact identifier were used by the platform when creating the record and attaching it to a contact. However in V3, this extra data is not required (there are additional params in the request) and this unmodified structure actually breaks the abandoned cart block in email templates.

## How to review / test this change

- Set up a program for abandoned cart in DD.
- Have the program trigger an email campaign that contains the Abandoned Cart block.
- Test abandoned cart sync. Confirm cart insight reaches DD.
- Confirm you receive an email containing your cart details.
- Test the order complete queue consumer (it should sent cart insight with an ORDER_COMPLETE cart phase).
- Test dummy data creation (click save in account creds to run).

Related work items: #245029
  • Loading branch information
sta1r committed Feb 26, 2024
1 parent ba67fa4 commit 914d39d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 114 deletions.
28 changes: 12 additions & 16 deletions Model/AbandonedCart/CartInsight/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,15 @@ public function getPayload($quote, $store)
$quoteCurrency = $quote->getQuoteCurrencyCode();

$data = [
'key' => $quote->getId(),
'contactIdentifier' => $quote->getCustomerEmail(),
'json' => [
'cartId' => $quote->getId(),
'cartUrl' => $this->getBasketUrl($quote->getId(), $store),
'createdDate' => $this->dateTime->date(\DateTime::ATOM, $quote->getCreatedAt()),
'modifiedDate' => $this->dateTime->date(\DateTime::ATOM, $quote->getUpdatedAt()),
'currency' => $quoteCurrency,
'subTotal' => round($quote->getSubtotal() ?: 0, 2),
'taxAmount' => round($quote->getShippingAddress()->getTaxAmount() ?: 0, 2),
'shipping' => round($quote->getShippingAddress()->getShippingAmount() ?: 0, 2),
'grandTotal' => round($quote->getGrandTotal() ?: 0, 2)
]
'cartId' => $quote->getId(),
'cartUrl' => $this->getBasketUrl($quote->getId(), $store),
'createdDate' => $this->dateTime->date(\DateTime::ATOM, $quote->getCreatedAt()),
'modifiedDate' => $this->dateTime->date(\DateTime::ATOM, $quote->getUpdatedAt()),
'currency' => $quoteCurrency,
'subTotal' => round($quote->getSubtotal() ?: 0, 2),
'taxAmount' => round($quote->getShippingAddress()->getTaxAmount() ?: 0, 2),
'shipping' => round($quote->getShippingAddress()->getShippingAmount() ?: 0, 2),
'grandTotal' => round($quote->getGrandTotal() ?: 0, 2)
];

$discountTotal = 0;
Expand All @@ -225,9 +221,9 @@ public function getPayload($quote, $store)
];
}

$data['json']['discountAmount'] = (float) $discountTotal;
$data['json']['lineItems'] = $lineItems;
$data['json']['cartPhase'] = 'ORDER_PENDING';
$data['discountAmount'] = (float) $discountTotal;
$data['lineItems'] = $lineItems;
$data['cartPhase'] = 'ORDER_PENDING';

return $data;
}
Expand Down
41 changes: 18 additions & 23 deletions Model/DummyRecordsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function getContactInsightData($websiteId): array
{
/** @var Store $store */
$store = $this->getStore($websiteId);
return $this->getStaticData($store, $websiteId);
return $this->getStaticData($store);
}

/**
Expand All @@ -121,28 +121,23 @@ private function getStore($websiteId)
* Get static data.
*
* @param Store $store
* @param int $websiteId
* @return array
*/
private function getStaticData($store, $websiteId)
private function getStaticData($store)
{
$data = [
'key' => '1',
'contactIdentifier' => $this->getEmailFromAccountInfo($websiteId),
'json' => [
'cartId' => '1',
'cartUrl' => $store->getBaseUrl(
UrlInterface::URL_TYPE_WEB,
$store->isCurrentlySecure()
).'connector/email/getbasket/quote_id/1/',
'createdDate' => date(\DateTime::ATOM, time()),
'modifiedDate' => date(\DateTime::ATOM, time()),
'currency' => 'USD',
'subTotal' => round(52, 2),
'taxAmount' => (float) 0,
'shipping' => (float) 0,
'grandTotal' => round(52, 2)
]
'cartId' => '1',
'cartUrl' => $store->getBaseUrl(
UrlInterface::URL_TYPE_WEB,
$store->isCurrentlySecure()
).'connector/email/getbasket/quote_id/1/',
'createdDate' => date(\DateTime::ATOM, time()),
'modifiedDate' => date(\DateTime::ATOM, time()),
'currency' => 'USD',
'subTotal' => round(52, 2),
'taxAmount' => (float) 0,
'shipping' => (float) 0,
'grandTotal' => round(52, 2)
];

$lineItems[] = [
Expand All @@ -159,9 +154,9 @@ private function getStaticData($store, $websiteId)
'totalPrice' => round(52, 2)
];

$data['json']['discountAmount'] = 0;
$data['json']['lineItems'] = $lineItems;
$data['json']['cartPhase'] = 'ORDER_PENDING';
$data['discountAmount'] = 0;
$data['lineItems'] = $lineItems;
$data['cartPhase'] = 'ORDER_PENDING';

return $data;
}
Expand All @@ -185,7 +180,7 @@ private function getImageUrl()
* @return string
* @throws \Exception
*/
private function getEmailFromAccountInfo($websiteId)
public function getEmailFromAccountInfo($websiteId)
{
$accountInfo = $this->helper->getWebsiteApiClient($websiteId)->getAccountInfo();
return $this->account->getAccountOwnerEmail($accountInfo);
Expand Down
2 changes: 1 addition & 1 deletion Model/Queue/Sales/CartPhaseUpdateConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function process(CartPhaseUpdateData $messageData)
$store = $this->storeManager->getStore($messageData->getStoreId());

$data = $this->cartInsightData->getPayload($quote, $store);
$data['json']['cartPhase'] = 'ORDER_COMPLETE';
$data['cartPhase'] = 'ORDER_COMPLETE';

$client = $this->clientFactory->create(
['data' => ['websiteId' => $store->getWebsiteId()]]
Expand Down
7 changes: 4 additions & 3 deletions Model/Sync/DummyRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function syncForWebsite($websiteId)
*/
private function postContactAndCartInsightData($websiteId = 0)
{
$identifier = $this->dummyData->getEmailFromAccountInfo($websiteId);
$cartInsightData = $this->dummyData->getContactInsightData($websiteId);
$client = $this->clientFactory->create(
['data' => ['websiteId' => $websiteId]]
Expand All @@ -92,7 +93,7 @@ private function postContactAndCartInsightData($websiteId = 0)
try {
$contact = $this->sdkContactFactory->create();
$contact->setMatchIdentifier('email');
$contact->setIdentifiers(['email' => $cartInsightData['contactIdentifier']]);
$contact->setIdentifiers(['email' => $identifier]);
$client->contacts->create($contact);
} catch (ResponseValidationException $e) {
if (strpos($e->getMessage(), 'identifierConflict') === false) {
Expand All @@ -110,9 +111,9 @@ private function postContactAndCartInsightData($websiteId = 0)
try {
$client->insightData->createOrUpdateContactCollectionRecord(
'CartInsight',
$cartInsightData['key'],
'1',
'email',
$cartInsightData['contactIdentifier'],
$this->dummyData->getEmailFromAccountInfo($websiteId),
$cartInsightData
);
} catch (ResponseValidationException $e) {
Expand Down
112 changes: 54 additions & 58 deletions Test/Unit/Model/AbandonedCart/CartInsight/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,25 +186,25 @@ public function testSendFunction()
->method('create')
->willReturn($this->clientMock);

$key = "12345";
$contactIdentifier = "test@emailsim.io";
$expectedPayload = $this->getMockPayload();

$this->quoteMock->expects($this->atLeastOnce())
->method('getId')
->willReturn($expectedPayload['key']);
->willReturn($key);

$this->quoteMock->expects($this->atLeastOnce())
->method('__call')
->withConsecutive(
[$this->equalTo('getQuoteCurrencyCode')],
[$this->equalTo('getCustomerEmail')],
[$this->equalTo('getSubtotal')],
[$this->equalTo('getGrandTotal')]
)
->willReturnOnConsecutiveCalls(
$expectedPayload['json']['currency'],
$expectedPayload['contactIdentifier'],
$expectedPayload['json']['subTotal'],
$expectedPayload['json']['grandTotal']
$expectedPayload['currency'],
$expectedPayload['subTotal'],
$expectedPayload['grandTotal']
);

$this->storeMock->expects($this->atLeastOnce())
Expand All @@ -215,9 +215,9 @@ public function testSendFunction()
->method('getUrl')
->with(
'connector/email/getbasket',
['quote_id' => $expectedPayload['key']]
['quote_id' => $key]
)
->willReturn($expectedPayload['json']['cartUrl']);
->willReturn($expectedPayload['cartUrl']);

// Dates
$createdAt = "2018-03-31 18:53:28";
Expand All @@ -238,8 +238,8 @@ public function testSendFunction()
[\DateTime::ATOM, $updatedAt]
)
->willReturnOnConsecutiveCalls(
$expectedPayload['json']['createdDate'],
$expectedPayload['json']['modifiedDate']
$expectedPayload['createdDate'],
$expectedPayload['modifiedDate']
);

$addressMock = $this->createMock(Quote\Address::class);
Expand All @@ -254,8 +254,8 @@ public function testSendFunction()
[$this->equalTo('getShippingAmount')]
)
->willReturnOnConsecutiveCalls(
$expectedPayload['json']['taxAmount'],
$expectedPayload['json']['shipping']
$expectedPayload['taxAmount'],
$expectedPayload['shipping']
);

// Line items loop
Expand All @@ -273,19 +273,19 @@ public function testSendFunction()

$itemsArray[0]->expects($this->once())
->method('getDiscountAmount')
->willReturn($expectedPayload['json']['discountAmount']);
->willReturn($expectedPayload['discountAmount']);

$this->productMock->expects($this->atLeastOnce())
->method('getPrice')
->willReturn($productPrice = $expectedPayload['json']['lineItems'][0]['unitPrice']);
->willReturn($productPrice = $expectedPayload['lineItems'][0]['unitPrice']);

$this->urlFinderMock->expects($this->once())
->method('fetchFor')
->willReturn($expectedPayload['json']['lineItems'][0]['productUrl']);
->willReturn($expectedPayload['lineItems'][0]['productUrl']);

$this->imageFinderMock->expects($this->once())
->method('getCartImageUrl')
->willReturn($expectedPayload['json']['lineItems'][0]['imageUrl']);
->willReturn($expectedPayload['lineItems'][0]['imageUrl']);

$this->productRepositoryMock->expects($this->atLeastOnce())
->method('get')
Expand All @@ -297,38 +297,38 @@ public function testSendFunction()

$itemsArray[0]->expects($this->atLeastOnce())
->method('getSku')
->willReturn($expectedPayload['json']['lineItems'][0]['sku']);
->willReturn($expectedPayload['lineItems'][0]['sku']);

$itemsArray[0]->expects($this->exactly(2))
->method('getName')
->willReturn($expectedPayload['json']['lineItems'][0]['name']);
->willReturn($expectedPayload['lineItems'][0]['name']);

$itemsArray[0]->expects($this->once())
->method('getBasePrice')
->willReturn($itemBasePrice = $expectedPayload['json']['lineItems'][0]['salePrice']);
->willReturn($itemBasePrice = $expectedPayload['lineItems'][0]['salePrice']);

$itemsArray[0]->expects($this->once())
->method('getPriceInclTax')
->willReturn($expectedPayload['json']['lineItems'][0]['salePrice_incl_tax']);
->willReturn($expectedPayload['lineItems'][0]['salePrice_incl_tax']);

$itemsArray[0]->expects($this->once())
->method('getRowTotal')
->willReturn($expectedPayload['json']['lineItems'][0]['totalPrice']);
->willReturn($expectedPayload['lineItems'][0]['totalPrice']);

$itemsArray[0]->expects($this->once())
->method('getRowTotalInclTax')
->willReturn($expectedPayload['json']['lineItems'][0]['totalPrice_incl_tax']);
->willReturn($expectedPayload['lineItems'][0]['totalPrice_incl_tax']);

$itemsArray[0]->expects($this->once())
->method('getQty')
->willReturn($expectedPayload['json']['lineItems'][0]['quantity']);
->willReturn($expectedPayload['lineItems'][0]['quantity']);

$this->priceCurrencyInterfaceMock
->expects($this->atLeast(2))
->method('convertAndRound')
->withConsecutive(
[$productPrice, $this->storeId, $expectedPayload['json']['currency']],
[$itemBasePrice, $this->storeId, $expectedPayload['json']['currency']]
[$productPrice, $this->storeId, $expectedPayload['currency']],
[$itemBasePrice, $this->storeId, $expectedPayload['currency']]
)
->willReturnOnConsecutiveCalls(
$productPrice,
Expand All @@ -346,9 +346,9 @@ public function testSendFunction()
public function testThatTotalPriceIsCorrectSumRegardlessOfSale()
{
$expectedPayload = $this->getMockPayload();
$salePrice = $expectedPayload['json']['lineItems'][0]['salePrice'];
$quantity = $expectedPayload['json']['lineItems'][0]['quantity'];
$totalPrice = $expectedPayload['json']['lineItems'][0]['totalPrice'];
$salePrice = $expectedPayload['lineItems'][0]['salePrice'];
$quantity = $expectedPayload['lineItems'][0]['quantity'];
$totalPrice = $expectedPayload['lineItems'][0]['totalPrice'];

/*
* totalPrice is always salePrice * quantity,
Expand Down Expand Up @@ -376,36 +376,32 @@ private function setStoreMock()
private function getMockPayload()
{
return [
"key" => "12345",
"contactIdentifier" => "test@emailsim.io",
"json" => [
"cartId" => "12345",
"cartUrl" => "https://magentostore.com/cart/12345",
"createdDate" => "2018-03-31T18:53:28+00:00",
"modifiedDate" => "2018-03-31T19:53:28+00:00",
"currency" => "GBP",
"subTotal" => 98.4,
"discountAmount" => 8.4,
"taxAmount" => 12.34,
"shipping" => 11.43,
"grandTotal" => 90,
"lineItems" => [
[
"sku" => "PRODUCT-SKU",
"imageUrl" => "https://magentostore.com/catalog/product/image.jpg",
"productUrl" => "https://magentostore.com/product/PRODUCT-SKU",
"name" => "Test Product",
"unitPrice" => 49.2,
"unitPrice_incl_tax" => 59.04,
"quantity" => "2",
"salePrice" => 46.15,
"salePrice_incl_tax" => 50.25,
"totalPrice" => 92.3,
"totalPrice_incl_tax" => 110.76
]
],
"cartPhase" => "ORDER_PENDING"
]
"cartId" => "12345",
"cartUrl" => "https://magentostore.com/cart/12345",
"createdDate" => "2018-03-31T18:53:28+00:00",
"modifiedDate" => "2018-03-31T19:53:28+00:00",
"currency" => "GBP",
"subTotal" => 98.4,
"discountAmount" => 8.4,
"taxAmount" => 12.34,
"shipping" => 11.43,
"grandTotal" => 90,
"lineItems" => [
[
"sku" => "PRODUCT-SKU",
"imageUrl" => "https://magentostore.com/catalog/product/image.jpg",
"productUrl" => "https://magentostore.com/product/PRODUCT-SKU",
"name" => "Test Product",
"unitPrice" => 49.2,
"unitPrice_incl_tax" => 59.04,
"quantity" => "2",
"salePrice" => 46.15,
"salePrice_incl_tax" => 50.25,
"totalPrice" => 92.3,
"totalPrice_incl_tax" => 110.76
]
],
"cartPhase" => "ORDER_PENDING"
];
}
}
22 changes: 9 additions & 13 deletions Test/Unit/Model/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,15 @@ public function testRegisterQueueReturnsFalseIfFileDoNotExists()
*/
private function getData()
{
return $data = [
'key' => 1,
'contactIdentifier' => 'testContactIdentifier',
'json' => [
'cartId' => 1,
'cartUrl' => 'http://sampleurl.io/cartid/12',
'createdDate' => 'sampleDate',
'modifiedDate' => 'sampleDate',
'currency' => 'GBP',
'subTotal' => '120.00',
'taxAmount' => '20.00',
'grandTotal' => '140.00'
]
return [
'cartId' => 1,
'cartUrl' => 'http://sampleurl.io/cartid/12',
'createdDate' => 'sampleDate',
'modifiedDate' => 'sampleDate',
'currency' => 'GBP',
'subTotal' => '120.00',
'taxAmount' => '20.00',
'grandTotal' => '140.00'
];
}
}

0 comments on commit 914d39d

Please sign in to comment.