Magento 1 Event: After Create Subscription from Quote Item


Event Code: subscribepro_autoship_after_create_subscription_from_quote_item

Overview

The subscribepro_autoship_after_create_subscription_from_quote_item 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

NameTypeDescription
subscriptionsubscribepro_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

config.xml

<config>
    <global>
        <events>
            <subscribepro_autoship_after_create_subscription_from_quote_item>
                <observers>
                    <my_module_subscribepro_autoship_after_create_subscription_from_quote_item>
                        <class>my_module/observer</class>
                        <method>onAfterCreateSubscriptionFromQuoteItem</method>
                    </my_module_subscribepro_autoship_after_create_subscription_from_quote_item>
                </observers>
            </subscribepro_autoship_after_create_subscription_from_quote_item>
        </events>
    </global>
</config>

Observer.php

public function onAfterCreateSubscriptionFromQuoteItem(Varien_Event_Observer $observer)
{
    // Get data from $observer
    /** @var SubscribePro_Autoship_Model_Subscription $subscription */
    $subscription = $observer->getData('subscription');
    /** @var Mage_Sales_Model_Quote_Item $quoteItem */
    $quoteItem = $observer->getData('quote_item');

    if ($subscription->getData('coupon_code') == 'COUPON_X') {
        // Lookup order item
        /** @var Mage_Sales_Model_Order_Item $orderItem */
        $orderItem = Mage::getModel('sales/order_item')->load($quoteItem->getId(), 'quote_item_id');
        if(strlen($orderItem->getId())) {
            // Get options
            $options = $orderItem->getProductOptions();
            // Get existing additional_options
            if(isset($options['additional_options']) && is_array($options['additional_options'])) {
                $additionalOptions = $options['additional_options'];
            }
            else {
                $additionalOptions = array();
            }
            // Add our details
            $additionalOptions[] = array(
                'label' => 'Subscription Created With Coupon Code',
                'value' => 'COUPON_X',
            );
            // Set new additional_options on order item
            $options['additional_options'] = $additionalOptions;
            $orderItem->setProductOptions($options);
        }
    }
}