Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    Aug 21, 2018 @ 10:30
    Gordon Saxby
    0

    Get variant stock qty via Javascript

    I have products with 2 variants - size and colour. I have a dropdown for each on the product page. Each variant item (e.g. "size M, Blue") has it's own stock level configured in TC.

    I have got the first dropdown (size) to show the combined quantity per size (e.g. total of all size M for all colours).

    What I now want to do is show the stock level in the colour dropdown, once a size has been selected. Also, I want to disable any with a stock level of zero.

    How do I (or, can I?!) get the stock quantity of each colour for a specific size via Javascript?

  • Rune Grønkjær 1372 posts 3103 karma points
    Aug 21, 2018 @ 13:19
    Rune Grønkjær
    0

    Hi Gordon,

    So your stock is stored in the standard TC stock table?

    In the javascripts you have the https://docs.teacommerce.net/v3.0.0/reference#getstock method. So you could fetch them one at a time.

    You could also make your own controller that fetches a bunch of stocks at a time. Do that with a custom sql call to the database. That would be faster.

    /Rune

  • Gordon Saxby 1461 posts 1883 karma points
    Aug 21, 2018 @ 15:02
    Gordon Saxby
    0

    Yes, I assume so, it is using the built-in TC variants setup.

    GetStock needs a "productidentifier" - which is easy if it's the main product, but less easy if it's a variant.

    The code I have (I inherited) that populates the first dropdown using C# seems quite complicated, it doesn't seem like a good idea to replicate that in Javascript. Maybe it would be best to call my own controller / function. Also, this needs to be dynamic so that the values are always as up to date as possible.

  • Rune Grønkjær 1372 posts 3103 karma points
    Aug 22, 2018 @ 06:35
    Rune Grønkjær
    0

    The GetStock() needs a productidentifier yes. But note that the stock in the table is linked with a SKU if you want to fetch them yourself.

    The idea is that two different productidentifiers (products) with the same SKU will share their stock.

    Usually the productidentifier is the node id and in the case of the buildt in variant system the variants productidentifiers is the node id and a guid.

  • Gordon Saxby 1461 posts 1883 karma points
    Aug 22, 2018 @ 07:54
    Gordon Saxby
    0

    When a Size is selected, I need to update the quantities for each Colour variant, for that size. Is this something I can do via Javascript calls, or do I need to create my own controller / functions?

    I assume I would need to call GetVariants to get the variant GUID, but that does not mention Javascript - https://docs.teacommerce.net/reference#getvariants

  • Rune Grønkjær 1372 posts 3103 karma points
    Aug 22, 2018 @ 13:13
    Rune Grønkjær
    0

    You can do it with multiple getStock javascript calls. The GetVariants should provide you with id's (product identifiers) for each variant, which you can use.

    For speed optimization I would recommend you to get all of them in one call using your own SKU's. We currently don't have a way of getting multiple stocks in one call.

Please Sign in or register to post replies

Write your reply to:

Draft