Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. <?php
  2.  
  3. $data = array(
  4. 'product_ids' => null,
  5. 'name' => sprintf('Success Purchase Discount: 10 percent discount',$_customerId),
  6. 'description' => null,
  7. 'is_active' => 1,
  8. 'website_ids' => array(1),
  9. 'customer_group_ids' => array(1),
  10. 'coupon_type' => 2,
  11. 'coupon_code' => $order->getShippingAddress()->getFirstname().'-'.Mage::helper('core')->getRandomString(4),
  12. 'uses_per_coupon' => 1,
  13. 'uses_per_customer' => 1,
  14. 'from_date' => time(),
  15. 'to_date' => null,
  16. 'sort_order' => null,
  17. 'is_rss' => 1,
  18. 'rule' => array(
  19. 'conditions' => array(
  20. array( 'type' => 'salesrule/rule_condition_combine','aggregator' => 'all','value' => 1,'new_child' => null )
  21. )
  22. ),
  23. 'simple_action' => 'by_percent',
  24. 'discount_amount' => 10,
  25. 'discount_qty' => 0,
  26. 'discount_step' => null,
  27. 'apply_to_shipping' => 0,
  28. 'simple_free_shipping' => 0,
  29. 'stop_rules_processing' => 0,
  30. 'rule' => array(
  31. 'actions' => array(
  32. array('type' => 'salesrule/rule_condition_product_combine','aggregator' => 'all','value' => 1,'new_child' => null )
  33. )
  34. ),
  35. 'store_labels' => array('Purchase Discount')
  36. );
  37.  
  38. $model = Mage::getModel('salesrule/rule');
  39. //$data = $this->_filterDates($data, array('from_date', 'to_date'));
  40.  
  41. $validateResult = $model->validateData(new Varien_Object($data));
  42.  
  43. if ($validateResult == true) {
  44.  
  45. if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
  46. && isset($data['discount_amount'])) {
  47. $data['discount_amount'] = min(100, $data['discount_amount']);
  48. }
  49.  
  50. if (isset($data['rule']['conditions'])) {
  51. $data['conditions'] = $data['rule']['conditions'];
  52. }
  53.  
  54. if (isset($data['rule']['actions'])) {
  55. $data['actions'] = $data['rule']['actions'];
  56. }
  57.  
  58. unset($data['rule']);
  59.  
  60. $model->loadPost($data);
  61.  
  62. $model->save();
  63. }
  64.  
  65. //send email
  66.  
  67. $emailTemplate = Mage::getModel('core/email_template')->loadDefault('coupon_email_template');
  68.  
  69. //Variables for Confirmation Mail.
  70. $customerName=$order->getShippingAddress()->getName();
  71. $customerEmail=$order->getShippingAddress()->getEmail();
  72. $emailTemplateVariables = array();
  73. $emailTemplateVariables['name'] = $customerName;
  74. $emailTemplateVariables['coupon'] = $model->acquireCoupon()->getCode();
  75.  
  76. //Appending the Custom Variables to Template.
  77. $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
  78.  
  79. $senderEmail=Mage::getStoreConfig('trans_email/ident_general/email');
  80. $senderName=Mage::getStoreConfig('trans_email/ident_general/name');
  81.  
  82. //Sending E-Mail to Customers.
  83. $mail = Mage::getModel('core/email')
  84. ->setToName($senderName)
  85. ->setToEmail($customerEmail)
  86. ->setBody($processedTemplate)
  87. ->setSubject('Subject :')
  88. ->setFromEmail($senderEmail)
  89. ->setFromName($senderName)
  90. ->setType('html');
  91. try{
  92. //Confimation E-Mail Send
  93. $mail->send();
  94. }
  95. catch(Exception $error)
  96. {
  97. Mage::getSingleton('core/session')->addError($error->getMessage());
  98. return false;
  99. }
  100.  
  101. ?>
  102.  
  103. function coupon_gen(){
  104. jQuery.ajax({
  105. type: 'POST',
  106. url: 'coupon.php',
  107. success: function(data) {
  108. alert(data);
  109.  
  110. },
  111. error: function(){
  112. alert("Url not found");
  113. }
  114. });
  115. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement