In NopCommerce I need to display the shopping cart outside of the the HeaderLinks partial, where should I add a new controller?

StackOverflow https://stackoverflow.com/questions/23553713

  •  18-07-2023
  •  | 
  •  

Question

I need to display the shopping cart outside of the the HeaderLinks partial which takes the cart items out of the model passed by the CommonController.HeaderLinks action. By creating a new controller with an action that passes the cart info I was able to get the custom ui element from our template working. Right now the controller is inside a custom plugin I got going for some other stuff. The way I see it I have two options:

  • Leave the controller in the plugin project and live with the fact that if the plugin for some reason is not installed (i.e. a fresh checkout from a new dev.) the theme is going to break, possibly redirecting the user to the error view.

  • Put the controller in the Controllers folder at Nop.Web with the downside that this would add an extra step to the process of upgrading NopCommerce. If similar issues arise this could get ugly pretty quick.

So my question is: where is the best place to put the controller? Or is there a simpler way to do this thing with the shopping cart?

Regards, Jose

Was it helpful?

Solution

i will show you a shortcut way to solve it. First create a partial (razor) view e.g _CustomPartialView and then add these usings at the top inside the partial view

@using Nop.Core;
@using Nop.Core.Infrastructure;
@using Nop.Services.Orders;

then next is to query the shopping cart which is easy becuase nopCommerce has already static way of accessing depency resolver method i.e EngineContext.Current.Resolve<T>(). In your case (for shopping cart) it could be the following;

@{
        var shoppingCartEnabled = EngineContext.Current.Resolve<Nop.Services.Security.IPermissionService>()
            .Authorize(Nop.Services.Security.StandardPermissionProvider.EnableShoppingCart);
        var customer = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer;

        int shoppingCartItems = 0;
        if (customer.HasShoppingCartItems)
        {
            shoppingCartItems = customer.ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == Nop.Core.Domain.Orders.ShoppingCartType.ShoppingCart)
                .LimitPerStore(EngineContext.Current.Resolve<IStoreContext>().CurrentStore.Id)
                .ToList()
                .GetTotalProducts();
        }
        if (shoppingCartEnabled)
        {
            <div class="header-right pull-right wrap-cart hidden-xs ">
                <div class="cart-top pull-right">
                    <div id="cart">
                        <span class="icon fa fa-shopping-cart"></span>
                        <div class="heading">
                            <a href="@Url.RouteUrl("ShoppingCart")" class="ico-cart dropdown-toggle visible-md visible-lg" data-toggle="dropdown" data-hover="dropdown">
                                <h4 class="cart-label">
                                    @T("ShoppingCart")
                                    <span>@T("ShoppingCart.HeaderQuantity", shoppingCartItems)</span>
                                </h4>
                            </a>
                        </div>
                        <div class="content">
                            @if (!String.IsNullOrWhiteSpace(Html.Action("FlyoutShoppingCart", "ShoppingCart").ToString()))
                            {
                                <li>@Html.Action("FlyoutShoppingCart", "ShoppingCart")</li>
                            }
                        </div>

                    </div>
                </div>
            </div>
        }
    }

Let me know if you need more help :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top