Add customers to a customer group upon creating a subscription
We have created an addon extension that allows you to specify a customer group, to which customers will be added when they start a subscription. After installing the extension, you are able to choose the customer group in the admin backend, under System -> Configuration -> Subscribe Pro -> Subscriptions -> Customer Group.
Here are some key code excerpts from the addon extension to give you an idea how it works. First, in config.xml we see the event that is being observed sfc_autoship_after_create_subscription_from_quote_item
, and then in Observer.php we see the function that is called in response.
<events>
<sfc_autoship_after_create_subscription_from_quote_item>
<observers>
<customergroup_autoship_after_create_subscription>
<class>sfc_customergroup/observer</class>
<method>addCustomerToGroup</method>
</customergroup_autoship_after_create_subscription>
</observers>
</sfc_autoship_after_create_subscription_from_quote_item>
</events>
Observer.php:addCustomerToGroup()
/**
* Add the customer to a specific group, if configured in admin
* @param $obs
* @return $this
*/
public function addCustomerToGroup($obs)
{
/** @var Mage_Sales_Model_Quote_Item $quoteItem */
$quoteItem = $obs->getQuoteItem();
/** @var Mage_Sales_Model_Quote $quote */
$quote = $quoteItem->getQuote();
/** @var Mage_Customer_Model_Customer $customer */
$customer = $quote->getCustomer();
$subscription_group_id = $this->_getSubscriptionGroupId();
if ($subscription_group_id && $customer->getGroupId() != $subscription_group_id) {
$customer->setGroupId($subscription_group_id)->save();
}
return $this;
}
To download this addon extension, click the following link: sfc_customergroup.tgz (2 KB)
Note that this extension depends on the SFC_Autoship
extension.