Display in cart category name or category ids the product belongs to? FlyoutShoppingCart.cshtml

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 ano atrás
Hi.

NC 3.90

I want to display category name or category id of the product in FlyoutShoppingCart.cshtml


@model MiniShoppingCartModel
@using Nop.Web.Models.ShoppingCart;
<div id="flyout-cart" class="flyout-cart">
    <div class="mini-shopping-cart">
        <div class="count">
            @if (Model.TotalProducts == 0)
            {
                @T("ShoppingCart.Mini.NoItems")
            }
            else
            {
                @Html.Raw(string.Format(T("ShoppingCart.Mini.ItemsText").Text, string.Format("<a href=\"{0}\">{1}</a>", Url.RouteUrl("ShoppingCart"), string.Format(T("ShoppingCart.Mini.Items").Text, Model.TotalProducts))))
            }
        </div>
        @if (Model.TotalProducts > 0)
        {
            <div class="items">
                @for (int i = 0; i < Model.Items.Count; i++)
                {
                    var item = Model.Items[i];
                    <div class="item @(i == 0 ? "first" : null)">
                        @if (Model.ShowProductImages)
                        {
                            <div class="picture">
                                <a href="@Url.RouteUrl("Product", new { SeName = item.ProductSeName })" title="@item.Picture.Title">
                                    <img alt="@item.Picture.AlternateText" src="@item.Picture.ImageUrl" title="@item.Picture.Title" />
                                </a>
                            </div>
                        }
                        <div class="product">
                            <div class="name">
                                <a href="@Url.RouteUrl("Product", new { SeName = item.ProductSeName })">@item.ProductName</a>
                            </div>

<div class="productCategory">HERE I WANT TO DISPLAY PRODUCT CATEGORY NAME OR ID</div>

                            @if (!String.IsNullOrEmpty(item.AttributeInfo))
                            {
                                <div class="attributes">
                                    @Html.Raw(item.AttributeInfo)
                                </div>
                            }
                            <div class="price">@T("ShoppingCart.Mini.UnitPrice"): <span>@item.UnitPrice</span></div>
                            <div class="quantity">@T("ShoppingCart.Mini.Quantity"): <span>@item.Quantity</span></div>
                        </div>
                    </div>
                }
            </div>
            <div class="totals">@T("ShoppingCart.Totals.SubTotal"): <strong>@Model.SubTotal</strong></div>
            <div class="buttons">
                @if (Model.DisplayShoppingCartButton)
                {
                    <input type="button" value="@T("ShoppingCart.Mini.ViewCart")" class="button-1 cart-button" onclick="setLocation('@(Url.RouteUrl("ShoppingCart"))')" />
                    
                }
                @if (Model.DisplayCheckoutButton)
                {
                    var checkoutUrl = "";
                    if (Model.AnonymousCheckoutAllowed && Model.CurrentCustomerIsGuest)
                    {
                        checkoutUrl = Url.RouteUrl("LoginCheckoutAsGuest", new { returnUrl = Url.RouteUrl("ShoppingCart") });
                    }
                    else
                    {
                        checkoutUrl = Url.RouteUrl("Checkout");
                    }
                    <!-- <input type="button" value="@T("Checkout.Button")" class="button-1 checkout-button" onclick="setLocation('@checkoutUrl')" /> -->
          <a href="/onepagecheckout" style="display: inline-block;width: 100%;border: none;background-color: #069;padding: 5px 10px;font-size: 12px;font-weight: bold;     color: #fff;text-transform: uppercase;text-align: center;">@T("Checkout.Button")</a>
                }
            </div>
        }
    </div>
</div>



How can I do it?

Thank you! :)
1 ano atrás
1. add new field CategoryNames  here \src\Presentation\Nop.Web\Models\ShoppingCart\MiniShoppingCartModel.cs

public partial class ShoppingCartItemModel : BaseNopEntityModel
        {
...
            public string CategoryNames { get; set; }
        }


2. change factory method here \src\Presentation\Nop.Web\Factories\ShoppingCartModelFactory.cs

public partial class ShoppingCartModelFactory : IShoppingCartModelFactory
    {
        #region Fields
...
       private readonly ICategoryService _categoryService;

        #endregion

        #region Ctor

        public ShoppingCartModelFactory(
...
            ICategoryService categoryService)
        {
...
            this._categoryService = categoryService;
        }

        #endregion
public virtual MiniShoppingCartModel PrepareMiniShoppingCartModel()
        {
...
var cartItemModel = new MiniShoppingCartModel.ShoppingCartItemModel
                        {
                            Id = sci.Id,
                            ProductId = sci.Product.Id,
                            ProductName = sci.Product.GetLocalized(x => x.Name),
                            ProductSeName = sci.Product.GetSeName(),
                            Quantity = sci.Quantity,
                            AttributeInfo = _productAttributeFormatter.FormatAttributes(sci.Product, sci.AttributesXml),
                            CategoryNames = string.Join(",",_categoryService.GetProductCategoriesByProductId(sci.Product.Id).Select(x => x.Category.Name).ToList())
                        };

3. add this code in FlyoutShoppingCart.cshtml

                       <div class="productCategory">@item.CategoryNames</div>

1 ano atrás
Rashed Khan wrote:
1. add new field CategoryNames  here \src\Presentation\Nop.Web\Models\ShoppingCart\MiniShoppingCartModel.cs


Thank you. Is it possible to do it without source code?
1 ano atrás
zaf wrote:
Is it possible to do it without source code?

possible solution
1. use ModelPrepared event consumer, IConsumer<ModelPrepared<BaseNopModel>>. add your extra properties inside
        public Dictionary<string, object> CustomProperties { get; set; }
but 3.9 version this not available
2. you can make a widget plugin, pass your product id to the view component and prepare category names and push it through widget zone of your pointed areas
3. you can override PrepareShoppingCartModel factory method from your plugin and put category names at CustomProperties dictionary and show category names from there

note: CustomProperties  exits at BaseNopModel
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.