Question

I have another page in my OpenCart environment, let say the about us page, which has these forms below, assuming the user has items in their cart, these forms should work but they do not:

Enter your coupon code here:

<form action="index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="basket">
    <input type="text" value="" id="coupon" name="coupon"/>
    <input type="hidden" value="coupon" name="next"/>
    <input type="submit" class="button" value="Apply Coupon"/>
</form>

Enter your gift voucher code here:

<form action="index.php?route=checkout/cart" method="post" enctype="multipart/form-data" id="basket">
    <input type="text" value="" name="voucher"/>
    <input type="hidden" value="voucher" name="next"/>
    <input type="submit" class="button" value="Apply Voucher"/>
</form>

This is for the voucher code system but it does not work (this code is default not edited):

/catalog/controller/checkout/cart.php

// VOUCHER
// IF THE USER HAS ENTERED A VOUCHER
if (isset($this->request->post['voucher']) && $this->request->post['voucher']) {
    foreach ($this->request->post['voucher'] as $key) {
        if (isset($this->session->data['vouchers'][$key])) {
            unset($this->session->data['vouchers'][$key]);
        }
    }
}
Was it helpful?

Solution

Coupons/Vouchers/Shipping

These three system blocks are modules in OpenCart. They are looped together, you can edit the files, example make some blank or use an if/else statement to show only certain modules.

You cannot call the form itself in the cart.tpl, it must be:

<div class="right"> 
    <!-- eVoucher System -->
    <?php foreach ($modules as $module) { ?>
        <?=$module?>
    <?php } ?>
    <!-- eVoucher System --> 
</div>

File locations of Shipping/Voucher and Coupon modules

This will loop and show the module tpl files, shipping, coupon and voucher. They are strangely located

/catalog/view/theme/default/total/coupon.tpl
/catalog/view/theme/default/total/shipping.tpl
/catalog/view/theme/default/total/voucher.tpl

We do not use them all so we have blanked the voucher and shipping. Coupon form looks like:

<div>
  <div class="cart-heading"><?php echo $heading_title; ?></div>
  <div class="cart-content" id="coupon"><?php echo $entry_coupon; ?>&nbsp;
    <input type="text" name="coupon" value="<?php echo $coupon; ?>" />
    &nbsp;<a id="button-coupon" class="button"><span><?php echo $button_coupon; ?></span></a></div>
</div>
<script type="text/javascript">
<!--
//
//  jQuery dependent based on .post so make sure
//  your footer or header jQuery call is before this
//
$('#button-coupon').bind('click', function() {
    $.ajax({
        type: 'POST',
        url: 'index.php?route=total/coupon/calculate',
        data: $('#coupon :input'),
        dataType: 'json',       
        beforeSend: function() {
            $('.success, .warning').remove();
            $('#button-coupon').attr('disabled', true);
            $('#button-coupon').after('<span class="wait">&nbsp;<img src="catalog/view/theme/default/image/loading.gif" alt="" /></span>');
        },
        complete: function() {
            $('#button-coupon').attr('disabled', false);
            $('.wait').remove();
        },      
        success: function(json) {
            if (json['error']) {
                $('#basket').before('<div class="warning">' + json['error'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
            }

            if (json['redirect']) {
                location = json['redirect'];
            }
        }
    });
});
//-->
</script>

So that is how and where these files are, the total also has a controller and coupon and all the other modules are controller and standard MVC driven.

External Coupon Cart Form

So for usage on external pages as you wished, plucking for the tpl files and the $modules and $module loop, code should be:

(making sure "slash" index.php in case of SEO URI)

Sure, example, on your about us page:

<strong>Please enter your coupon:</strong>

<form action="/index.php?route=total/coupon/calculate" method="post" enctype="multipart/form-data" id="basket">
    <input type="text" value="" id="coupon" name="coupon"/>
    <input type="hidden" value="coupon" name="next"/>
    <input type="submit" class="button" value="Apply Coupon"/>
</form>

OTHER TIPS

I've tried putting those forms on a page and they are working for me. Entering a coupon/voucher code takes me to the checkout page with those codes already there for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top