I'm using Magento 1.9.2. I want to send Custom Coupon code to customer when they click on Claim button on Success page. I have the php code for generation of Coupon Code based on user name (ex- Siddharth-x34w) and sending it via mail in septate php file coupon.php located in the same directory as success.phtml file. The coupon.php file shall only be executed when the customer clicks on claim now button.I tried using Ajax call for the file but it didn't worked.

coupon.php code:


$data = array(
    'product_ids' => null,
    'name' => sprintf('Success Purchase Discount: 10 percent discount',$_customerId),
    'description' => null,
    'is_active' => 1,
    'website_ids' => array(1),
    'customer_group_ids' => array(1),
    'coupon_type' => 2,
    'coupon_code' => $order->getShippingAddress()->getFirstname().'-'.Mage::helper('core')->getRandomString(4),
    'uses_per_coupon' => 1,
    'uses_per_customer' => 1,
    'from_date' => time(),
    'to_date' => null,
    'sort_order' => null,
    'is_rss' => 1,
    'rule' => array(
        'conditions' => array(
                                array(  'type' => 'salesrule/rule_condition_combine','aggregator' => 'all','value' => 1,'new_child' => null  )
    'simple_action' => 'by_percent',
    'discount_amount' => 10,
    'discount_qty' => 0,
    'discount_step' => null,
    'apply_to_shipping' => 0,
    'simple_free_shipping' => 0,
    'stop_rules_processing' => 0,
    'rule' => array(
        'actions' => array(
            array('type' => 'salesrule/rule_condition_product_combine','aggregator' => 'all','value' => 1,'new_child' => null  )
    'store_labels' => array('Purchase Discount')

$model = Mage::getModel('salesrule/rule');
//$data = $this->_filterDates($data, array('from_date', 'to_date'));

$validateResult = $model->validateData(new Varien_Object($data));

if ($validateResult == true) {

    if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
            && isset($data['discount_amount'])) {
        $data['discount_amount'] = min(100, $data['discount_amount']);

    if (isset($data['rule']['conditions'])) {
        $data['conditions'] = $data['rule']['conditions'];

    if (isset($data['rule']['actions'])) {
        $data['actions'] = $data['rule']['actions'];




 //send email

$emailTemplate = Mage::getModel('core/email_template')->loadDefault('coupon_email_template');

//Variables for Confirmation Mail.
$emailTemplateVariables = array();
$emailTemplateVariables['name'] = $customerName;
$emailTemplateVariables['coupon'] = $model->acquireCoupon()->getCode();

//Appending the Custom Variables to Template.
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);


//Sending E-Mail to Customers.
$mail = Mage::getModel('core/email')
 ->setSubject('Subject :')
 //Confimation E-Mail Send
 catch(Exception $error)
 return false;


and the javascript to call the php file:

function coupon_gen(){
                type: 'POST',
                url: 'coupon.php',
                success: function(data) {

                error: function(){
                alert("Url not found");

But on click of button the php file is not executed.Also I can't simply use the php code in function and refresh the page, as it's the success page. On refreshing the user session will be cleared.I'm a noob in Magento and don't know how to use ajax in Magento. Kindly suggest any solution

  • Your code in PHP file will go in a controller file, that's where your ajax call should point.
    – Prateek
    Commented Jan 4, 2016 at 13:36
  • can you please explain it in detail.Should I place the PHP file in checkout controller directory? And how would i use ajax to point to it Commented Jan 5, 2016 at 18:49

1 Answer 1


Do not give direct call to file. Html and Ajax call do not executes any type of file externally like this.

Best way is to keep the code in any controller's custom function, and just give call to that function with proper magento url like Mage::getBaseUrl() and your controller name, and function name.

Then definately it will execute the function.

Try it and let me know

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.