Developer forum

Forum » Development » Add units
Per Ljung
Reply
Hi!

I want to add my own units to a product, use my own unit ids to be able to import them from the erp system.


I have tried this:

 Dynamicweb.eCommerce.Stocks.StockUnit unit = new Dynamicweb.eCommerce.Stocks.StockUnit("");

unit.UnitID = dwProduct.Unit.UnitCode;

unit.ProductID = product.ID;
unit.VariantID = "";

product.Units.Add(unit);

unit.Save(unit.ProductID, unit.VariantID, unit.UnitID);


product.DefaultUnitID = unit.UnitID;
But nothing is added. The variable dwProducts is a class of my own.

Best regards,
Per

(By the way, this question has nothing to do with earlier questions from me about units, it's another solution and customer).

Replies

 
Kim Sørensen
Reply

It looks like you have forgot to save your product :-)

 
Per Ljung
Reply


Hi!
 

No, I had saved the product. I just forgot to include it in the forum post because that function call is a little later in the code.

But I wonder, do you first have to create the unit in the Management Center and then add it to the unit? I have not done this.

The problem is that I can't connect the value "st" with the id VO1, I want to use "st" as the id instead av "VO1". Or is it possible to search and add units to a product by the name of the unit?

Best regards,
Per
 
Morten Snedker
Reply
This post has been marked as an answer
 Hi Per,

If you already have your units set up, and you want to apply a unit to a product by the unit name, this is a working example:

       private void SetUnit()
        {
            Dynamicweb.eCommerce.Stocks.StockUnit unit = new Dynamicweb.eCommerce.Stocks.StockUnit();
            unit.ProductID = "PROD1";
            unit.VariantID = string.Empty;
            unit.StockQuantity = 10;
            unit.UnitID = GetUnitIDByName("stk");
            unit.Save(unit.ProductID, unit.VariantID, unit.UnitID);
        }

        private string GetUnitIDByName(string unitName)
        {
            return Base.ChkString(Database.ExecuteScalar(string.Format("SELECT TOP 1 VariantOptionID FROM EcomVariantsOptions WHERE VariantOptionName='{0}'", unitName), string.Empty));
        }

Regards /Snedker
Votes for this answer: 0

 

You must be logged in to post in the forum