1

I am using Magento Enterprise 1.14.0.1 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
 );

 $generator->validateData($data);
 $generator->setData($data);
 $generator->generatePool();

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


 $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.

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

1 Answer 1

3

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

app/code/local/[Namespace]/[Module]/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <version>1.0.0</version>
        </[Namespace]_[Module]>
    </modules>
    <global>
        <blocks>
            <[namespace]_[module]>
                <class>[Namespace]_[Module]_Block</class>
            </[namespace]_[module]>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <[namespace]_[module]>
                    <file>ffm_codebin.xml</file>
                </[namespace]_[module]>
            </updates>
        </layout>
    </frontend>
</config>

The block class will contain your code

app/code/local/[Namespace]/[Module]/Block/Coupon.php

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

app/design/fontend/base/default/template/[namespace]_[module]/coupon.phtml

<?php

$vouchercode = $this->getVoucherCode();
?>

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

And the layout XML file

app/design/fontend/base/default/layout/[namespace]_[module].xml

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

Your Answer

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

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