How to Display "Out of Stock" in the Product Box.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
I must let out the "Out of Stock" message on ProductBox when the "Stock Quantity" product value is 0. Can you help me with that?

Use NopCommerce 3.20
7 years ago
prodigio100 wrote:
I must let out the "Out of Stock" message on ProductBox when the "Stock Quantity" product value is 0. Can you help me with that?

Use NopCommerce 3.20


Soln1: if you to modify core code:==>

Add a field ProductOverviewModel.cs (namespace Nop.Web.Models.Catalog)
like bellow==>

public string StockAvailability { get; set; }


Map data from PrepareProductOverviewModels(...)  in namespace Nop.Web.Controllers


Code will look like
After adding "StockAvailability = product.FormatStockMessage(_localizationService)" CatalogController

[NonAction]
        protected IEnumerable<ProductOverviewModel> PrepareProductOverviewModels(IEnumerable<Product> products,
            bool preparePriceModel = true, bool preparePictureModel = true,
            int? productThumbPictureSize = null, bool prepareSpecificationAttributes = false,
            bool forceRedirectionAfterAddingToCart = false)
        {
            if (products == null)
                throw new ArgumentNullException("products");

            var models = new List<ProductOverviewModel>();
            foreach (var product in products)
            {
                var model = new ProductOverviewModel()
                {
                    Id = product.Id,
                    Name = product.GetLocalized(x => x.Name),
                    ShortDescription = product.GetLocalized(x => x.ShortDescription),
                    FullDescription = product.GetLocalized(x => x.FullDescription),
                    SeName = product.GetSeName(),
                    StockAvailability = product.FormatStockMessage(_localizationService),
                };
                //price
.......


and go to _PrpductBox.cshtml  and _ProductBox.Mobile.cshtml

DefaultClean File Path(3.20):  \Nop.Web\Views\Catalog\_ProductBox.cshtml  or _ProductBox.Mobile.cshtml
Other Theme: ~\Themes\{Themename}\Views\Catalog\_ProductBox.cshtml or _ProductBox.Mobile.cshtml
use like bellow


<span>Model.StockAvailability </span>
7 years ago
I do not find "Product Over viewModel.cs"
which means "Map data from PrepareProductOver ViewModels".
I'm not a programmer, you can give me directions simpler?
Thank you
7 years ago
prodigio100 wrote:
I do not find "Product Over viewModel.cs"
which means "Map data from PrepareProductOver ViewModels".
I'm not a programmer, you can give me directions simpler?
Thank you


go to _PrpductBox.cshtml  and _ProductBox.Mobile.cshtml

And try with like bellow


@{
    var product = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Catalog.IProductService>().GetProductById(Model.Id);
    var stockMessage = string.Empty;
    if (product != null)
    {
        stockMessage = product.StockQuantity == 0 ? "Out of stock" : "Avaliable in Stock";
    }

}
<span>@stockMessage</span>
7 years ago
Waooo, how it works, you're great!!!!!

Can you tell me how to get out in the green "Avaliable in Stock" and the Red color "Out of stock", and how to change the font and font size?

Thank you
7 years ago
prodigio100 wrote:
Waooo, how it works, you're great!!!!!

Can you tell me how to get out in the green "Avaliable in Stock" and the Red color "Out of stock", and how to change the font and font size?

Thank you


Try with ==>


@{
    var product = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Catalog.IProductService>().GetProductById(Model.Id);
    var stockMessage = string.Empty;
    bool isOutOfStock = false;
    if (product != null)
    {
        stockMessage = product.StockQuantity == 0 ? "Out of stock" : "Avaliable in Stock";
        isOutOfStock = product.StockQuantity == 0;
    }

}

<style>
    .outofstock {
        font: normal 13px "Trebuchet MS", Arial, Helvetica, sans-serif;
        color: red;
      font-size: 14px;
    }
    .stockavailable {
        font: normal 13px "Trebuchet MS", Arial, Helvetica, sans-serif;
        color: green;
        font-size: 14px;
    }
</style>
@if (isOutOfStock)
{
    <span class="outofstock">@stockMessage</span>
}
else
{
    <span class="stockavailable">@stockMessage</span>
}


Note: It will better to take help from css expert.
7 years ago
Thanks Sohel,
It all works fine.
You're a great one.
7 years ago
prodigio100 wrote:
Thanks Sohel,
It all works fine.
You're a great one.


Welcome! And also thanks for informing that your problem is solved.
7 years ago
How can I make this work with multiple warehouses?

Thanks in advance for your help.
Nico De Bruin
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.