Overriding Register and My Account

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Is it feasible to override both 'registration' and 'my account' controllers in order to implement a membership look-up? The scenario is this. We want to use club membership, managed by a third party (RevUp), to provide a 10% discount to verified members. I have already added a Customer Attribute for the user to enter their membership number in the registration form. So far so good.

What needs to happen is when a user registers or updates their details a call to RevUp's API verifies their membership and returns a Boolean result, if true the member is added to a custom membership role in nopComerce which can then be used to apply a 10% discount on products they purchase.

Ideally I'd like to do this via a plugin rather than modifying the core code (I'm not a c# coder so have a double learning curve) or there a better or alternative way of doing this?

Thank you.
7 years ago
If you want to change the behavior of controller/action, try ActionFilter. Here's a tutorial I've written: Overriding (Intercepting) NopCommerce Controllers And Actions
7 years ago
wooncherk wrote:
If you want to change the behavior of controller/action, try ActionFilter. Here's a tutorial I've written: Overriding (Intercepting) NopCommerce Controllers And Actions


Thank you. I'll look at that later.
7 years ago
Plugin way is more difficult but more rewarding and fun. I think you can do this with a plugin and if you do so you will come to the exact problem i had to deal. Lucky for you i solved it :

https://www.nopcommerce.com/boards/t/46349/overriding-productdetails-action-for-plugin.aspx
7 years ago
wooncherk wrote:
If you want to change the behavior of controller/action, try ActionFilter. Here's a tutorial I've written: Overriding (Intercepting) NopCommerce Controllers And Actions


Argh does this mean i didn't need to get in all those routing mechanisms to implement my own ProductDetails action?

All i wanted to is fetch a product record update another table using it's product id called ProductViewCounter.

Can we get the models or productids in action filters?
7 years ago
Rakyuz wrote:
Can we get the models or productids in action filters?


Yes, you can simply use action filter get product id and send it to your plugin controller and update view count.

Action Filter:

    public class TestFilter : ActionFilterAttribute, IFilterProvider, IActionFilter
    {
        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            if (controllerContext.Controller is Nop.Web.Controllers.ProductController && actionDescriptor.ActionName.Equals("ProductDetails", StringComparison.InvariantCultureIgnoreCase))
            {
                return new List<Filter>() { new Filter(this, FilterScope.Action, 0) };
            }

            return new List<Filter>();
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var routeValues = new RouteValueDictionary();
            var productId = int.Parse(filterContext.RouteData.Values["productId"].ToString());

            var myController = EngineContext.Current.Resolve<TestController>(); // my plugin controller
            filterContext.Result = myController.UpdateViewCount(productId); // my controller method
        }
    }  



Register Dependency:

builder.RegisterType<TestFilter>().As<IFilterProvider>().SingleInstance(); 


Side: TestFilter, TestController and UpdateViewCount are my custom names, you can use yours.
You'll be need to add some references of DLLs  

Hope this helps!
7 years ago
This is great.

Thank you very much i will update my plugin with this for cleaner code!
7 years ago
Rakyuz wrote:
This is great.

Thank you very much i will update my plugin with this for cleaner code!


Glad I could help!
7 years ago
Getting this far without error is an achievement however I'm not getting the result I expected. The customer variable is not returning the logged in user. I'd appreciate any pointers on this please.

  
 public class LoginFilterProvider : ActionFilterAttribute, IFilterProvider
    {
        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            if ((actionDescriptor.ControllerDescriptor.ControllerType == typeof(CustomerController)) &&
                    (actionDescriptor.ActionName.Equals("Login")) && controllerContext.HttpContext.Request.HttpMethod == "POST")
            {
                return new List<Filter>() { new Filter(this, FilterScope.Action, 0) };
            }
            return new List<Filter>() { };
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //base.OnActionExecuted(filterContext);
            var viewData = filterContext.Controller.ViewData;
            if (viewData.ModelState.IsValid)
            {

                var customer = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer;
                var model = new CustomerInfoModel();

                model.FirstName = customer.GetAttribute<string>(SystemCustomerAttributeNames.FirstName);
                model.LastName = customer.GetAttribute<string>(SystemCustomerAttributeNames.LastName);

            }
        }

    }
7 years ago
I would really appreciate some help in getting this to return meaningful data. Thanks.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.