Create
backup for your database and project directory 2.create a form to
post the coupon code information. such as
- <div >
- <h2>Create New Coupons</h2>
- </div>
- <div >
- <div >
- <div >
- <dl >
- <dt><label for="discount">Discount Value Type</label></dt>
- <dd>
- <div >
- <select id="discount" name="discountType">
- <option value="by_percent">Percent</option>
- <option value="by_fixed">Fixed</option>
- </select></div>
- <div >
- <label for="value">value</label>
- <input id="spinner" name="discount_amount" value="">
- </div>
- <div ></div>
- </dd>
- <dt><label for="start_date">Start Date <a href="javascript:void(0)">?</a></label></dt>
- <dd>
- <div ><input id="datepicker" name="start_date" value=""></div>
- <!--input id="start_date" name="start_date" value=""></div-->
- <div >
- <label for="expire_date">Expiration Date</label>
- <input id="datepickerend" name="expire_date" value=" ">
- <!--input id="expire_date" name="expire_date" value=""-->
- </div>
- <div ></div>
- </dd>
- <dt><label for="coupon_code">Coupon Code <a href="javascript:void(0)">?</a></label></dt>
- <dd>
- <div >
- <input id="coupon_code" name="coupon_code" value=" "></div>
- <div ></div>
- </dd>
- <dt><label for="coupon_detail">Coupon Description <a href="javascript:void(0)">?</a></label></dt>
- <dd>
- <div ><textarea id="coupon_detail" name="coupon_detail"></textarea></div>
- <div ></div>
- </dd>
- </dl>
- </div>
- </div>
- <p><label for="discount_assign"> The Discount will be assigned on <a href="javascript:void(0)">?</a></label> <select id="discount_assign" name="discount_assign">
- <option value="0">Order Dishes Sum</option>
- <option value="1">Order Total Sum</option>
- </select></p>Coupon Limitations <a href="javascript:void(0)">?</a><p></p>
- <div >
- <ul >
- <li ># Times
- <div ><input name="times" value=" " id="spinner1"></div>
- </li>
- <li> # Times Per User
- <div ><input name="perUser" value=" " id="spinner2" "=""></div>
- </li>
- </ul>
- </div>
- </div>
3:create Action for post the form data such as
- <p><!--?php public function saveCouponAction(){</p-->
- </p><pre>$param=$this->getRequest()->getPost();
- $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;}}}';
- $data = unserialize($conditions_serialized);
- $data['conditions'][0]['value'] = $param['cart_value'];
- (string)$conditions_serialized = serialize($data);
- (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\";}';
- $data = array(
- 'product_ids' => null,
- 'name' =>$param['coupon_code'],
- 'description' =>$param['coupon_detail'],
- 'is_active' => 1,
- 'website_ids' => array(1),
- 'customer_group_ids' => array(1),
- 'coupon_type' => 2,
- 'coupon_code' =>$param['coupon_code'],
- 'uses_per_coupon' =>$param['times'],
- 'uses_per_customer' =>$param['perUser'],
- 'from_date' =>$param['start_date'],
- 'to_date' =>$param['expire_date'],
- 'sort_order' => null,
- 'is_rss' => 1,
- 'conditions_serialized' => "'$conditions_serialized'",
- 'actions_serialized' => "'$actions_serialized'",
- 'simple_action' =>$param['discountType'],
- 'discount_amount' =>$param['discount_amount'],
- 'discount_qty' => 0,
- 'discount_step' => null,
- 'apply_to_shipping' => $param['discount_assign'],
- 'simple_free_shipping' => 0,
- 'stop_rules_processing' => 0,
- 'store_labels' => array($param['coupon_detail'])
- </pre>
- <p>);
- /<em>echo "</em></p><pre><em>";
- print_r($data);
- die();</em>/
- try{
- $model = Mage::getModel('salesrule/rule');
- $data = $this-><em>filterDates($data, array('from</em>date', 'to<em>date'));
- $validateResult = $model->validateData(new Varien</em>Object($data));
- if ($validateResult == true) {
- if (isset($data['simple<em>action']) && $data['simple</em>action'] == 'by<em>percent'
- && isset($data['discount</em>amount'])) {
- $data['discount<em>amount'] = min(100, $data['discount</em>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();
- $this->_redirect('manager/account/coupons');
- }
- }
- catch (Exception $e){
- echo $e->getMessage();
- }
- }<p></p>
4: check from admin panel or database table salesrule coupon has been added to your magento store.
For
more details visit the link
http://findnerd.com/list/view/Programmatically-create-coupon-codes-in-Magento-/170/
No comments:
Post a Comment