Is there a way to automatically apply a coupon code?

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
valtinho
Ensign (ENS)
Ensign (ENS)
Posts: 12
Joined: Thu Nov 13, 2008 9:11 am

Is there a way to automatically apply a coupon code?

Post by valtinho » Fri Nov 21, 2008 3:06 pm

I need to apply a coupon code automatically based on the affiliate ID.

How could I do this?

Thanks in advance!

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Is there a way to automatically apply a coupon code?

Post by mazhar » Sat Nov 22, 2008 12:26 am

You can check the affliate id on the page where you want to apply the coupon. Depending upon the affliate you can apply some coupon. For example

Code: Select all

protected void Page_Load(object sender, EventArgs e)
    {
        if (Token.Instance.User.AffiliateId == 2)
            ApplyCoupon("coupon-code");
    }

private void ApplyCoupon(string couponCode) 
    {
        if (String.IsNullOrEmpty(couponCode))
            return;
        Coupon coupon = CouponDataSource.LoadForCouponCode(couponCode);
        String couponValidityMessage = String.Empty;
        if (coupon != null)
        {
            if (!CouponCalculator.IsCouponAlreadyUsed(Token.Instance.User.Basket, coupon))
            {
                if (CouponCalculator.IsCouponValid(Token.Instance.User.Basket, coupon, out couponValidityMessage))
                {
                    Basket basket = Token.Instance.User.Basket;
                    BasketCoupon recentCoupon = new BasketCoupon(Token.Instance.UserId, coupon.CouponId);
                    basket.BasketCoupons.Add(recentCoupon);
                    
                    // APPLY COUPON COMBINE RULE
                    //THE RULE: 
                    //If most recently applied coupon is marked "do not combine", then all previously
                    //entered coupons must be removed from basket.

                    //If most recently applied coupon is marked "combine", then remove any applied
                    //coupon that is marked "do not combine".  (Logically, in this instance there
                    //could be at most one other coupon of this type...)
                    string previousCouponsRemoved = "";

                    if (recentCoupon.Coupon.AllowCombine)
                    {
                        //IF ALLOW COMBINE, REMOVE ALL PREVIOUS NOCOMBINE COUPONS
                        for (int i = (basket.BasketCoupons.Count - 1); i >= 0; i--)
                        {
                            if (!basket.BasketCoupons[i].Coupon.AllowCombine)
                            {
                                if (previousCouponsRemoved.Length > 0)
                                {
                                    previousCouponsRemoved += "," + basket.BasketCoupons[i].Coupon.Name;
                                }
                                else
                                {
                                    previousCouponsRemoved = basket.BasketCoupons[i].Coupon.Name;
                                }

                                basket.BasketCoupons.DeleteAt(i);
                            }
                        }
                    }
                    else
                    {
                        //IF NOT ALLOW COMBINE, REMOVE ALL EXCEPT THIS COUPON
                        for (int i = (basket.BasketCoupons.Count - 1); i >= 0; i--)
                        {
                            if (basket.BasketCoupons[i] != recentCoupon)
                            {
                                if (previousCouponsRemoved.Length > 0)
                                {
                                    previousCouponsRemoved += "," + basket.BasketCoupons[i].Coupon.Name;
                                }
                                else
                                {
                                    previousCouponsRemoved = basket.BasketCoupons[i].Coupon.Name;
                                }
                                basket.BasketCoupons.DeleteAt(i);
                            }
                        }
                    }

                    basket.Save();
                    basket.Recalculate();
                }
                else
                {
                    //Invalid Coupon
                    //Output couponValidityMessage here 
                }
            }
            else
            {
                //"The coupon code you've entered is already in use.<br /><br />";
            }
        }
        else
        {
            //"The coupon code you've entered is invalid.<br /><br />";
        }
    }

valtinho
Ensign (ENS)
Ensign (ENS)
Posts: 12
Joined: Thu Nov 13, 2008 9:11 am

Re: Is there a way to automatically apply a coupon code?

Post by valtinho » Mon Nov 24, 2008 12:09 pm

Thank you so much, it worked perfectly!

saiho01
Ensign (ENS)
Ensign (ENS)
Posts: 13
Joined: Wed Jun 10, 2009 10:35 pm

Re: Is there a way to automatically apply a coupon code?

Post by saiho01 » Mon Jun 22, 2009 12:08 am

that's pretty convenient, where should I put that code?

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Is there a way to automatically apply a coupon code?

Post by mazhar » Mon Jun 22, 2009 4:09 am

saiho01 wrote:that's pretty convenient, where should I put that code?
It depends upon requirement, for example have a look at following thread
viewtopic.php?f=42&t=10921

saiho01
Ensign (ENS)
Ensign (ENS)
Posts: 13
Joined: Wed Jun 10, 2009 10:35 pm

Re: Is there a way to automatically apply a coupon code?

Post by saiho01 » Mon Jun 22, 2009 11:11 am

Those are exactly what I was looking for! I have a few more questions though, bear with me here. These are the 3 conditions I am trying to implement into our shopping cart.

1)introduce a continual 30% discount for subscribers to our service.
2)one time 30% discount for non-subscribers
3)expiration date for the coupon (see it as an introductory discount)

So the question is, what can I do to accomplish that? And if I used the same coupon for both subscribers/non-subscribers with an expiration date within one week, what would happen to the price for subscribers after that?

Thanks

Post Reply