Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Programmatically create coupon codes in Magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.38k
    Comment on it
    1. 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> &nbsp; <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-&gt;getRequest()-&gt;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' =&gt; null,
    'name' =&gt;$param['coupon_code'],
    'description' =&gt;$param['coupon_detail'],
    'is_active' =&gt; 1,
    'website_ids' =&gt; array(1),
    'customer_group_ids' =&gt; array(1),
    'coupon_type' =&gt; 2,
    'coupon_code' =&gt;$param['coupon_code'],
    'uses_per_coupon' =&gt;$param['times'],
    'uses_per_customer' =&gt;$param['perUser'],
    'from_date' =&gt;$param['start_date'],
    'to_date' =&gt;$param['expire_date'],
    'sort_order' =&gt; null,
    'is_rss' =&gt; 1,
    'conditions_serialized' =&gt; "'$conditions_serialized'",
    'actions_serialized' =&gt; "'$actions_serialized'",
    'simple_action' =&gt;$param['discountType'],
    'discount_amount' =&gt;$param['discount_amount'],
    'discount_qty' =&gt; 0,
    'discount_step' =&gt; null,
    'apply_to_shipping' =&gt; $param['discount_assign'],
    'simple_free_shipping' =&gt; 0,
    'stop_rules_processing' =&gt; 0,
    'store_labels' =&gt; 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-&gt;<em>filterDates($data, array('from</em>date', 'to<em>date'));
            $validateResult = $model-&gt;validateData(new Varien</em>Object($data));
            if ($validateResult == true) {
                if (isset($data['simple<em>action']) &amp;&amp; $data['simple</em>action'] == 'by<em>percent'
                        &amp;&amp; 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-&gt;loadPost($data);
                $model-&gt;save();
                $this-&gt;_redirect('manager/account/coupons');
                }
            }
            catch (Exception $e){
            echo $e-&gt;getMessage();
          }
        }<p></p>
    

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

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: