nopcommerce: How to get Role Name from applied discount coupon?

//nopcommerce: How to get Role Name from applied discount coupon?

nopcommerce: How to get Role Name from applied discount coupon?

By |2012-10-11T11:08:02+00:00October 11th, 2012|Categories: Programming|Tags: , , , |0 Comments

– Create a discount from the cms admin

– Add  a requirement for that discount and select “Must be assigned to customer role” and choose a role.

The next steps can be done anywhere in the website, I will use the CommonController to show the role name inside the website header beside the user email

–  Go to CommonController  and inside the HeaderLinks method add these lines:

// Added by treenodes 10 Oct 2012
            string discountRoleName = string.Empty;
            if (discount !=null && discount.DiscountRequirements != null)
            {
                var requ = discount.DiscountRequirements.FirstOrDefault();
                foreach (var customerRole in customer.CustomerRoles.Where(cr => cr.Active).ToList())
                    if (requ != null && requ.RestrictedToCustomerRoleId == customerRole.Id)
                        discountRoleName = customerRole.Name;
            }

So the method will look like this

 

– You will need to declare the _discountService, so declare it in the Fields region

and then initialize it in the controller constructor:

– Add DiscountRoleName property inside the HeaderLinksModel

– Inside the HeaderLinks view, inside the IsAuthinticated check, add this:

<li><a href="#">@Model.DiscountRoleName</a></li>

– Login to the store and apply a coupon code you just created, you will see the role name of this applied discount coupon in the header. 

About the Author:

Senior Software Engineer.

Leave A Comment