Skip to content
Snippets Groups Projects
Select Git revision
  • 10570227e1bf7bf6f77e469f93733229d53ebf50
  • dev default protected
  • prod protected
  • 1.0.58
  • 1.0.57
  • 1.0.52
  • 1.0.56
  • 1.0.51
  • 1.0.50
  • 1.0.33
  • 1.0.32
  • 1.0.31
  • 1.0.30
  • 1.0.29
  • 1.0.28
  • 1.0.27
  • 1.0.26
  • 1.0.25
  • 1.0.24
  • 1.0.23
  • 1.0.22
  • 1.0.21
  • 1.0.20
23 results

OrderUpdateWebhook.php

Blame
  • OrderUpdateWebhook.php 4.21 KiB
    <?php
    
    namespace BobGroup\BobGo\Observer;
    
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\HTTP\Client\Curl;
    use BobGroup\BobGo\Model\Carrier\UData;
    use Psr\Log\LoggerInterface;
    use Magento\Store\Model\StoreManagerInterface;
    
    class OrderUpdateWebhook implements ObserverInterface
    {
        protected Curl $curl;
        protected LoggerInterface $logger;
        protected StoreManagerInterface $storeManager;
    
        public function __construct(LoggerInterface $logger, Curl $curl, StoreManagerInterface $storeManager)
        {
            $this->logger = $logger;
            $this->curl = $curl;
            $this->storeManager = $storeManager;
        }
    
        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');
        }
    
        private function sendWebhook($order, $eventType)
        {
            // Webhook URL
            $url = $this->getWebhookUrl();
    
            // Extract order items
            $itemsData = [];
            foreach ($order->getAllItems() as $item) {
                $itemsData[] = $item->getData();
            }
    
            // Extract shipping address
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressData = $shippingAddress ? $shippingAddress->getData() : [];
    
            // Extract billing address
            $billingAddress = $order->getBillingAddress();
            $billingAddressData = $billingAddress ? $billingAddress->getData() : [];
    
            $storeId = $this->getStoreId();
    
            // Prepare payload
            $data = [
                'event' => $eventType,
                'order_id' => $order->getId(),
                'channel_identifier' => $this->getStoreUrl(),
                'store_id' => $storeId,
                //'order_data' => $order->getData(),
                //'items' => $itemsData,
                //'shipping_address' => $shippingAddressData,
                //'billing_address'  => $billingAddressData,
            ];
    
            // Send the webhook
            $this->makeHttpPostRequest($url, $data, $storeId);