29 March, 2024
Coupon code field in Magento

Add Discount Coupon in Magento One Page Checkout (with and without coding)

Today, in this tutorial, I’ll assist you in the process of adding a discount coupon facility in checkout page of your ecommerce store to improve customer experience using coding and no coding. Now in short period of time you’d be able to add coupon code field to default Magento store and can start attracting customers amazingly.

There is no denying the fact that Magento is a robust platform. But, when it comes to customization, it can be really hard. However, customizations are inevitable for enhancing the performance of business. To sum up, you must customize your cart with or without developers to help your customers enjoy a perfect shopping on your store.

Also Read: Simplified Customer Checkout Guaranteed!

A common issue associated with the installation of Magento one page checkout on your store is that it won’t provide any option for customers to feed their coupon codes directly here. However, you can make it work for your customers through customization.

Down below here I’ve included a step-by-step guide using which it becomes easier to add a discount coupon field on the store of default Magento.

I have implemented discount coupon in “review” step of one page checkout and it has been displayed below.

Coupon Code in Default Magento

 

3-Step approach to add coupon code field with coding on default Magneto store

Step 1: Adding coupon action in one page controller

Open the onepage controller file. app/code/core/Mage/Checkout/controllers/OnepageController.php

Then go to Line # near to 593

function couponAction() {

$this->loadLayout(‘checkout_onepage_review’);

$this->couponCode = (string) $this->getRequest()->getParam(‘coupon_code’);

Mage::getSingleton(‘checkout/cart’)->getQuote()->getShippingAddress()->setCollectShippingRates(true);

Mage::getSingleton(‘checkout/cart’)->getQuote()->setCouponCode(strlen($this->couponCode) ?

$this->couponCode : ‘ ‘)->collectTotals()->save();

$result[‘goto_section’] = ‘review’;

$result[‘update_section’] = array( ‘name’ => ‘review’, ‘html’ => $this->_getReviewHtml() );

$this->getResponse()->setBody(Mage::helper(‘core’)->jsonEncode($result));

}

Step 2: Adding Js script to phtml file

Open file: app/design/frontend/base/default/template/checkout/onepage.phtml

Then go to Line # near to 63 and confirm that this is last line then copy and paste this code

<script type=”text/javascript”>

//<![CDATA[

function updateCoupon(reqid) {

var reqid;

if(reqid == 2) {  $(‘coupon_code’).setValue(”);  }

$(‘discount-coupon-form’).request({

method: ‘post’,

onComplete: payment.onComplete,

onSuccess: payment.onSave,

onFailure: checkout.ajaxFailure.bind(checkout),

})

}

//]]>

</script>

Step 3: Adding form to phtml file

Open file: app/design/frontend/base/default/template/checkout/onepage/agreements.phtml

Then go to Line # near to 33 and confirm that this is first line then copy and paste this code

<?php $getCouponCode = Mage::getSingleton(‘checkout/cart’)->getQuote()->getCouponCode(); ?>

<form id=”discount-coupon-form” action=”http://www.localhost.com/mage17/index.php/checkout/onepage/coupon/” method=”post”>

<label for=”coupon_code”> <?php echo $this->__(‘Enter your coupon code if you have one.’) ?></label><br />

<input id=”coupon_code” name=”coupon_code” value=”<?php echo $getCouponCode; ?>”/>

<?php  if(strlen($getCouponCode)) { ?>

<button type=”button” onclick=”updateCoupon(2);” value=”<?php echo $this->__(‘Remove Coupon’)?>”><span><?php echo $this->__(‘Remove Coupon’) ?></span></button>

<?php } ?>

<button type=”button” onclick=”updateCoupon(1);” value=”<?php echo $this->__(‘Apply Coupon’)?>”><span><?php echo $this->__(‘Apply Coupon’) ?></span></button>

</form>

Adding coupon code option to one page checkout without coding

I know most of you would find it difficult to add coupon codes to default Magento store. Why to waste time when you’ve a best alternative to quickly add coupon code field on checkout page!

Now, it’s time to learn the easiest method to add coupon code option to your one page checkout page, of course, by deploying an extension. It just takes a few minutes to install Apptha’s one step checkout extension. As the extension carries the option of coupon code field already, it’ll appear in the checkout page in no time!!!

Looking forward to learn about more features of this product, check here: 

http://www.apptha.com/category/extension/Magento/OneStepCheckout

Wish to try your hands on this product functionality, check here:  http://www.apptha.com/demo/onestepcheckout/

I hope the tutorial helped everyone reading this and it was beneficial. If you find it useful, try this and let us know your thoughts via comments.

Alex Sam is a digital marketer by choice & profession. He munches on topics relating to technology, eCommerce, enterprise mobility, Cloud Solutions and Internet of Things. His other interests lies in SEO, Online Reputation Management and Google Analytics. Follow Me Socially: Habr , Dev.to & Viblo .

6 Comments

  1. Demian Reply

    Looking good, but I get an Syntax Phrase error on this line:
    $this->couponCode : ‘ ‘)->collectTotals()->save();

    missing something in the code?

  2. cynthia johnson Reply

    You have shared a worthy information. When I was looking to add coupons to my store this information came handy.

    1. ramanathan Reply

      Hi Cynthia,

      Hope this information helped you for your ecommerce store. Also check out our ecommerce store related tips to get more insights.

      Thanks.

  3. Tim Franklin Reply

    Hi,

    I just implemented this script in my ecommerce store with one step check out. Promptly added the discount coupon codes in the default magento store with the code given. Now it works fine just replaced the quotations with ‘.

    1. ramanathan Reply

      Hi Tim,

      We are glad to know that you are using our product. Thanks for your review, if you need any extensions for your eCommerce store then keep visiting our store. We are updating with new sales boosting extensions for ecommerce stores with 20% off. Check and avail your offer now.

Leave a Reply

Your email address will not be published. Required fields are marked *