display product availability in _ProductBox.cshtml

Posted: April 12, 2016 at 4:50 PM Quote #165574
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.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: April 13, 2016 at 1:38 AM Quote #165591
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
This post/answer is useful
3
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
www.Nop-Templates.com - the best place for nopCommerce themes and plugins

Follow us on
Facebook: http://www.facebook.com/NopTemplates
Twitter: http://twitter.com/NopTemplates
Blog: http://www.nop-templates.com/blog

www.Nop-Templates.com
nopCommerce Solution Partners
Posted: April 13, 2016 at 8:30 AM Quote #165629
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
This post/answer is useful
2
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: April 14, 2016 at 2:26 AM Quote #165704
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
This post/answer is useful
1
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
www.Nop-Templates.com - the best place for nopCommerce themes and plugins

Follow us on
Facebook: http://www.facebook.com/NopTemplates
Twitter: http://twitter.com/NopTemplates
Blog: http://www.nop-templates.com/blog

www.Nop-Templates.com
nopCommerce Solution Partners
Posted: April 14, 2016 at 8:19 AM Quote #165742
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
This post/answer is useful
1
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.
eCommerce CONFERENCE 2017
Learn more