Adding additional step to the checkout process NopCommerce V4.4

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
2 年 前
This is a snippet of My Code :
1- we should create our filter attribute that inheri ts From ActionFilterAttribute
public class WelcomeProgramAttribute : ActionFilterAttribute
    {
        //This is the interface responsible of the verification if the customer should have a Gift
        private readonly IWelcomeGiftCalculationMethod _welcomeGiftCalculationMethod;

        public WelcomeProgramAttribute(IWelcomeGiftCalculationMethod
       welcomeGiftCalculationMethod)
        {
            _welcomeGiftCalculationMethod = welcomeGiftCalculationMethod;
        }

        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //Checking if the request is coming from our action
            if (context.ActionDescriptor is ControllerActionDescriptor actionDescriptor && actionDescriptor.ControllerTypeInfo == typeof(ShoppingCartController)
                && actionDescriptor.ActionName.Equals("Cart"))
            {
               // Applying our logic
                await _welcomeGiftCalculationMethod.CheckWelcomeGiftRules();
                await next();
            }
            else
            {
                await next();
            }
        }
2-This is the class responsible of registering our filter wich implement INopStartup
public class WelcomeProgramStartup : INopStartup
    {

        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<MvcOptions>(options =>
            {
                options.Filters.Add<WelcomeProgramAttribute>();
            });
        }

        public void Configure(IApplicationBuilder application)
        {

        }
        public int Order => 0;
    }
2 年 前
I just succeed in showing a modal when My Filter is executed :
Steps 1 :
I transform my plugin to widget plugin by implementing IWidgetPlugin
Step2 :
i modified the OnActionExecutionAsync like this :
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (context.ActionDescriptor is ControllerActionDescriptor actionDescriptor && actionDescriptor.ControllerTypeInfo == typeof(ShoppingCartController)
                && actionDescriptor.ActionName.Equals("Cart"))
            {
                await _welcomeGiftCalculationMethod.CheckWelcomeGiftRules();
                var controller = context.Controller as ShoppingCartController;
                controller.ViewBag.ShowModal = true;
                await next();
            }
            else
            {
                await next();
            }
        }
Step3:
I added this code to render my widget in a specific widgetZone
public Task<IList<string>> GetWidgetZonesAsync()
        {
            return Task.FromResult<IList<string>>(new List<string> { PublicWidgetZones.ContentBefore });
        }

Step3:
i added a view that will show the modal
@if (ViewBag.ShowModal ?? false)
{

    <script src="https://code.jquery.com/jquery-latest.min.js"></script>

    <script type="text/javascript">
        $(window).on('load', function () {
            $('#myModal').modal('show');
        });
    </script>

    <!-- The Modal -->
    <div class="modal" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- Modal Header -->
                <div class="modal-header">
                    <h4 class="modal-title">Modal Heading</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- Modal body -->
                <div class="modal-body">
                    Modal body..
                </div>

                <!-- Modal footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>

            </div>
        </div>
    </div>
}
the modal will show just when the viewbag.ShowMmodal is set to true.

and Offcorse the viewComponentClass :
public class WelcomeProgramViewComponent : NopViewComponent
    {
        public async Task<IViewComponentResult> InvokeAsync(string widgetZone, object additionalData)
        {
            return await Task.Run(() => View("~/Plugins/Farmasi.WelcomeProgram/Views/GiftModal.cshtml"));
        }
    }

I Hope that this will help :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.