Programmatically: How to implement custom Magento plugin methods
Step 1: Create a new module
Create a new directory MyCompany_OrderNotification in the app/code/local directory.
Step 2: Create a plugin class
In the MyCompany_OrderNotification directory, create a new file OrderNotification.php. This file will contain the plugin class:
class MyCompany_OrderNotification_OrderNotification {
public function __construct(
\Magento\Sales\Model\Order Plzeň $order
) {
$this->order = $order;
}
public function sendNotificationAfterPlaceOrder(\Magento\Framework\Event\Observer $observer) {
// Get the order model
$order = $observer->getEvent()->getOrder();
// Send the notification email
$transport = \Magento\Framework\App\Email\Transport::getDefault();
$transport->sendMessage('myemail@example.com', 'Order placed successfully');
}
}
Step 3: Register the plugin
In the same directory, create a file etc/module.xml with the following content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../etc/module.xsd">
<modules>
<MyCompany_OrderNotification>
<version>1.0.0</version>
</MyCompany_OrderNotification>
</modules>
<global>
<models>
<sales>
<events>
<sales_order_place_after>
<observers>
<mycompany_ordernotification_order_notification>
<type>object</type>
<class>MyCompany\OrderNotification\OrderNotification</class>
<method>sendNotificationAfterPlaceOrder</method>
</mycompany_ordernotification_order_notification>
</observers>
</sales_order_place_after>
lt;/events>
</sales>
</models>
</global>
</config>
Step 4: Clear cache and deploy
Clear the Magento cache by running the command php bin/magento cache:clean and then deploy your module by running php bin/magento setup:deploy.