0

I have searched sms code for magento 2. But there is only purchased extension only there. Is it possible to integrate sms in magento 2 by only coding?

1
  • On which event you want to send sms? Aug 17, 2018 at 15:24

1 Answer 1

0

Suppose, you may be want to fire SMS on order status `pending, processing, complete.

So, in that case, you can use Events and observers

Events are dispatched by modules when certain actions are triggered. In addition to its own events, Magento allows you to create your own events that can be dispatched in your code. When an event is dispatched, it can pass data to any observers configured to watch that event.

Events and observers provide a point where you can run some code.

For you can use sales_order_save_commit_after event, at this event fire run an observer which will track order status Change.

On event this event, you can get old Data: using $Object->getOrigData();

and you can get new Data using $object->getData(),SO here you can get order status changs easily and fire the sms

namespace MyCompany\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class MyObserver implements ObserverInterface
{

  public function execute(\Magento\Framework\Event\Observer $observer)
  {
     $order = $observer->getEvent()->getOrder();

        if (!$order->getId()) {

            return $this;
        }



        $OldData=$order->getOrigData();
        $NewData=$order->getData();
        $OldState==$order->getOrigData('state');
        $NewState=$order->getState();
        $OldStatus=$order->getOrigData('status');
        $NewStatus=$order->getStatus();


        /* Fire sms when New Status  and  old Statu are not same */
        if($OldStatus!=$NewStatus){
            //fire sms;
      // Write your  sms code at here               
        }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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