display product availability in _ProductBox.cshtml

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Hello. I want to get the product stock count in the _ProductBox.cshtml so the category list will show if the product is out of stock. Does anyone have the code that will tell me in that view?
I am using nop 3.6.
Thanks.
8 years ago
ddegil wrote:
Hello. I want to get the product stock count in the _ProductBox.cshtml so the category list will show if the product is out of stock. Does anyone have the code that will tell me in that view?
I am using nop 3.6.
Thanks.


Hi,

You can modify the controller extension "PrepareProductOverviewModels". You can find it here: /Presentation/Nop.Web/Extensions/ControllerExtensions.cs.

Just add this row inside it:
model.CustomProperties.Add("stockQuantity", product.StockQuantity);


Then go to your _ProductBox view and add this code:

@if (Model.CustomProperties.ContainsKey("stockQuantity"))
        {
            <div class="quantity">
                <span>Quantity:</span>
                <span>@Model.CustomProperties["stockQuantity"]</span>
            </div>
        }


I hope this helped !

Regards,
Deni
8 years ago
Nop-Templates.com wrote:
You can modify the controller extension "PrepareProductOverviewModels". You can find it here: /Presentation/Nop.Web/Extensions/ControllerExtensions.cs.


Hi Deni,

I was hoping to do it in the view only and not recompile the source. Your post gave me the info I needed to do it that way though, so thank you. What I did (in case someone wants to do this as well):
In the _ProductBox.cshtml view I added:
@using Nop.Services.Catalog;


Then at the end of the top
@{}
I added:
var productQuantity = EngineContext.Current.Resolve<IProductService>().GetProductById(Model.Id).StockQuantity;


Lastly, in the page I wanted to react to the stock quantity I added:
@if (productQuantity < 1)
   {
      <span class="out-of-stock">Out of Stock</span>
   }


Thanks for the clues that helped me!

Sincerely,

David
8 years ago
ddegil wrote:
Hi Deni,

I was hoping to do it in the view only and not recompile the source. Your post gave me the info I needed to do it that way though, so thank you.

Thanks for the clues that helped me!

Sincerely,

David



Hi David,

I was hesitating to show you the "View" approach as it is easier. But I showed you the modification in the "PrepareProductOverviewModels" method in order of performance - because opening a page with 20 products would make additional 20 calls to the database.

But now I remembered that the product is cached per request so there will not be any additional calls to the database.

So, your approach is good enough !


Regards,
Deni
8 years ago
Nop-Templates.com wrote:
I was hesitating to show you the "View" approach as it is easier. But I showed you the modification in the "PrepareProductOverviewModels" method in order of performance - because opening a page with 20 products would make additional 20 calls to the database.

Hi Deni,

Thanks for the follow up. Though I have Visual Studio and CAN recompile the source, I prefer to use code in the views if possible. That way I do not have to worry about moving modifications into source IF/WHEN I decide to upgrade to a new nop version OR if I decide to use another nop store. Also I like to show how to do it in views on the chance someone else wants to do the same thing and they are not a programmer and/or have Visual Studio. Still I am glad to learn how to do it in the source so am always glad to be shown. I am not a C# developer, working with VB and JavaScript is what I know, so when I look into the source files it helps me learn the C# side more. :)

SO thank you for your source  change suggestion and info, it helped me to look at the files and helped me to figure out how to do it in the view. :)


Sincerely,

David
6 years ago
ddegil wrote:
@if (productQuantity < 1)
   {
      <span class="out-of-stock">Out of Stock</span>
   }


Hello! Thanks for your post, it helped me... But how to correct that code if I want it to show both In Stock and Out Of Stock? Now it just shows OUT OF STOCK for those products which are not in stock.

What code will I need in this case?

Thanks.
3 years ago
This solution is no longer working in the 4.30 version.
Anyone done this in this version?
3 years ago
Neither in 4,20 work anymore
3 years ago
The update is to add @using Nop.Core.Infrastructure
Also if you are using a theme then you need to edit the correct _ProductBox.cshtml
i.e. You may find one in the theme directory
\Themes\<ThemeName>\Views\Shared\_ProductBox.cshtml

In the _ProductBox.cshtml  add:
@using Nop.Core.Infrastructure;
@using Nop.Services.Catalog;

Then at the end of the top
@{}
Add:
var productQuantity = EngineContext.Current.Resolve<IProductService>().GetProductById(Model.Id).StockQuantity;

In the main section - maybe under the SKU Display Add
@if (productQuantity < 1)
{
    <span class="out-of-stock">Out of Stock</span>
}
else
{
    <span class="stock-quantity">Stock Quantity: @productQuantity</span>
}

This works in v4.2 & v4.3
2 years ago
Hello all,

I am using 4.40.4. Would this approach work for adding the manufacturer to each product in the product box? I imagine since there doesnt need to be logic, I just add some include and insert the variable somewhere in the view? I am not a developer but can fudge my way around code and make small changes to the views.

Regards
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.