|
| 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 | +} |
0 commit comments