diff --git a/DataLayer/Event/Purchase.php b/DataLayer/Event/Purchase.php
index fb947cf..f71cc58 100644
--- a/DataLayer/Event/Purchase.php
+++ b/DataLayer/Event/Purchase.php
@@ -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;
@@ -15,15 +17,18 @@ 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;
}
/**
@@ -31,7 +36,14 @@ public function __construct(
*/
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',
@@ -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,
+ ];
+ }
}
diff --git a/Observer/TriggerPurchaseDataLayerEvent.php b/Observer/TriggerPurchaseDataLayerEvent.php
index c4ceadb..cb2dffe 100644
--- a/Observer/TriggerPurchaseDataLayerEvent.php
+++ b/Observer/TriggerPurchaseDataLayerEvent.php
@@ -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;
}
@@ -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,
- ];
- }
}
diff --git a/view/frontend/layout/checkout_onepage_success.xml b/view/frontend/layout/checkout_onepage_success.xml
index 0db700d..5e83dfc 100644
--- a/view/frontend/layout/checkout_onepage_success.xml
+++ b/view/frontend/layout/checkout_onepage_success.xml
@@ -18,6 +18,8 @@
+ - Yireo\GoogleTagManager2\DataLayer\Event\Purchase
+