Discount Code in URL

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
9 лет назад
Hi there!

So, is possible to automaticly apply a discount code received from URL parameter?

E.g.: /product?c=123456

Do you know a plugin or something similar?

Thanks!
9 лет назад
Hi,

Assuming you are after discount coupon. I dont know of any plugin existing if i am not wrong.

Yes, i did same thing but modified the source code to pick the discount code from url. When we send out promotional email, we used to get lots of calls from customers saying we forgot to add the coupon and asking us to add. So this is a headache for operational crew. So i modified the source code to accept discount coupon from an email link.

Here is how it is done,

01. Create an Attribute class (Recommended).  or you can use an existing attribute class. I used StoreIpAddressAttribute. this is located under Nop.Web.Framework project.

02. Add following code inside OnActionExecuting method.

           

            if (!DataSettingsHelper.DatabaseIsInstalled())
                return;

            if (filterContext == null || filterContext.HttpContext == null || filterContext.HttpContext.Request == null)
                return;

            //don't apply filter to child methods
            if (filterContext.IsChildAction)
                return;

            //only GET requests
            if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
                return;

var discountService = EngineContext.Current.Resolve<IDiscountService>();
            Uri uri = new Uri(pageUrl);
            string discountcouponcode = HttpUtility.ParseQueryString(uri.Query).Get(SystemCustomerAttributeNames.DiscountCouponCode.ToLower());

            if (!string.IsNullOrWhiteSpace(discountcouponcode))
            {
                var lastDiscountcouponcode = workContext.CurrentCustomer.GetAttribute<string>(SystemCustomerAttributeNames.DiscountCouponCode);
                if (!String.IsNullOrWhiteSpace(discountcouponcode) && (String.IsNullOrWhiteSpace(lastDiscountcouponcode) || !lastDiscountcouponcode.ToLower().Equals(discountcouponcode.ToLower())))
                {
                    var discount = discountService.GetDiscountByCouponCode(discountcouponcode);
                    bool isDiscountValid = discount != null &&
                        discount.RequiresCouponCode &&
                        discountService.IsDiscountValid(discount, workContext.CurrentCustomer, discountcouponcode);
                    if (isDiscountValid)
                    {
                        genericAttributeService.SaveAttribute(workContext.CurrentCustomer,
                            SystemCustomerAttributeNames.DiscountCouponCode, discountcouponcode);
                    }
                }
            }


03. If you use new attribute add this attribute in BaseNopController class. This is in  Nop.Web project. Alternatively, if you updated any existing filters, then step 3 is no need.
9 лет назад
For the same reason you said, I need to do it.

Where the pageUrl variable is coming from?

workContext and genericAttributeService, where do you created it?

Im using Nop 3.3.

Thanks!
9 лет назад
ivanslater wrote:
For the same reason you said, I need to do it.

Where the pageUrl variable is coming from?

workContext and genericAttributeService, where do you created it?

Im using Nop 3.3.

Thanks!


Here is the answers,

            var webHelper = EngineContext.Current.Resolve<IWebHelper>();
            var pageUrl = webHelper.GetThisPageUrl(true);
            var workContext = EngineContext.Current.Resolve<IWorkContext>();
            var genericAttributeService = EngineContext.Current.Resolve<IGenericAttributeService>();

in the same method just as first part.
9 лет назад
Done, but I cant find the step 3 class. (I created DiscountURLAttribute class)
9 лет назад
Another question.

How is your discount URL after the changes??

Ivan
9 лет назад
ivanslater wrote:
Another question.

How is your discount URL after the changes??

Ivan


http://www.domain.com?discountcouponcode='discount20'
9 лет назад
Thanks, but I cant find the class of step 3.

03. If you use new attribute add this attribute in BaseNopController class. This is in  Nop.Web project. Alternatively, if you updated any existing filters, then step 3 is no need.
9 лет назад
@jeyara, can you help?
9 лет назад
ivanslater wrote:
@jeyara, can you help?


My answer is based on Nov 3.20. Its the base class where all the controller classes were inherited from.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.