- Create backup for your database and project directory
2.create a form to post the coupon code information.
such as
<div class="descriptionBox">
<h2>Create New Coupons</h2>
</div>
<div class="descriptionBox">
<div class="couponBox">
<div class="couponLT">
<dl class="clearfix" style="width:100%">
<dt><label for="discount">Discount Value Type</label></dt>
<dd>
<div class="couponLTN">
<select id="discount" class="selectBox" name="discountType">
<option value="by_percent">Percent</option>
<option value="by_fixed">Fixed</option>
</select></div>
<div class="couponRTN">
<label for="value">value</label>
<input id="spinner" name="discount_amount" type="text" class="input-text required-entry" value="">
</div>
<div class="clear"></div>
</dd>
<dt><label for="start_date">Start Date <a class="toottip" href="javascript:void(0)">?</a></label></dt>
<dd>
<div class="couponLTN"><input type="text" id="datepicker" class="datePicker input-text required-entry" name="start_date" value=""></div>
<!--input id="start_date" type="text" class="datePicker" name="start_date" value=""></div-->
<div class="couponRTN">
<label for="expire_date">Expiration Date</label>
<input type="text" id="datepickerend" class="datePicker input-text required-entry" name="expire_date" value=" ">
<!--input id="expire_date" type="text" name="expire_date" class="datePicker" value=""-->
</div>
<div class="clear"></div>
</dd>
<dt><label for="coupon_code">Coupon Code <a class="toottip" href="javascript:void(0)">?</a></label></dt>
<dd>
<div class="couponLTN">
<input id="coupon_code" class="inbox input-text required-entry " type="text" name="coupon_code" value=" "></div>
<div class="clear"></div>
</dd>
<dt><label for="coupon_detail">Coupon Description <a class="toottip" href="javascript:void(0)">?</a></label></dt>
<dd>
<div class="couponLTN"><textarea id="coupon_detail" class="inbox" name="coupon_detail"></textarea></div>
<div class="clear"></div>
</dd>
</dl>
</div>
</div>
<p><label class="bottomSpace" for="discount_assign"> The Discount will be assigned on <a class="toottip" href="javascript:void(0)">?</a></label> <select id="discount_assign" class="selectBox" name="discount_assign">
<option value="0">Order Dishes Sum</option>
<option value="1">Order Total Sum</option>
</select></p>Coupon Limitations <a class="toottip" href="javascript:void(0)">?</a><p></p>
<div class="closeBox">
<ul class="clearfix timeTableOption">
<li class="alwyasWidth"># Times
<div class="spinnerBox"><input type="text" name="times" value=" " id="spinner1"></div>
</li>
<li> # Times Per User
<div class="spinnerBox"><input type="text" 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
0 Comment(s)