Search code examples
phpmagentomagento2webhooks

Magento 2 register call back Url / webbook for e.g. add to cart without code changes in Magento


Is there way from magento2 to register web hook like function where external API get called upon some operation on magneto site (add product to cart, add shipment...ect)

I need to do this from API, where Magento site is hosted in different end (client) I have very little control over it. Just I wanted to do all in API call to magento site,

I know it possible with woocommerce where we can register my external API to woocommerce site and from there I can register my application as callback. This all can be done with just API calls to woocommerce site no any source code changes from woocommerce site.

Does anyone have some insight on this kindly give me a path to references.


Solution

  • For these cases Magento event / observer design pattern would be suitable, also refer to documentation.

    So for example for add product to cart webhook, you can use checkout_cart_product_add_after event.

    Your module should look like this:

    app/code/Company/Module/registration.php:

    <?php
    declare(strict_types = 1);
    
    use Magento\Framework\Component\ComponentRegistrar;
    
    ComponentRegistrar::register(
        ComponentRegistrar::MODULE,
        'Company_Module',
        __DIR__
    );
    

    app/code/Company/Module/etc/module.xml:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Company_Module"/>
    </config>
    

    app/code/Company/Module/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="checkout_cart_product_add_after">
            <observer name="company_module_checkout_cart_product_add_after" instance="Company\Module\Observer\CheckoutCartProductAddAfter" />
        </event>
    </config>
    

    app/code/Company/Module/Observer/CheckoutCartProductAddAfter.php:

    <?php
    declare(strict_types = 1);
    
    namespace Company\Module\Observer;
    
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    
    class CheckoutCartProductAddAfter implements ObserverInterface
    {
        /**
         * Execute observer
         *
         * @param Observer $observer
         * @return void
         */
        public function execute(
            Observer $observer
        ): void {
            // Your code logic here, like:
            $item = $observer->getEvent()->getQuoteItem();
            $product = $item->getProduct();
            $data = $this->_request->getParams();
        }
    }
    

    After adding these files run php bin/magento setup:upgrade from command line to enable the module.

    If you make this an open source composer package than they only have to install the package and do a little configuration. That would be the way to go.

    It's not possible however to observe these event from outside of Magento. So if you really can't make any changes in Magento, what you can do is polling the Magento api for new quotes/shipments.

    E.g. getting list of shipments with Magento Rest API (sending date from/to filter recommended): https://magento.redoc.ly/2.4.3-admin/tag/shipments#operation/salesShipmentRepositoryV1GetListGet