Who to use Action Filter to check product stock in external DB

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Hi everyone!

I need to check the stock of a product in the external DB before it is added to the shopcart, and I have read about “Action Filters” and I think that it can be implemented for this purpose. But my current problem is: How can I get the product that was sent/returned by the Action Filter?

My actual code



namespace Nop.Plugin.Sync.asd
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            builder.RegisterType<CheckWarehouseActionFilter>().As<IFilterProvider>();
        }

        public int Order
        {
            get { return 1; }
        }
    }
}



namespace Nop.Plugin.Sync.asd
{
    public class CheckWarehouseActionFilter : ActionFilterAttribute, IFilterProvider
    {
        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext,
            ActionDescriptor actionDescriptor)
        {
            if (controllerContext.Controller is ShoppingCartController &&
                actionDescriptor.ActionName.Equals("AddProductToCart_Details",
                StringComparison.InvariantCultureIgnoreCase))
            {
                return new List<Filter>() { new Filter (this, FilterScope.Action, 0) };
            }
            return new List<Filter>();
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //TODO: check stock
        }
    }
}
7 years ago
get the Product ID from the Route. filterContext provides you with that info. :)
7 years ago
wooncherk wrote:
get the Product ID from the Route. filterContext provides you with that info. :)


Tkx Wooncherk, this helped me alot. But now i have another issue.

Now i can check (before) when user trying add product from catalog or details views but i cant catch the "Action" before the updateCart method (remove, change Quantity or pass any item from Wishlist to Cart). When i try catch these events, the Actionname is "Cart" and this haven't any ActionParameters.
7 years ago
Check the POST values, since it's actually the POST value (specifically the button name) that differentiates. :)
7 years ago
sorry, How can I do that?
7 years ago
It's all in the request. filterContext.RequestContext.HttpContext.Request.Form :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.