Skip to content

Commit

Permalink
Make sure purchase event is dealt with equally everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Sep 12, 2024
1 parent 97540b7 commit 1f27f47
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
47 changes: 45 additions & 2 deletions DataLayer/Event/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Yireo\GoogleTagManager2\DataLayer\Event;

use Magento\Checkout\Model\Session;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Yireo\GoogleTagManager2\Api\Data\EventInterface;
use Yireo\GoogleTagManager2\Config\Config;
use Yireo\GoogleTagManager2\DataLayer\Tag\Order\OrderItems;
Expand All @@ -15,23 +17,33 @@ class Purchase implements EventInterface
private OrderItems $orderItems;
private Config $config;
private PriceFormatter $priceFormatter;
private Session $checkoutSession;

public function __construct(
OrderItems $orderItems,
Config $config,
PriceFormatter $priceFormatter
PriceFormatter $priceFormatter,
Session $checkoutSession
) {
$this->orderItems = $orderItems;
$this->config = $config;
$this->priceFormatter = $priceFormatter;
$this->checkoutSession = $checkoutSession;
}

/**
* @return string[]
*/
public function get(): array
{
$order = $this->order;
$order = $this->getOrder();
if (false === $order instanceof OrderInterface) {
return [];
}

if (false === in_array($order->getState(), $this->getOrderStates())) {
return [];
}

return [
'event' => 'purchase',
Expand All @@ -57,4 +69,35 @@ public function setOrder(OrderInterface $order): Purchase
$this->order = $order;
return $this;
}

private function getOrder(): ?OrderInterface
{
if ($this->order instanceof OrderInterface) {
return $this->order;
}

$this->order = $this->checkoutSession->getLastRealOrder();
return $this->order;
}

private function getOrderStates(): array
{
$orderStates = $this->config->getOrderStatesForPurchaseEvent();
if (!empty($orderStates)){
return $orderStates;
}

return $this->getDefaultOrderStates();
}

private function getDefaultOrderStates(): array
{
return [
Order::STATE_PENDING_PAYMENT,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_HOLDED,
Order::STATE_PROCESSING,
Order::STATE_COMPLETE,
];
}
}
31 changes: 3 additions & 28 deletions Observer/TriggerPurchaseDataLayerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Yireo\GoogleTagManager2\Api\CheckoutSessionDataProviderInterface;
use Yireo\GoogleTagManager2\Config\Config;
use Yireo\GoogleTagManager2\DataLayer\Event\Purchase as PurchaseEvent;

class TriggerPurchaseDataLayerEvent implements ObserverInterface
{
private CheckoutSessionDataProviderInterface $checkoutSessionDataProvider;
private PurchaseEvent $purchaseEvent;
private Config $config;

public function __construct(
CheckoutSessionDataProviderInterface $checkoutSessionDataProvider,
PurchaseEvent $purchaseEvent,
Config $config
PurchaseEvent $purchaseEvent
) {
$this->checkoutSessionDataProvider = $checkoutSessionDataProvider;
$this->purchaseEvent = $purchaseEvent;
$this->config = $config;
}

public function execute(Observer $observer)
{
/** @var OrderInterface $order */
$order = $observer->getData('order');
if (false === in_array($order->getStatus(), $this->getOrderStates())) {
$purchaseEventData = $this->purchaseEvent->setOrder($order)->get();
if (empty($purchaseEventData)) {
return;
}

Expand All @@ -39,25 +35,4 @@ public function execute(Observer $observer)
$this->purchaseEvent->setOrder($order)->get()
);
}

private function getOrderStates(): array
{
$orderStates = $this->config->getOrderStatesForPurchaseEvent();
if (!empty($orderStates)){
return $orderStates;
}

return $this->getDefaultOrderStates();
}

private function getDefaultOrderStates(): array
{
return [
Order::STATE_PENDING_PAYMENT,
Order::STATE_PAYMENT_REVIEW,
Order::STATE_HOLDED,
Order::STATE_PROCESSING,
Order::STATE_COMPLETE,
];
}
}
3 changes: 3 additions & 0 deletions view/frontend/layout/checkout_onepage_success.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
</argument>

<argument name="data_layer_events" xsi:type="array">
<item name="purchase" xsi:type="object">Yireo\GoogleTagManager2\DataLayer\Event\Purchase</item>
<!--
<item name="purchase_event" xsi:type="array">
<item name="event" xsi:type="string">purchase</item>
<item name="ecommerce" xsi:type="array">
<item name="order" xsi:type="object">Yireo\GoogleTagManager2\DataLayer\Tag\Order\Order</item>
<item name="items" xsi:type="object">Yireo\GoogleTagManager2\DataLayer\Tag\Order\OrderItems</item>
</item>
</item>
-->
</argument>

<argument name="data_layer" xsi:type="array">
Expand Down

0 comments on commit 1f27f47

Please sign in to comment.