Discount by payment type

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
dhdennis
Ensign (ENS)
Ensign (ENS)
Posts: 1
Joined: Wed Jun 09, 2010 4:13 pm

Discount by payment type

Post by dhdennis » Wed Jun 09, 2010 6:00 pm

We are looking at a discount type by single credit card used. Is it possible to enable a 10% discount for consumers who only use American Express with AbleCommerce?

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

Re: Discount by payment type

Post by mazhar » Thu Jun 10, 2010 6:01 am

I think you need to make some smal customization to accomplish this. This is not possible out of the box. First create new user group let say it Credit Card Discount Group. Then create a coupon with 10% discount amount. Now edit website/checkout/paymentforms/CreditCardPaymentForms.ascx file and add following method to it just above closing </script> tag

Code: Select all

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 />";
        }
    }
Then locate following line of code

Code: Select all

Payment payment = GetPayment();
and update it as

Code: Select all

            if (CardType.SelectedItem.Text == "payment method name")
            {
                if (!Token.Instance.User.IsInGroup(groupId))
                {
                    Token.Instance.User.UserGroups.Add(new UserGroup(Token.Instance.User.UserId, groupId));
                    Token.Instance.User.Save();
                    ApplyCoupon("couponcode");
                }
            }
            Payment payment = GetPayment();
Now for payment method name specify the payment method for whom you want to give 10% discount. For groupId you need to provide the GroupId of your newly created group. You can quickly check it by just after creating new group click the user icon and it will take you to a page for group member listing. You can see GroupId value in query string just copy that integer value and replace it here. Finally for couponcode provide the coupon code of 10% discount coupon. Now try placing an order with desired payment method for testing.

bhill
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 84
Joined: Mon Oct 31, 2005 5:06 pm
Location: Institute

Re: Discount by payment type

Post by bhill » Thu Aug 28, 2014 2:51 pm

This would be great! I'm trying it out for our new bitcoin plug-in. Since we are not using a credit card type can you please tell me what would this line need to be changed to:
if (CardType.SelectedItem.Text == "payment method name")

maybe PaymentMethod?

Post Reply