Sunday 26 April 2015

Programmatically create coupon codes in Magento

Create backup for your database and project directory 2.create a form to post the coupon code information. such as
  1. <div >
  2. <h2>Create New Coupons</h2>
  3. </div>
  4. <div >
  5. <div >
  6. <div >
  7. <dl >
  8. <dt><label for="discount">Discount Value Type</label></dt>
  9. <dd>
  10. <div >
  11. <select id="discount" name="discountType">
  12. <option value="by_percent">Percent</option>
  13. <option value="by_fixed">Fixed</option>
  14. </select></div>
  15. <div >
  16. <label for="value">value</label>
  17. <input id="spinner" name="discount_amount" value="">
  18. </div>
  19. <div ></div>
  20. </dd>
  21. <dt><label for="start_date">Start Date <a href="javascript:void(0)">?</a></label></dt>
  22. <dd>
  23. <div ><input id="datepicker" name="start_date" value=""></div>
  24. <!--input id="start_date" name="start_date" value=""></div-->
  25. <div >
  26. <label for="expire_date">Expiration Date</label>
  27. <input id="datepickerend" name="expire_date" value=" ">
  28. <!--input id="expire_date" name="expire_date" value=""-->
  29. </div>
  30. <div ></div>
  31. </dd>
  32. <dt><label for="coupon_code">Coupon Code <a href="javascript:void(0)">?</a></label></dt>
  33. <dd>
  34. <div >
  35. <input id="coupon_code" name="coupon_code" value=" "></div>
  36. <div ></div>
  37. </dd>
  38. <dt><label for="coupon_detail">Coupon Description <a href="javascript:void(0)">?</a></label></dt>
  39. <dd>
  40. <div ><textarea id="coupon_detail" name="coupon_detail"></textarea></div>
  41. <div ></div>
  42. </dd>
  43. </dl>
  44. </div>
  45. </div>
  46. <p><label for="discount_assign"> The Discount will be assigned on <a href="javascript:void(0)">?</a></label> &nbsp; <select id="discount_assign" name="discount_assign">
  47. <option value="0">Order Dishes Sum</option>
  48. <option value="1">Order Total Sum</option>
  49. </select></p>Coupon Limitations <a href="javascript:void(0)">?</a><p></p>
  50. <div >
  51. <ul >
  52. <li ># Times
  53. <div ><input name="times" value=" " id="spinner1"></div>
  54. </li>
  55. <li> # Times Per User
  56. <div ><input name="perUser" value=" " id="spinner2" "=""></div>
  57. </li>
  58. </ul>
  59. </div>
  60. </div>

3:create Action for post the form data such as
  1. <p><!--?php public function saveCouponAction(){</p-->
  2. </p><pre>$param=$this-&gt;getRequest()-&gt;getPost();
  3. $conditions_serialized = 'a:7:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_address";s:9:"attribute";s:13:"base_subtotal";s:8:"operator";s:2:"==";s:5:"value";s:2:"12";s:18:"is_value_processed";b:0;}}}';
  4. $data = unserialize($conditions_serialized);
  5. $data['conditions'][0]['value'] = $param['cart_value'];
  6. (string)$conditions_serialized = serialize($data);
  7. (string)$actions_serialized = 'a:6:{s:4:\"type\";s:40:\"salesrule/rule_condition_product_combine\";s:9:\"attribute\";N;s:8:\"operator\";N;s:5:\"value\";s:1:\"1\";s:18:\"is_value_processed\";N;s:10:\"aggregator\";s:3:\"all\";}';
  8. $data = array(
  9. 'product_ids' =&gt; null,
  10. 'name' =&gt;$param['coupon_code'],
  11. 'description' =&gt;$param['coupon_detail'],
  12. 'is_active' =&gt; 1,
  13. 'website_ids' =&gt; array(1),
  14. 'customer_group_ids' =&gt; array(1),
  15. 'coupon_type' =&gt; 2,
  16. 'coupon_code' =&gt;$param['coupon_code'],
  17. 'uses_per_coupon' =&gt;$param['times'],
  18. 'uses_per_customer' =&gt;$param['perUser'],
  19. 'from_date' =&gt;$param['start_date'],
  20. 'to_date' =&gt;$param['expire_date'],
  21. 'sort_order' =&gt; null,
  22. 'is_rss' =&gt; 1,
  23. 'conditions_serialized' =&gt; "'$conditions_serialized'",
  24. 'actions_serialized' =&gt; "'$actions_serialized'",
  25. 'simple_action' =&gt;$param['discountType'],
  26. 'discount_amount' =&gt;$param['discount_amount'],
  27. 'discount_qty' =&gt; 0,
  28. 'discount_step' =&gt; null,
  29. 'apply_to_shipping' =&gt; $param['discount_assign'],
  30. 'simple_free_shipping' =&gt; 0,
  31. 'stop_rules_processing' =&gt; 0,
  32. 'store_labels' =&gt; array($param['coupon_detail'])
  33. </pre>
  34. <p>);
  35. /<em>echo "</em></p><pre><em>";
  36. print_r($data);
  37. die();</em>/
  38. try{
  39. $model = Mage::getModel('salesrule/rule');
  40. $data = $this-&gt;<em>filterDates($data, array('from</em>date', 'to<em>date'));
  41. $validateResult = $model-&gt;validateData(new Varien</em>Object($data));
  42. if ($validateResult == true) {
  43. if (isset($data['simple<em>action']) &amp;&amp; $data['simple</em>action'] == 'by<em>percent'
  44. &amp;&amp; isset($data['discount</em>amount'])) {
  45. $data['discount<em>amount'] = min(100, $data['discount</em>amount']);
  46. }
  47. if (isset($data['rule']['conditions'])) {
  48. $data['conditions'] = $data['rule']['conditions'];
  49. }
  50. if (isset($data['rule']['actions'])) {
  51. $data['actions'] = $data['rule']['actions'];
  52. }
  53. unset($data['rule']);
  54. $model-&gt;loadPost($data);
  55. $model-&gt;save();
  56. $this-&gt;_redirect('manager/account/coupons');
  57. }
  58. }
  59. catch (Exception $e){
  60. echo $e-&gt;getMessage();
  61. }
  62. }<p></p>

4: check from admin panel or database table salesrule coupon has been added to your magento store.



For such more Blogs you can visit to http://findnerd.com/NerdDigest


No comments:

Post a Comment