2

I need to get the website_id of the website from which the order is placed in my observer as my project includes multiwebsite functionality.

Please help.

Thanks in advance.

/app/code/Vendor/PushNotifications/Observer/SendPushNotifications.php

<?php

namespace Vendor\PushNotifications\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Customer\Model\SessionFactory;
use \Magento\Customer\Model\CustomerFactory;
use \Magento\Framework\HTTP\ZendClient;
use \Magento\Framework\HTTP\Adapter\Curl;
use \Amos\PushNotifications\Api\NotificationsRepositoryInterface;
use \Magento\Framework\App\Filesystem\DirectoryList;
use \Magento\Store\Model\StoreManagerInterface;

class SendPushNotifications implements ObserverInterface {

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;
    protected $customerSessionFactory;
    protected $_customerFactory;
    protected $_storeManager;
    protected $_httpAdapter;
    protected $_notificationsRepository;
    protected $_directoryList;

    /**
     * Recipient email config path
     */
    const XML_PATH_IS_ENABLED_NOTIFICATION = 'customNotifications/general/enable';
    const XML_PATH_ANDROID_FIREBASE_SERVER_KEY = 'customNotifications/general/androidFirebaseServerKey';
    const DEVICE_TYPE = ['1' => 'ANDROID', '2' => 'IOS'];
    const FIREBASE_URL = "https://fcm.googleapis.com/fcm/send";

    public function __construct(
    DirectoryList $directoryList, NotificationsRepositoryInterface $notificationsRepository, Curl $httpAdapter, StoreManagerInterface $storeManager, CustomerFactory $customerFactory, SessionFactory $customerSessionFactory, ScopeConfigInterface $scopeConfig
    ) {
        $this->_storeManager = $storeManager;
        $this->scopeConfig = $scopeConfig;
        $this->customerSessionFactory = $customerSessionFactory;
        $this->_customerFactory = $customerFactory;
        $this->_httpAdapter = $httpAdapter;
        $this->_notificationsRepository = $notificationsRepository;
        $this->_directoryList = $directoryList;
    }

    /**
     * customer register event handler
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $isEnable = $this->scopeConfig->getValue(self::XML_PATH_IS_ENABLED_NOTIFICATION, $storeScope,'2');
        $customerSession = $this->customerSessionFactory->create();
        $customerFactory = $this->_customerFactory->create();
        $notification = $observer->getData('notification');
        if ($isEnable) {

//            $notification = $observer->getData('notification');
            $customerId = ($customerSession->isLoggedIn()) ? $customerSession->getCustomerId() : $notification['customer_id'];
            $customerFactory->setWebsiteId('2');
            $customerData = $customerFactory->load($customerId);
            // Modified for common notification - By Deepak
            if ($customerData->getId()) {
                $deviceType = $customerData->getData('device_type');
                return $this->sendPushNotificationToDevice($deviceType, [$customerData->getData('device_token')], $notification);
            }
        }
    }
    // Modified for common notification - By Deepak
    private function sendPushNotificationToDevice($deviceType, $deviceToken, $notification) {
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $androidFirebaseServerKey = $this->scopeConfig->getValue(self::XML_PATH_ANDROID_FIREBASE_SERVER_KEY, $storeScope);

        $result = $this->_notificationsRepository->save($notification['customer_id'], $notification['message'], $notification['type'], $notification['targat_id'], $notification['status']);


        if ($result->getStatus()) {
            $notification['notification_id'] = $result->getNotificationId();
            $notification['body'] = $notification['message'];
            unset($notification['message']);
            $fields['registration_ids'] = $deviceToken;
            if ($deviceType) {
                if ($deviceType == 1) {
                    $fields['data'] = $notification;
                } elseif ($deviceType == 2) {
                    $notification['sound'] = 'Enabled';
                    $fields['notification'] = $notification;
                }
            }

            $headers = [
                'Authorization: key=' . $androidFirebaseServerKey,
                'Content-Type: application/json',
                'Content-Length: ' . strlen(json_encode($fields))
            ];

            $this->_httpAdapter->write(\Zend_Http_Client::POST, self::FIREBASE_URL, '1.1', $headers, json_encode($fields));
            $response = $this->_httpAdapter->read();
            $this->_httpAdapter->close();
        }
    }

}

Vendor/PushNotifications/etc/events.xml

<?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="customer_notification_dispatch">
        <observer name="Customer_Notification_Dispatch" instance="Vendor\PushNotifications\Observer\SendPushNotifications" />
    </event>
</config>
  • 1
    Which observer you have using? – Amit Bera Apr 11 at 7:44
  • @AmitBera I have added the code in my question. – Flying Finner Apr 11 at 8:20
  • please update your event name – Arunprabakaran M Apr 11 at 8:39
  • which event you have using or want to use? – Amit Bera Apr 11 at 8:50
2

Try this for getting website id from an order in observer file

$websiteid=$order->getStore()->getWebsiteId();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.