Skip to main content

How to

Send an order note to an external system

<?php

namespace MyVendor\MyModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Simpler\Checkout\Event\Event;
use Simpler\Checkout\Event\Submit\AfterOrderNoteCreatedEvent;

class MyObserver implements ObserverInterface
{
public function execute(Observer $observer)
{
/** @var AfterOrderNoteCreatedEvent $event */
$event = $observer->getData(Event::KEY);

if ($note = $event->getNote()){
$orderId = $event->getOrder()->getEntityId();
// Todo: Send $note to external system for order with id $orderId.
}
}
}

Expose available pickup locations

src/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="simpler_checkout_pickup_locations_requested">
<observer name="my_custom_observer" instance="MyVendor\MyModule\Observer\CollectPickupLocations" shared="true" />
</event>
</config>
src/Observer/CollectPickupLocations.php
<?php
namespace MyVendor\MyModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Simpler\Checkout\Api\Data\PickupLocationInterfaceFactory;

class CollectPickupLocations implements ObserverInterface
{
/**
* @var PickupLocationInterfaceFactory
*/
private $factory;

public function __construct(PickupLocationInterfaceFactory $factory)
{
$this->factory = $factory;
}

public function execute(Observer $observer)
{
$locations = $observer->getData('locations');

// use the SimplerCheckoutFactory we injected in the constructor to create location items
$location1 = $this->factory->create()->setId('foo')->setName('Foo');
$location2 = $this->factory->create()->setId('bar')->setName('Bar');

//mutate the locations object
$locations->addPickupLocation($location1);
$locations->addPickupLocation($location2);
}
}

Get the store pickup id

src/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="simpler_checkout_after_shipping_method_added">
<observer name="my_custom_observer" instance="MyVendor\MyModule\Observer\SetPickupLocationId" shared="true" />
</event>
</config>
src/Observer/SetPickupLocationId.php
<?php
namespace MyVendor\MyModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class SetPickupLocationId implements ObserverInterface
{
public function execute(Observer $observer)
{
$storeId = $observer->getData('store_id');
$quote = $observer->getData('quote');

//run only if the shipping method is custom pickup and store id exists
if (is_null($storeId) || $quote->getShippingMethod() == 'mycustompickup_method') {
return;
}

// mutate the quote with the location id - we're using the quote extension attributes as an example here.
$quote->getExtensionAttributes()->setPickupLocationId($storeId);
}
}

Get invoicing data

<?php

namespace MyVendor\MyModule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Simpler\Checkout\Event\Event;
use Simpler\Checkout\Event\Submit\AfterCustomerInvoiceCreated;

class MyObserver implements ObserverInterface
{
public function execute(Observer $observer)
{
/** @var AfterCustomerInvoiceCreated $event */
$event = $observer->getData(Event::KEY);

$invoice = $event->getInvoice();
// $invoice->getCompanyName();
// $invoice->getActivity();
// $invoice->getTaxId();
// $invoice->getTaxAuthority();
// $invoice->getCompanyAddress();
}
}