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
public string StockAvailability { get; set; }
[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
.......
<span>Model.StockAvailability </span>
@{
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>
@{
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>
}