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:
<?php
$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'];
}
unset($data['rule']);
$model->loadPost($data);
$model->save();
}
//send email
$emailTemplate = Mage::getModel('core/email_template')->loadDefault('coupon_email_template');
//Variables for Confirmation Mail.
$customerName=$order->getShippingAddress()->getName();
$customerEmail=$order->getShippingAddress()->getEmail();
$emailTemplateVariables = array();
$emailTemplateVariables['name'] = $customerName;
$emailTemplateVariables['coupon'] = $model->acquireCoupon()->getCode();
//Appending the Custom Variables to Template.
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
$senderEmail=Mage::getStoreConfig('trans_email/ident_general/email');
$senderName=Mage::getStoreConfig('trans_email/ident_general/name');
//Sending E-Mail to Customers.
$mail = Mage::getModel('core/email')
->setToName($senderName)
->setToEmail($customerEmail)
->setBody($processedTemplate)
->setSubject('Subject :')
->setFromEmail($senderEmail)
->setFromName($senderName)
->setType('html');
try{
//Confimation E-Mail Send
$mail->send();
}
catch(Exception $error)
{
Mage::getSingleton('core/session')->addError($error->getMessage());
return false;
}
?>
and the javascript to call the php file:
function coupon_gen(){
jQuery.ajax({
type: 'POST',
url: 'coupon.php',
success: function(data) {
alert(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