Skip to content

Commit 1680068

Browse files
committed
Added hyva support / added user_data tag order
1 parent e8261c9 commit 1680068

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

DataLayer/Tag/Order/UserData.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tagging\GTM\DataLayer\Tag\Order;
6+
7+
use Magento\Checkout\Model\Session as CheckoutSession;
8+
use Magento\Sales\Api\Data\OrderInterface;
9+
use Magento\Sales\Api\OrderRepositoryInterface;
10+
use Tagging\GTM\Api\Data\TagInterface;
11+
12+
class UserData implements TagInterface
13+
{
14+
private CheckoutSession $checkoutSession;
15+
private OrderRepositoryInterface $orderRepository;
16+
17+
public function __construct(
18+
CheckoutSession $checkoutSession,
19+
OrderRepositoryInterface $orderRepository
20+
) {
21+
$this->checkoutSession = $checkoutSession;
22+
$this->orderRepository = $orderRepository;
23+
}
24+
25+
/**
26+
* @return array
27+
*/
28+
public function get(): array
29+
{
30+
$order = $this->getOrder();
31+
$billingAddress = $order->getBillingAddress();
32+
$shippingAddress = $order->getShippingAddress();
33+
34+
return [
35+
'customer_id' => $order->getCustomerId() ?? '',
36+
'billing_first_name' => $billingAddress ? $billingAddress->getFirstname() ?? '' : '',
37+
'billing_last_name' => $billingAddress ? $billingAddress->getLastname() ?? '' : '',
38+
'billing_address' => $billingAddress && $billingAddress->getStreet() ? $billingAddress->getStreet()[0] ?? '' : '',
39+
'billing_postcode' => $billingAddress ? $billingAddress->getPostcode() ?? '' : '',
40+
'billing_country' => $billingAddress ? $billingAddress->getCountryId() ?? '' : '',
41+
'billing_state' => $billingAddress ? $billingAddress->getRegion() ?? '' : '',
42+
'billing_city' => $billingAddress ? $billingAddress->getCity() ?? '' : '',
43+
'billing_email' => $billingAddress ? $billingAddress->getEmail() ?? '' : '',
44+
'billing_phone' => $billingAddress ? $billingAddress->getTelephone() ?? '' : '',
45+
'shipping_first_name' => $shippingAddress ? $shippingAddress->getFirstname() ?? '' : '',
46+
'shipping_last_name' => $shippingAddress ? $shippingAddress->getLastname() ?? '' : '',
47+
'shipping_company' => $shippingAddress ? $shippingAddress->getCompany() ?? '' : '',
48+
'shipping_address' => $shippingAddress && $shippingAddress->getStreet() ? $shippingAddress->getStreet()[0] ?? '' : '',
49+
'shipping_postcode' => $shippingAddress ? $shippingAddress->getPostcode() ?? '' : '',
50+
'shipping_country' => $shippingAddress ? $shippingAddress->getCountryId() ?? '' : '',
51+
'shipping_state' => $shippingAddress ? $shippingAddress->getRegion() ?? '' : '',
52+
'shipping_city' => $shippingAddress ? $shippingAddress->getCity() ?? '' : '',
53+
'shipping_phone' => $shippingAddress ? $shippingAddress->getTelephone() ?? '' : '',
54+
'email' => $order->getCustomerEmail() ?? '',
55+
'first_name' => $order->getCustomerFirstname() ?? '',
56+
'last_name' => $order->getCustomerLastname() ?? '',
57+
'new_customer' => $order->getCustomerIsGuest() ? 'true' : 'false'
58+
];
59+
}
60+
61+
/**
62+
* @return OrderInterface
63+
*/
64+
private function getOrder(): OrderInterface
65+
{
66+
return $this->orderRepository->get($this->checkoutSession->getLastRealOrder()->getId());
67+
}
68+
}

view/frontend/layout/checkout_onepage_success.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<item name="order" xsi:type="object">Tagging\GTM\DataLayer\Tag\Order\Order</item>
2525
<item name="items" xsi:type="object">Tagging\GTM\DataLayer\Tag\Order\OrderItems</item>
2626
</item>
27+
<item name="user_data" xsi:type="object">Tagging\GTM\DataLayer\Tag\Order\UserData</item>
2728
</item>
2829
</argument>
2930

view/frontend/templates/hyva/script-additions.phtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ $commons = $block->getCommonsViewModel();
3434
}
3535

3636
var getSectionNames = function() {
37-
return ['cart', 'customer'];
37+
return ['cart', 'customer', 'gtm-checkout'];
3838
}
3939

4040
getSectionNames().forEach(function(sectionName) {
41-
if (!event.detail.data[sectionName].gtm) {
41+
if (!event.detail.data[sectionName] || !event.detail.data[sectionName].gtm) {
4242
return;
4343
}
4444

@@ -52,7 +52,7 @@ $commons = $block->getCommonsViewModel();
5252

5353
let attributes = {};
5454
getSectionNames().forEach(function(sectionName) {
55-
if (!event.detail.data[sectionName].gtm_events) {
55+
if (!event.detail.data[sectionName] || !event.detail.data[sectionName].gtm_events) {
5656
return;
5757
}
5858

0 commit comments

Comments
 (0)