Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- }
- ?>
- function coupon_gen(){
- jQuery.ajax({
- type: 'POST',
- url: 'coupon.php',
- success: function(data) {
- alert(data);
- },
- error: function(){
- alert("Url not found");
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement