Try our conversational search powered by Generative AI!

How to get and update Quantity of product/variant?

Vote:
 

Hi,

we're using the newest commerce 12.

For a customer 'Im about to make a admin tool to upload an excel-sheet with variant id's and data like stock count/quantity, so that the catalog/variants quantity should be updated to this number.

Any ideas on how to get to a variant/prodycts quantity property, hence update it?

Is it through [ContentReference] InventoryReference when I'm traversing VariantContent objects?

If yes, any hints on the best way?

thx

J

#194342
Jun 19, 2018 11:59
Vote:
 

IInventoryService is what you need, you can get the stocks of a variant by its code, update the data, then save it back 

https://world.episerver.com/documentation/Class-library/?documentId=commerce/8/95A5598E 

#194343
Edited, Jun 19, 2018 12:09
Vote:
 

Hi Quan,

Thx, could you elaborate a little? ;)

Perhaps with example or reference to a concrete example.
I would much appreciate this.

#194345
Jun 19, 2018 12:13
Vote:
 

Here you go: https://world.episerver.com/documentation/Items/Developers-Guide/Episerver-Commerce/9/Warehouses-and-inventories/Warehouses-and-inventories-examples/ 

#194346
Jun 19, 2018 12:15
Vote:
 

;) Thx, I've just googlet that...

#194347
Jun 19, 2018 12:17
Vote:
 

Hi again,
When I'm trying to  update values in the InventoryRecord, it isn't stored in the database.

What am I doing wrong?

public someClass
{
...

private bool UpdateVariant(VariantData vd)
        {
            if(vd == null)
            {
                return false;
            }
            try
            {

                IEnumerable<InventoryRecord> irs = Helpers.Utilities.QueryInventoriesByEntryCode(vd.Code);
                var editableVariant = irs.First();
                if (editableVariant != null && vd.Quantity > -1)
                {
                    var tmpV = editableVariant.CreateWritableClone();
                    tmpV.PreorderAvailableQuantity = vd.Quantity;
                    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();

                    UpdateInventoryRecord(tmpV);

                    return true;
                }
            }
            catch (Exception)
            {

            }
            return false;
        }
        private void UpdateInventoryRecord(InventoryRecord record)
        {
            var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
            inventoryService.Update(new[] { record });
        }
...

}
#194411
Jun 20, 2018 16:11
Vote:
 

What do you mean "it isn't stored in the database." ?

#194412
Jun 20, 2018 16:41
Vote:
 

the changes isn't saved to the inventory

#194413
Jun 20, 2018 16:50
Vote:
 

Did you make sure inventoryService.Update was actually called, and not the empty catch that swallows any exception? 

#194414
Jun 20, 2018 16:51
Vote:
 

No there's no exceptions, it runs the update method on all the WritableClones of the InventoryRecords
Should I perhaps use inventoryservice Save method instead?
Or is it overall a wrong approach. Should I update the 'containing' Variant also or what?
I'm in the dark here...

#194438
Jun 21, 2018 10:31
Vote:
 

... When I'm debugging I can see that the InventoryRecord is Readonly (InventoryRecord.IsReadOnly == True).
That might be the issue ;) ...
I'll repport back with a fix/status

#194439
Jun 21, 2018 10:38
Vote:
 

OMG!
I'm sorry for wasting your time...
I tried to set the WRONG property...

the right way is to set InventoryRecord.PurchaseAvailableQuantity

...

tmpV.PurchaseAvailableQuantity = (decimal)vd.Quantity;

...

And with use of update method everything works - THX anyway

#194442
Jun 21, 2018 11:18
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.