Discount Routing / Filter Attributes Sealed

Hace 1 semana
I am trying to build a plugin that would allow me to route a customer to a specific product page when a discount code is used.

User would go to https://store.com?discountcoupon=abc123 (default coupon route)
The discount would be applied to the cart, as it is now, but then (if the discount has been assigned to a product or category) the user would be routed to the product/category that the coupon is related to.

However, upon looking into how i could do this, all of the MVC filter classes are sealed, and cannot be overridden, which is what I would prefer to do, so as to keep the standard nop functionality.
Was this done intentionally to prevent 3rd party plugins from overriding core nop functionalities?? Anyone know why this is designed like this?

I could just make a plugin route that applies the discount and also handles the routing, but I always try to implement a plugin using the tools already available rather than building the same thing from scratch that requires independent maintenance.

Thanks!

Please, no solicitation.
Hace 1 semana
Hi,
This is done intentionally. Besides the fact that attribute sealing is a recommendation to improve performance, there is another side of the coin. This is because a derived attribute class can change the behavior of the original attribute, which is undesirable from a security point of view.
You can find more information in this article.
Hace 1 semana
You can create a plugin with a class that inherits from ActionFilterAttribute, and then have that filter be applied globally (to all action methods in all controllers).

using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

public class DiscountCodeRedirectFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var request = filterContext.HttpContext.Request;

        var discountCode = request.Query["discountcoupon"];
        if (!string.IsNullOrEmpty(discountCode))
        {
            var redirectUrl = "/camera-photo"; // Replace with your product page URL
            filterContext.Result = new RedirectResult(redirectUrl);
        }
    }
}

Typically, an ActionFilterAttribute needs to be applied to an action method or a controller in order for it to take effect. To be applied globally, add it to the global filter collection during application startup.  I.e., in the ConfigureServices method in the NopStartup class in your NopStartup.cs file:
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews(options =>
    {
        options.Filters.Add(new DiscountCodeRedirectFilter());
    });
}


However, there's a bit more effort/coding needed if you want to use Dependency Injection  (e.g. use IDiscountService), or you can just "Resolve" the services manually in the method.
Hace 5 días
Is there a way to know what order the filters will be executed?
Will anything I add be executed last?

For example, if someone visits with a discount code specified, will it get applied to the cart by the default filter (and the rest of the filters), and then hit my filter last, which will execute its code to find the related db entry that tells it where to route that user?

Or is there no guarantee on order of execution and I should I apply the discount to the cart with my filter in case the default filter is never hit?
Hace 5 días
There is Order property, see docs.