Developer forum

Forum » Ecommerce - Standard features » Get Default Unit Name

Get Default Unit Name

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I need to get the Name of the default Unit set on a product. Not the ones set in the "Stock" section, the one set in the Default Unit.

I can get the DefaultUnitID using the standard tag but I don;t seem to be able to get the name of it.

I have looked in the API and it looks like there are a few methods to get StockUnit by GetStockUnitById is looking for an integer as ID while my Unit IDs are strings.

I suppose there should be another method to get the Unit based on the DefaultUnitID, right?

Thank you,
Adrian


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Adrian,

 

I went throught the same struggle recently. AFAIK there's no way to get it from tags. This is how I did it

var unitIdName = string.Empty;
using (var command = Database.CreateConnection().CreateCommand())
{
    command.CommandText = "SELECT TOP 1 VariantOptionId, VariantOptionName FROM EcomVariantsOptions WHERE VariantOptionLanguageId = 'LANG1' AND VariantOptionId = @UnitId";
    command.Parameters.Add(new System.Data.SqlClient.SqlParameter
    {
        ParameterName = "UnitId",
        SqlDbType = System.Data.SqlDbType.NVarChar,
        Value = GetString("Ecom:Product.DefaultUnitID")
    });
    if(command.ExecuteScalar() != null)
    {
        using(var reader = command.ExecuteReader())
        {
            while(reader.Read())
            {
                unitIdName = reader["VariantOptionName"].ToString();
            }
        }
    }
}

 

If you happen to find a more effeccient way let me know.

 

Best Regards,

Nuno Aguiar

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply
This post has been marked as an answer

Hi Nuno,

Thank you for the suggestion.

I ended up using the VariantOptionService:

string defaultUnitID = GetString("Ecom:Product.DefaultUnitID");
var variantService = new Dynamicweb.Ecommerce.VariantOptionService();
string productLanguageID = GetString("Ecom:Product.LanguageID");
var dafaultUnit = variantService.GetVariantOption(defaultUnitID,productLanguageID);
string defaultUnitName = dafaultUnit.Name;
Votes for this answer: 1
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Adrian,

 

Perfect, I changed mine to use Services too. Will take benefit of the cache now :)

 

You must be logged in to post in the forum