I need to apply a coupon code automatically based on the affiliate ID.
How could I do this?
Thanks in advance!
Is there a way to automatically apply a coupon code?
Re: Is there a way to automatically apply a coupon code?
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 />";
}
}
Re: Is there a way to automatically apply a coupon code?
Thank you so much, it worked perfectly!
Re: Is there a way to automatically apply a coupon code?
that's pretty convenient, where should I put that code?
Re: Is there a way to automatically apply a coupon code?
It depends upon requirement, for example have a look at following threadsaiho01 wrote:that's pretty convenient, where should I put that code?
viewtopic.php?f=42&t=10921
Re: Is there a way to automatically apply a coupon code?
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
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