Skip to content

Commit

Permalink
Allow configuring which order states should lead to purchase event
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Sep 12, 2024
1 parent f1091cc commit 97540b7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.9.9] - 12 September 2024
### Fixed
- Allow configuring which order states should lead to `purchase` event

## [3.9.8] - 7 September 2024
### Fixed
- Fix Uncaught TypeError: products.forEach is not a function #246 @nahall
Expand Down
5 changes: 5 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ public function getProductListValueOnCategory(): string
return (string)$this->getModuleConfigValue('product_list_value_on_category');
}

public function getOrderStatesForPurchaseEvent(): array
{
return explode(',', (string)$this->getModuleConfigValue('order_states_for_purchase_event'));
}

/**
* Return a configuration value
*
Expand Down
29 changes: 27 additions & 2 deletions Observer/TriggerPurchaseDataLayerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@
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
PurchaseEvent $purchaseEvent,
Config $config
) {
$this->checkoutSessionDataProvider = $checkoutSessionDataProvider;
$this->purchaseEvent = $purchaseEvent;
$this->config = $config;
}

public function execute(Observer $observer)
{
/** @var OrderInterface $order */
$order = $observer->getData('order');
if ($order->getStatus() === Order::STATE_CANCELED) {
if (false === in_array($order->getStatus(), $this->getOrderStates())) {
return;
}

Expand All @@ -35,4 +39,25 @@ 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,
];
}
}
8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@
</depends>
<comment>Generate the view_cart event only when expanding the minicart</comment>
</field>
<field id="order_states_for_purchase_event" type="multiselect" translate="label" sortOrder="16" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Orders states for purchase event</label>
<source_model>Yireo\GoogleTagManager2\Config\Source\OrderStateOptions</source_model>
<comment>Order states at which to trigger the purchase event</comment>
<depends>
<field id="enabled">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<view_cart_occurances>everywhere</view_cart_occurances>
<view_cart_on_mini_cart_expand_only>0</view_cart_on_mini_cart_expand_only>
<product_list_value_on_category>product_first_category</product_list_value_on_category>
<order_states_for_purchase_event>payment_review,pending_payment,holded,processing,complete</order_states_for_purchase_event>
</settings>
</googletagmanager2>
</default>
Expand Down

0 comments on commit 97540b7

Please sign in to comment.