|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magefan ([email protected]). All rights reserved. |
| 4 | + * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magefan\GoogleTagManager\Model\DataLayer; |
| 10 | + |
| 11 | +use Magefan\GoogleTagManager\Model\AbstractDataLayer; |
| 12 | +use Magefan\GoogleTagManager\Model\Config; |
| 13 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 14 | +use Magento\Sales\Model\Order; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magefan\GoogleTagManager\Api\DataLayer\Order\ItemInterface; |
| 17 | + |
| 18 | +abstract class AbstractOrder extends AbstractDataLayer |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var ItemInterface |
| 22 | + */ |
| 23 | + private $gtmItem; |
| 24 | + |
| 25 | + /** |
| 26 | + * Purchase constructor. |
| 27 | + * |
| 28 | + * @param Config $config |
| 29 | + * @param StoreManagerInterface $storeManager |
| 30 | + * @param CategoryRepositoryInterface $categoryRepository |
| 31 | + * @param ItemInterface $gtmItem |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + Config $config, |
| 35 | + StoreManagerInterface $storeManager, |
| 36 | + CategoryRepositoryInterface $categoryRepository, |
| 37 | + ItemInterface $gtmItem |
| 38 | + ) { |
| 39 | + $this->gtmItem = $gtmItem; |
| 40 | + parent::__construct($config, $storeManager, $categoryRepository); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritDoc |
| 45 | + */ |
| 46 | + public function get(Order $order, string $requester = ''): array |
| 47 | + { |
| 48 | + if ($order) { |
| 49 | + $items = []; |
| 50 | + foreach ($order->getAllVisibleItems() as $item) { |
| 51 | + $items[] = $this->gtmItem->get($item); |
| 52 | + } |
| 53 | + |
| 54 | + return $this->eventWrap([ |
| 55 | + 'event' => $this->getEventName(), |
| 56 | + 'ecommerce' => [ |
| 57 | + 'transaction_id' => $order->getIncrementId(), |
| 58 | + 'value' => $this->getValue($order), |
| 59 | + 'tax' => $this->formatPrice((float)$order->getTaxAmount()), |
| 60 | + 'shipping' => $this->formatPrice((float)$order->getShippingAmount()), |
| 61 | + 'currency' => $this->getCurrentCurrencyCode(), |
| 62 | + 'coupon' => $order->getCouponCode() ?: '', |
| 63 | + 'items' => $items |
| 64 | + ], |
| 65 | + 'is_virtual' => (bool)$order->getIsVirtual(), |
| 66 | + 'shipping_description' => $order->getShippingDescription(), |
| 67 | + 'customer_is_guest' => (bool)$order->getCustomerIsGuest(), |
| 68 | + 'customer_identifier' => hash('sha256', (string)$order->getCustomerEmail()), |
| 69 | + ]); |
| 70 | + } |
| 71 | + |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param Order $order |
| 77 | + * @return float |
| 78 | + */ |
| 79 | + protected function getValue(Order $order): float |
| 80 | + { |
| 81 | + $orderValue = (float)$order->getGrandTotal(); |
| 82 | + |
| 83 | + if (!$this->config->isPurchaseTaxEnabled()) { |
| 84 | + $orderValue -= $order->getTaxAmount(); |
| 85 | + } |
| 86 | + |
| 87 | + if (!$this->config->isPurchaseShippingEnabled()) { |
| 88 | + $orderValue -= $order->getShippingAmount(); |
| 89 | + } |
| 90 | + |
| 91 | + return $this->formatPrice($orderValue); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @return string |
| 96 | + */ |
| 97 | + abstract protected function getEventName(): string; |
| 98 | +} |
0 commit comments