Skip to content

Commit

Permalink
Merge pull request #110 from FosterCommerce/fix/missing-array-key-slu…
Browse files Browse the repository at this point in the history
…g-when-variants-deleted

Fix/missing array key slug when variants deleted
  • Loading branch information
peteeveleigh authored Mar 31, 2023
2 parents 078b5cb + 9b210cb commit 0b2bce2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Release Notes for KlaviyoConnect

## 4.0.16 2023-03-31
### Fixed

- Issue that caused queue faliures when pushing order data to Klaviyo if any of the orders contained line items for deleted variants

## 4.0.15

## 4.0.14

## 4.0.13 2022-07-04
### Added

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "fostercommerce/klaviyoconnect",
"description": "Craft Commerce",
"type": "craft-plugin",
"version": "4.0.15",
"version": "4.0.16",
"keywords": [
"klaviyo"
],
Expand Down
28 changes: 26 additions & 2 deletions src/services/Track.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public function trackOrder($eventName, $order, $profile = null, $timestamp = nul
);

Plugin::getInstance()->api->track($eventName, $profile, $eventProperties, $timestamp);

if ($eventName === 'Placed Order') {
foreach ($orderDetails['Items'] as $item) {
$event = [
Expand Down Expand Up @@ -353,7 +353,31 @@ protected function getOrderDetails($order, $event = '') // no return type as mix

foreach ($order->lineItems as $lineItem) {
$lineItemProperties = [];


// set some defaults
// This is a messy, temporary fix and needs refactoring
// If a variant has been deleted since an order was placed the array assignment for Klaviyo event properties is skipped
// since there is no "product"
// this means that when creating the event ID in trackOrder() (around line 317), there is no key named "Slug".
// It was possible to get around this by ignoring "Slug", since it's always going to be null anyway,
// but this also meant that no record of the product was sent to Kalviyo.
// This "fix" makes sure there is *some* data to send to Klaviyo if the Variant has been deleted
// We should probably refactor this part to always use the lineitem snapshot
$lineItemProperties = [
'value' => $lineItem->price * $lineItem->qty,
'ProductName' => $lineItem->snapshot['title'],
'Slug' => $lineItem->snapshot['slug'],
'ProductURL' => $lineItem->snapshot['url'],
'ProductType' => '',
'ItemPrice' => $lineItem->snapshot['price'],
'RowTotal' => $lineItem->subtotal,
'Quantity' => $lineItem->qty,
'SKU' => $lineItem->snapshot['sku'],
'Note' => 'Variant no longer available',
'Snapshot' => $lineItem->snapshot
];


// Add regular Product purchasable properties
$product = $lineItem->purchasable->product ?? [];
if ($product) {
Expand Down

0 comments on commit 0b2bce2

Please sign in to comment.