Welcome Guest, Not a member yet? Register   Sign In
Coupon/discount code for the cart class
#1

[eluser]mighty_falcon[/eluser]
Hey there,

Has anyone attempted at adding somekind of coupon/discount feature to the cart library yet? I am in need of such a feature and looked all over the forums, but wanted to make sure before venturing into doing it myself?

Thanks
#2

[eluser]WanWizard[/eluser]
What do you exactly want it to do?

It seems to me that this is more a functionality of your application, not of the cart class.
#3

[eluser]mighty_falcon[/eluser]
Not quite. I ended up adding the following functions to the extended multi_cart library that was posted here on the forum sometime ago. For those that may be interested in the future here are the functions i added:
Code:
function coupon($coupon = array(), $cart = 'default') {
        // does the requested cart exist
        if ( ! isset($this->_cart_contents[$cart]) ) {
            log_message('error', 'The requested cart is not created yet.');
            return FALSE;
        }

        $this->_cart_contents[$cart]['coupon'] = $coupon;


        if($this->_save_cart($cart)) {
            return TRUE;
        }
        else return FALSE;
    }

    function has_coupon($cart = 'default') {
        // does the requested cart exist
        if ( ! isset($this->_cart_contents[$cart]) ) {
            log_message('error', 'The requested cart is not created yet.');
            return FALSE;
        }

        if(isset($this->_cart_contents[$cart]['coupon'])) {
            return $this->_cart_contents[$cart]['coupon'];
        }
        else return FALSE;
    }

    function sub_total($cart = 'default') {
        // does the requested cart exist
        if ( ! isset($this->_cart_contents[$cart]) ) {
            log_message('error', 'The requested cart is not created yet.');
            return FALSE;
        }

        return $this->_cart_contents[$cart]['cart_total'];

    }

The changed the total function to
Code:
function total($cart = 'default') {
        // does the requested cart exist
        if ( ! isset($this->_cart_contents[$cart]) ) {
            log_message('error', 'The requested cart is not created yet.');
            return FALSE;
        }

        if(isset($this->_cart_contents[$cart]['coupon']['discount'])) $total = $this->_cart_contents[$cart]['cart_total'] * (1 + $this->_cart_contents[$cart]['coupon']['discount']/100);
        else $total = $this->_cart_contents[$cart]['cart_total'];

        return $total;
    }

So now you have a sub-total to display to the user if they have applied a coupon and in the end a final total.


Also in the constructor, under the last if make sure you set the index for the coupon:
Code:
$this->_cart_contents['default']['coupon'] = array();
#4

[eluser]blindrc[/eluser]
I've built a form where the user can input a coupon code. My Model gets the coupon information from the database, and the controller iterates through the info comparing it with the user submitted coupon code. If there is a match, the total price is discounted. I have everything working (with xss / form validation in place) except that when a user updates the cart...the total price goes back to the original amount. Is your code designed to work with coupons being read from a database? I've tried to extend the CI_Cart library with your code but have had no luck.

Can you explain how this code works? Or what extended library it is based on?

Thank you.
#5

[eluser]blindrc[/eluser]
anyone have any ideas on how to implement and use coupons with the cart class?




Theme © iAndrew 2016 - Forum software by © MyBB