Initial setup of the Gift Promotions.
The first step will be to configure a promotion in your admin.
Simply browse to the Admin area, and enter the Gift Promotions admin area (Promotions->Gift Promotions) and add a new promotion
Main Tab:
- Name: Free gift on newsletter signup
- Status: Active
- Customer Groups: Select all the groups you want this promotion to work on.
- Coupon: Specific Coupon and tick the 'Use Auto Generation' checkbox
- Uses per coupon: 1 (or more if you like)
- Uses per Customer: Leave blank if only once, or set per customer
Conditions Tab:
Here you can set any conditions you like.
The simplest condition would be to set a total cart value greater than the cart value required to allow the gifting
Gift Products Tab:
Configure here the product(s) you'd like to add to teh cart when the coupon is used.
Manage Coupon codes:
We will generate these coupons via code that gets created.
You can configure start and end dates, and the coupon generator will adhere to those dates for coupons generated
Save the Gift Prootion and take note of the Gift Promotion ID
The Transactional Email:
You need to create a transactional email to send to the subscriber.
To do this simply enter the transactional email manager, and select to add a new template.
In the manager select the 'Newsletter Subscription Success' email template and click 'Load Template'
Set a new Template Name (example 'Subscription with Coupon'
Set a subject of your choice
Set the content to your choice. To display the coupon you will use a line as such:
Here is your unique coupon code:
Save the transactional email, and take not of the ID for this template.
The code:
The code for this module is available via github: https://github.com/ProxiBlue/GiftFromSubscription
The scope of this blog entry is not to teach how to create a magento extension, thus will not go into all parts of what to do to create a valid extension.
Below is the main bits that are required to make this work.
- Observer Event when customer subscribes / unsubscribes
https://github.com/ProxiBlue/GiftFromSubscription/blob/master/app/code/local/ProxiBlue/GiftFromSubscription/Model/Observer.php
You need a way to kow that a subscription took place, and the best way for this is to observe the event 'newsletter_subscriber_save_before'
You can see the code for this event in the Observer.php located in the Models folder.
You will see the code caters for subscribe and unsubscribe, but the later code is not completed as it is not required to illustrate the functionality.
public function newsletter_subscriber_save_before($observer) {
$subscriber = $observer->getEvent()->getSubscriber();
if ($subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED) {
$coupon = mage::helper('proxiblue_giftfromsubscription')->generateCouponCode();
if ($coupon) {
//send email
Mage::helper('proxiblue_giftfromsubscription/couponmailer')->sendCoupon($subscriber, $coupon->getCode());
} else {
Mage::throwException("couldnt generate coupon");
}
} elseif ($subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED) {
//TODO - code to cancel coupon if person unsubscribe
}
}
- A method to generate the coupon code
https://github.com/ProxiBlue/GiftFromSubscription/blob/master/app/code/local/ProxiBlue/GiftFromSubscription/Helper/Data.php
You will find this code in the helper class Helpers/Data.php
You will find at the top of this helpder class reference to two admin config settings:
const XML_PATH_RULE_ID = "giftfromsubscription/options/giftpromotionruleid";
const XML_PATH_COUPON_EMAIL = "giftfromsubscription/options/coupon_email";
These will be the IDS for the Gift Promotions, and Transactional emails respectfully.
You can configure these via the admin Under the ProdiBlue tab.public function generateCouponCode() { /** @var $rule Mage_SalesRule_Model_Rule */ $rule = Mage::getModel('giftpromo/promo_rule')->load($this->getRuleId()); $data = array( 'rule_id' => $rule->getId(), 'qty' => 1, 'length' => 5, 'format' => 'alphanum', 'prefix' => 'NEWSLETTER-', 'suffix' => '', 'dash' => 0, 'uses_per_coupon' => $rule->getUsesPerCoupon(), 'uses_per_customer' => $rule->getUsesPerCustomer(), 'to_date' => $rule->getToDate(), ); if (!$rule->getId()) { throw new Exception('Shopping Cart Rule ID could not be found - did you configure the rule ID in configuration?'); } else { try { $generator = $rule->getCouponMassGenerator(); if (!$generator->validateData($data)) { throw new Exception('Not valid data provided to use coupon mass generator!'); } else { $generator->setData($data); $generator->generatePool(); $collection = Mage::getModel('giftpromo/promo_coupon') ->getCollection() ->addFieldToSelect('*'); $collection->getSelect() ->order('coupon_id DESC') ->limit(1); return $collection->getFirstItem(); } } catch (Exception $e) { throw $e; } } return false; }
- Next a method to send the email.
https://github.com/ProxiBlue/GiftFromSubscription/blob/master/app/code/local/ProxiBlue/GiftFromSubscription/Helper/Couponmailer.php
You will find this in the helper class Couponmailer.php located in the Helper folder.
public function sendCoupon($subscriber, $couponcode) { // Set sender information $senderName = Mage::getStoreConfig('trans_email/ident_general/name'); $senderEmail = Mage::getStoreConfig('trans_email/ident_general/email'); $sender = array( 'name' => $senderName, 'email' => $senderEmail ); $storeId = Mage::app()->getStore()->getId(); $transactional = Mage::getModel('core/email_template'); $customer = Mage::getModel("customer/customer"); $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); $name = $customer->loadByEmail($subscriber->getSubscriberEmail())->getName(); if (!$customer->getId() || $name = '') { $name = 'Customer'; } $vars = array( 'coupon' => $couponcode, 'name' => $name, ); $transactional->sendTransactional( Mage::helper('proxiblue_giftfromsubscription')->getCouponEmailId(), $sender, $subscriber->getSubscriberEmail(), $name, $vars, $storeId ); }
If you install the example module,. create the Gift Promotiuon, create the Transactional Email, and configure their IDs in admin, you will find that after you do a subscription, ou will get a coupon emailed to you, and if you look in the Gift Promotion coupon tab, you will see the coupon appearing there. If the customer uses the coupon, it will be marked as used.