I am using Magento Enterprise On the success.phtml I have a php code that automatically generates a coupon code. The code does generate the coupon I see in the Magento admin.However I can not figure out how to display it on the page?

Here is my code that generates the coupon

//Below is code to generate a coupon code and display it in a jquery popup window
$todaysdateis = date('Y-m-d', strtotime('+1 year'));
$generator = Mage::getModel('salesrule/coupon_massgenerator');

$data = array
    'max_probability'   => .25,
    'max_attempts'      => 10,
    'uses_per_customer' => 1,
    'uses_per_coupon'   => 1,
    'qty'               => 1, //number of coupons to generate
    'length'            => 14, //length of coupon string
    'to_date'           => "$todaysdateis", //ending date of generated promo
     * Possible values include:
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHABETICAL
     * Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_NUMERIC
    'format'          => Mage_SalesRule_Helper_Coupon::COUPON_FORMAT_ALPHANUMERIC,
    'rule_id'         => 68 //the id of the shopping cart rule you will use as a template


 //get the generate coupon code
 $salesRule = Mage::getModel('salesrule/rule')->load($data['rule_id']);
 $collection = Mage::getResourceModel('salesrule/coupon_collection')

 $thecode = $generator->getCode();

 echo "Your Code Is $thecode";

//End code to generate a coupon code and display it in a jquery popup window ?>

The goal is I want to eventually display this to the page not send it in an email.

  • Have you thought about using follow up emails instead?
    – ladle3000
    Sep 16, 2015 at 17:25

1 Answer 1


Start by building your own module. This tutorial will explain how.

Now we'll need 3 things. A block class, template file and a layout XML.

The config


<?xml version="1.0"?>

The block class will contain your code


Class [Namespace]_[Module]_Block_Coupon extends Mage_Core_Block_Template
   public function getVoucherCode() 
      // your code goes here
      // but instead of echo, return it

      return $thecode;

Now for the template file displaying the code, this can be quite simpel



$vouchercode = $this->getVoucherCode();

Your voucher code is <?php echo $vouchercode; ?>

And the layout XML file


<?xml version="1.0"?>
<layout version="0.1.0">
      <reference name="content">
         <block type="[namespace]_[module]/coupon" name="couponcode" template="[namespace]_[module]/coupon.phtml"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.