Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lewis Smith 208 posts 617 karma points c-trib
    Oct 10, 2017 @ 12:45
    Lewis Smith
    0

    Hi,

    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.

    Any help as its driving me round the bend!

    Thanks, Lewis

  • Rune Grønkjær 1371 posts 3102 karma points
    Oct 11, 2017 @ 09:05
    Rune Grønkjær
    100

    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

  • Lewis Smith 208 posts 617 karma points c-trib
    Oct 11, 2017 @ 10:59
    Lewis Smith
    0

    Hi Rune,

    Is there a default way of only showing products that are in stock? IE with a stock level of >= 1?

    Thanks, Lewis

  • Lewis Smith 208 posts 617 karma points c-trib
    Oct 11, 2017 @ 11:25
    Lewis Smith
    0

    I have ended up with the following:

    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!

    Thanks, Lewis

  • Rune Grønkjær 1371 posts 3102 karma points
    Oct 11, 2017 @ 12:25
    Rune Grønkjær
    0

    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.

  • Lewis Smith 208 posts 617 karma points c-trib
    Oct 11, 2017 @ 15:21
    Lewis Smith
    0

    Could you elaborate on what you mean please Rune? :)

  • Rune Grønkjær 1371 posts 3102 karma points
    Oct 12, 2017 @ 05:58
    Rune Grønkjær
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft