Skip to content
Snippets Groups Projects
Commit 2c7e23cd authored by Christel Loftus's avatar Christel Loftus
Browse files

orderId for both created and updated

parent 9d91f05a
No related branches found
No related tags found
2 merge requests!201.0.41,!14Start webhook implementation
......@@ -14,6 +14,6 @@ class OrderCreateWebhook extends OrderWebhookBase
}
// Extract order data and send to the webhook URL
$this->sendWebhook($order, 'order_created');
$this->sendWebhook($order);
}
}
<?php
namespace BobGroup\BobGo\Observer;
use Magento\Framework\Event\Observer;
class OrderUpdateWebhook extends OrderWebhookBase
{
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
if (!$order) {
return;
}
// Extract order data and send to the webhook URL
$this->sendWebhook($order, 'order_updated');
}
}
......@@ -21,17 +21,36 @@ abstract class OrderWebhookBase implements ObserverInterface
$this->storeManager = $storeManager;
}
protected function sendWebhook($order, $eventType)
protected function sendWebhook($order)
{
// Webhook URL
$url = $this->getWebhookUrl();
$storeId = $this->getStoreId();
$orderId = $order->getId();
// Get the order creation time
$createdAt = strtotime($order->getCreatedAt());
$currentTime = time();
// Define a time threshold to consider the order as newly created
$threshold = 5; // 5 seconds
// Determine the event type based on the creation time
if (($currentTime - $createdAt) < $threshold) {
$eventType = 'order_created';
} else {
$eventType = 'order_updated';
}
// Log the event type for debugging purposes
$this->logger->info('event: ' . $eventType);
// Prepare payload
$data = [
'event' => $eventType,
'order_id' => $order->getId(),
'order_id' => $orderId,
'channel_identifier' => $this->getStoreUrl(),
'store_id' => $storeId,
];
......
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
<observer name="order_create_webhook" instance="BobGroup\BobGo\Observer\OrderCreateWebhook"/>
</event>
<event name="sales_order_save_after">
<observer name="order_update_webhook" instance="BobGroup\BobGo\Observer\OrderUpdateWebhook"/>
<observer name="order_create_webhook" instance="BobGroup\BobGo\Observer\OrderCreateWebhook"/>
</event>
</config>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment