Magento create personal coupon to newsletter

Magento create personal coupon to newsletter

September 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento create personal coupon to newsletter

Auto Generating Personalized Discount Coupons for Newsletter Recipients

With Magento version 1.7, you can create personalized 10% discount coupons for each newsletter recipient that are unique to their account. This guide will walk you through the process of setting up this feature.

Understanding the Requirements

Before we begin coding, let’s understand what we need to achieve:

  • The system should automatically generate a new coupon when a user subscribes or unsubscribes from the newsletter.
  • Each coupon must be unique and associated with a specific customer account.
  • The coupon should have a usage limit of one.

Setting Up the Coupon Model

First, we’ll modify the Mage_SalesRule_Model_Coupon model to include an additional field for the customer ID. Open this file:

app/code/core/Mage/SalesRule/Model/Coupon.php

Add the following property to the Coupon class:

public $customer_id;

Then, in the constructor method (__construct), initialize this new field:

$this->customer_id = null; // Default value is null

Next, update the save() method to save the customer ID as well:

if ($this->getCustomerId()) {
    $data['customer_id'] = $this->getCustomerId();
}

Finally, add a new getter and setter for this field in the Mage_Core_Model_Abstract class:

public function getCustomerId()
{
    return $this->getData('customer_id');
}

public function setCustomerId($value)
{
    return $this->setData('customer_id', $value);
}

Save your changes and clear the cache.

Creating the Coupon Generation Observer

Now, let’s create an observer that will trigger when a customer subscribes or unsubscribes from the newsletter. Open this file:

app/code/core/Mage/Newsletter/etc/config.xml

Find the <models> node and add the following inside it:

<newsletter>
    <class>Mage_Newsletter_Model</class>
</newsletter>

<newsletter_subscriber>
    <class>Mage_Newsletter_Model_Subscriber</class>
    <resourceModel>newsletter_resource_subscriber</resourceModel>
</newsletter_subscriber>

Next, update the <events> node:

<events>
    <!-- ... existing events ... -->
    <newsletter_subscriber_save_before>
        <observers>
            <generate_coupon_observer>
                <type>singleton</type>
                <class>Mage_Newsletter_Model_Observer</class>
                <method>generateCoupon</method>
            </generate_coupon_observer>
        </observers>
    </newsletter_subscriber_save_before>
    <!-- ... other events ... -->
</events>

Implementing the Coupon Generation Logic

Finally, let’s implement the generateCoupon() method in Mage_Newsletter_Model_Observer:

public function generateCoupon($observer)
{
    $event = $observer->getEvent();
    $subscriber = $event->getSubscriber();

    if ($subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED) {
        // If unsubscribing, delete any existing coupon for this customer
        $this->_deleteCouponForCustomer($subscriber->getId());
    } else {
        // Generate a new unique coupon for the subscriber
        $coupon = Mage::getModel('salesrule/coupon')->setData([
            'code' => $this->_generateUniqueCode(),
            'customer_id' => $subscriber->getId(), // Associate the coupon with this customer
            'percentage_value' => 10, // 10% discount
            'uses_per_customer' => 1, // Limit to one use per customer
        ])->save();
    }
}

private function _generateUniqueCode()
{
    $length = 8;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $code = '';

    for ($i = 0; $i < $length; $i++) {
        $index = rand(0, strlen($characters) - 1);
        $code .= $characters[$index];
    }

    return $code;
}

private function _deleteCouponForCustomer($customerId)
{
    $couponCollection = Mage::getModel('salesrule/coupon')->getCollection()
        ->addFieldToFilter('customer_id', $customerId);

    foreach ($couponCollection as $coupon) {
        $coupon->delete();
    }
}

This observer will generate a new 10% discount coupon for each customer who subscribes or unsubscribes from the newsletter, ensuring that the coupon is unique and can only be used once by the specific customer.

Conclusion

In this guide, we’ve created a personalized coupon generation system for Magento version 1.7 using observers and customizations to the Mage_SalesRule_Model_Coupon model. This allows you to automatically issue unique 10% discount coupons to each newsletter subscriber or un-subscriber.

Remember that this is a core file modification, so it’s essential to backup your files before making any changes. Also, consider using a custom extension if you’re planning on upgrading Magento version in the future, as direct code modifications can cause issues during updates.

For more advanced coupon management features and best practices for Magento development, check out our comprehensive guides at Laravel Company.

Magento 1 Configuration Customers Pricing

Our network