Magento 2 Event: After Create Subscription


Event code: subscribe_pro_after_create_subscription_from_quote_item

Overview

This event is dispatched just after the Subscribe Pro extension creates a new subscription from a quote item. This occurs when a subscription is being created from a successful checkout, just after the order is successfully placed.

Parameters

When fired, the following parameters are passed to observers of this event:

NameTypeDescription
subscriptionSFC_Autoship_Model_SubscriptionThe subscription parameter holds a data structure which is a representation of the new subscription which will be created. The data in this object can be used to learn about the newly created subscription.
quote_itemMage_Sales_Model_Quote_ItemThe quote_item parameter holds a reference to the Magento quote item data structure from the just-placed quote / order. The quote_item holds the product details and the customer's "request" to create a subscription for the given product.

Example

The following example shows how to write custom code in your own M2 module My\Module to intercept and handle the event.

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="subscribe_pro_after_create_subscription_from_quote_item">
        <observer name="after_create_subscription_from_quote_item" instance="My\Module\Observer\SubscribePro\AfterCreateSubscriptionFromQuoteItem" />
    </event>
</config>

My/Module/Observer/SubscribePro/AfterCreateSubscriptionFromQuoteItem.php

<?php

namespace My\Module\Observer\SubscribePro;

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

class AfterCreateSubscriptionFromQuoteItem implements ObserverInterface
{
    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        /** @var \Magento\Framework\DataObject $result */
        $result = $observer->getData('subscription');

        /** @var \Magento\Quote\Api\Data\CartInterface $quote */
        $quoteItem = $observer->getData('quote_item');

        // ...
    }
}