I'm trying to only show products that are in stock on my website.
Currently I have this:
@using System.Globalization
@using TeaCommerce.Api.Models
@using TeaCommerce.Umbraco.Web
@inherits UmbracoViewPage<IPublishedContent>
@{
Layout = "Master.cshtml";
long storeId = long.Parse(Model.GetPropertyValue<string>( "store", true ));
var allProducts = Model.Children();
}
@foreach (var product in allProducts) {
var name = product.GetPropertyValue<string>("productName",true);
Price price = TC.GetPrice(storeId,product.Id.ToString(CultureInfo.InvariantCulture));
var image = product.GetPropertyValue<IPublishedContent>("image");
var q = product.GetPropertyValue<int>("stock");
<a class="item" href="@product.Url">
<img src="@image.Url"/>
<h3>@name</h3>
<p>@price.WithVatFormatted</p>
<span>View More</span>
<p>Quantity: @q</p>
</a>
}
I have tried changing the allProducts to the following but then nothing is returned. I have also tried manually creating a list and adding only products that have a stock level over 0 to the list but again nothing is returned.
I have tried:
var allProducts = Model.Children().Where(x => x.GetPropertyValue<int>("stock") > 0);
My best guess is that stock isnt returning an int. The data type is Tea Commerce: Stock management.
long storeId = long.Parse(Model.GetPropertyValue<string>( "store", true ));
List<IPublishedContent> productsList = new List<IPublishedContent>();
var allProducts = Model.Children();
foreach(var product in allProducts)
{
decimal? stock = TC.GetStock( storeId, product.Id.ToString() );
if(stock != null && stock > 0)
{
productsList.Add(product);
}
}
Then just looping through the products List like so:
@foreach(var item in productsList)
{
var nameInput = item.GetPropertyValue<string>("productName",true);
var name = !string.IsNullOrWhiteSpace(nameInput) ? nameInput : item.Name;
Price price = TC.GetPrice(storeId,item.Id.ToString(CultureInfo.InvariantCulture));
var image = item.GetPropertyValue<IPublishedContent>("image");
<a class="item" href="@item.Url">
<img src="@image.Url"/>
<h3>@name</h3>
<p>@price.WithVatFormatted</p>
<span>View More</span>
</a>
}
Does anyone know of a cleaner way to do this? At the moment I'm manually creating a list and adding to it. This, although it works, is not performance efficient!
Stock Level
Hi,
I'm trying to only show products that are in stock on my website.
Currently I have this:
I have tried changing the allProducts to the following but then nothing is returned. I have also tried manually creating a list and adding only products that have a stock level over 0 to the list but again nothing is returned.
I have tried:
My best guess is that stock isnt returning an int. The data type is Tea Commerce: Stock management.
Any help as its driving me round the bend!
Thanks, Lewis
Hi Lewis,
The stock is not stored on the Umbraco node, but in a seperate table in the database. That means you cannot get it like in your example.
You must use the TC API to get it instead: https://docs.teacommerce.net/v3.0.0/reference#getstock
/Rune
Hi Rune,
Is there a default way of only showing products that are in stock? IE with a stock level of >= 1?
Thanks, Lewis
I have ended up with the following:
Then just looping through the products List like so:
Does anyone know of a cleaner way to do this? At the moment I'm manually creating a list and adding to it. This, although it works, is not performance efficient!
Thanks, Lewis
Yes. That's just about how I would do it. Remember there's another overload of the method if you need to check variant stock.
Could you elaborate on what you mean please Rune? :)
Yes, and I just found another improvement to make.
You are using this overload: decimal? GetStock( long storeId, string productIdentifier )
You should actually be using this one: decimal? GetStock
Anyways, to get the variant stock you would use this overload: decimal? GetStock
The variant is found using the TC methods for that: https://docs.teacommerce.net/v3.0.0/reference#getvariants
Hope this sheds some more light on the situation.
is working on a reply...