|
4 | 4 |
|
5 | 5 | use Craft; |
6 | 6 | use craft\commerce\elements\Order; |
| 7 | +use craft\commerce\enums\LineItemType; |
7 | 8 | use craft\commerce\events\OrderStatusEvent; |
8 | 9 | use craft\commerce\events\RefundTransactionEvent; |
9 | 10 | use craft\commerce\events\TransactionEvent; |
| 11 | +use craft\commerce\models\LineItem; |
10 | 12 | use craft\elements\Address; |
11 | 13 | use craft\helpers\ArrayHelper; |
12 | 14 | use DateTime; |
@@ -285,54 +287,16 @@ protected function createProfile(array $profile, ?string $eventName = null, mixe |
285 | 287 |
|
286 | 288 | protected function getOrderDetails(Order $order, string $event = ''): array |
287 | 289 | { |
288 | | - /** @var Settings $settings */ |
289 | | - $settings = Plugin::getInstance()->getSettings(); |
290 | | - |
291 | 290 | $lineItemsProperties = []; |
292 | 291 |
|
293 | 292 | foreach ($order->lineItems as $lineItem) { |
294 | | - $lineItemProperties = []; |
295 | | - |
296 | | - // Add regular Product purchasable properties |
297 | | - $product = $lineItem->purchasable->product ?? []; |
298 | | - if ($product) { |
299 | | - $lineItemProperties = [ |
300 | | - 'value' => $lineItem->price * $lineItem->qty, |
301 | | - 'ProductName' => $product->title, |
302 | | - 'Slug' => $product->slug, |
303 | | - 'ProductURL' => $product->getUrl(), |
304 | | - 'ProductType' => $product->type->name, |
305 | | - 'ItemPrice' => $lineItem->price, |
306 | | - 'RowTotal' => $lineItem->subtotal, |
307 | | - 'Quantity' => $lineItem->qty, |
308 | | - 'SKU' => $lineItem->purchasable->sku, |
309 | | - 'Options' => $lineItem->getOptions(), |
310 | | - 'Adjustments' => collect($lineItem->getAdjustments()) |
311 | | - ->flatMap(static fn ($adjustment) => [ |
312 | | - $adjustment->name => $adjustment->amountAsCurrency, |
313 | | - ]) |
314 | | - ->toArray(), |
315 | | - |
316 | | - ]; |
317 | | - |
318 | | - $variant = $lineItem->purchasable; |
319 | | - |
320 | | - $productImageField = $settings->productImageField; |
321 | | - |
322 | | - if (isset($variant->{$productImageField}) && $variant->{$productImageField}->count()) { |
323 | | - if ($image = $variant->{$productImageField}->one()) { |
324 | | - $lineItemProperties['ImageURL'] = $image->getUrl($settings->productImageFieldTransformation, true); |
325 | | - } |
326 | | - } elseif (isset($product->{$productImageField}) && $product->{$productImageField}->count()) { |
327 | | - if ($image = $product->{$productImageField}->one()) { |
328 | | - $lineItemProperties['ImageURL'] = $image->getUrl($settings->productImageFieldTransformation, true); |
329 | | - } |
330 | | - } |
| 293 | + if ($lineItem->type !== LineItemType::Custom) { |
| 294 | + $lineItemProperties = $this->populateLineItemProperties($lineItem); |
331 | 295 | } |
332 | 296 |
|
333 | 297 | // Add any additional user-defined properties |
334 | 298 | $addLineItemCustomPropertiesEvent = new AddLineItemCustomPropertiesEvent([ |
335 | | - 'properties' => $lineItemProperties, |
| 299 | + 'properties' => $lineItemProperties ?? [], |
336 | 300 | 'order' => $order, |
337 | 301 | 'lineItem' => $lineItem, |
338 | 302 | 'event' => $event, |
@@ -361,6 +325,53 @@ protected function getOrderDetails(Order $order, string $event = ''): array |
361 | 325 | return $addOrderCustomPropertiesEvent->properties; |
362 | 326 | } |
363 | 327 |
|
| 328 | + private function populateLineItemProperties(LineItem $lineItem): array |
| 329 | + { |
| 330 | + /** @var Settings $settings */ |
| 331 | + $settings = Plugin::getInstance()->getSettings(); |
| 332 | + |
| 333 | + $lineItemProperties = []; |
| 334 | + |
| 335 | + // Add regular Product purchasable properties |
| 336 | + $product = $lineItem->purchasable?->product ?? []; |
| 337 | + if ($product !== []) { |
| 338 | + $lineItemProperties = [ |
| 339 | + 'value' => $lineItem->price * $lineItem->qty, |
| 340 | + 'ProductName' => $product->title, |
| 341 | + 'Slug' => $product->slug, |
| 342 | + 'ProductURL' => $product->getUrl(), |
| 343 | + 'ProductType' => $product->type->name, |
| 344 | + 'ItemPrice' => $lineItem->price, |
| 345 | + 'RowTotal' => $lineItem->subtotal, |
| 346 | + 'Quantity' => $lineItem->qty, |
| 347 | + 'SKU' => $lineItem->purchasable->sku, |
| 348 | + 'Options' => $lineItem->getOptions(), |
| 349 | + 'Adjustments' => collect($lineItem->getAdjustments()) |
| 350 | + ->flatMap(static fn ($adjustment) => [ |
| 351 | + $adjustment->name => $adjustment->amountAsCurrency, |
| 352 | + ]) |
| 353 | + ->toArray(), |
| 354 | + |
| 355 | + ]; |
| 356 | + |
| 357 | + $variant = $lineItem->purchasable; |
| 358 | + |
| 359 | + $productImageField = $settings->productImageField; |
| 360 | + |
| 361 | + if (isset($variant->{$productImageField}) && $variant->{$productImageField}->count()) { |
| 362 | + if ($image = $variant->{$productImageField}->one()) { |
| 363 | + $lineItemProperties['ImageURL'] = $image->getUrl($settings->productImageFieldTransformation, true); |
| 364 | + } |
| 365 | + } elseif (isset($product->{$productImageField}) && $product->{$productImageField}->count()) { |
| 366 | + if ($image = $product->{$productImageField}->one()) { |
| 367 | + $lineItemProperties['ImageURL'] = $image->getUrl($settings->productImageFieldTransformation, true); |
| 368 | + } |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + return $lineItemProperties; |
| 373 | + } |
| 374 | + |
364 | 375 | private function isInGroup(array $selectedGroups, array $userGroups): bool |
365 | 376 | { |
366 | 377 | $groupIds = ArrayHelper::getColumn($userGroups, 'id'); |
|
0 commit comments