Developer forum

Forum » Ecommerce - Standard features » PrimaryGroup name on orderline

PrimaryGroup name on orderline

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

It looks like I can the PrimaryGroupId for a product in an orderline, but is it not posible to get the PrimaryGroupName (The name of the primary group the prouct is in) in an orderline?

/Hans


Replies

 
Nicolai Pedersen
Reply

Hi Hans

No, that is not available. But you can use the API to find it:

Dynamicweb.Ecommerce.Products.GroupService.GetGroup(PrimaryGroupId).Name

https://doc.dynamicweb.com/api/html/8b284438-8c93-4b54-bef4-f1983e47a5bb.htm

BR Nicolai

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

HI again

Sorry for the late followup, but I still can´t get it to work. I am probably doing it completely wrong, but hope you can help me.

 

My code is:

 @foreach (LoopItem i in GetLoop("OrderLines")){

 <text>         
@{
   string productid = @i.GetString("Ecom:Product.PrimaryGroupID");    
   string group = Dynamicweb.Ecommerce.Products.GroupService.GetGroup(@productid).Name;

}
</text>

}

 

But i get the message:

Line 115: An object reference is required for the non-static field, method, or property 'Dynamicweb.Ecommerce.Products.GroupService.GetGroup(string)'

 

What am I doing wrong?

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Hans,

 

You probably don't have a PrimaryGroupId set. Try to use PrimaryOrFirstGroupId https://doc.dynamicweb.com/template-tags/ecommerce/product-catalog/product#Ecom:Product.PrimaryOrFirstGroupID instead.

 

And you should still check if it's null in case you run into a product that is not associated with any groups.

 

It's also confusing to call productId to a variable that holds a groupId :P

 

Best Regards,

Nuno Aguiar

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi Nuno

Thanks for the reply. I´ve tried your suggestion, but still doesn´t work

My code now looks like this

 @foreach (LoopItem i in GetLoop("OrderLines")){

<text>

    @{
   string groupid = @i.GetString("Ecom:Product.PrimaryOrFirstGroupID");  
           
           <text>
           @if(!String.IsNullOrEmpty(@i.GetString("Ecom:Product.PrimaryOrFirstGroupID"))){
           string groupname = Dynamicweb.Ecommerce.Products.GroupService.GetGroup(@groupid).Name;
           }
                </text>

</text>

}

 

PS: I changed the name from product to group :) And I do get the value like eg GROUP32 in Ecom:Product.PrimaryOrFirstGroupID

But I get an error message from the:

string groupname = Dynamicweb.Ecommerce.Products.GroupService.GetGroup(@groupid).Name;

 

What could be wrong?

 

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Stil have not got this to work

I am attaching the entire template here, and hopefully someone can help?

Would really apprecieate it

/Hans

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Hans,

 

Looks like the problem might be in the API method. That can change depending on the DW version. If you're using an older version this might be the way to go:

string groupname = Dynamicweb.Ecommerce.Products.GetGroupById(@groupid).Name;

 

You should not be calling the group name directly through, and check if it's not null prior to getting the Name property.

 

Best Regards,

Nuno Aguiar

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi Nuno

Thanx for your Reply. Am running a version 9.4.14 - so it´s not an old solution.

Your suggestion didn´t work unfortunately :/

 

Sorry for my little knowledge, but what do you mean when you say "You should not be calling the group name directly through" ? Please ellaborate

Shouldn´t this do the trick of checking if it is null?

  @if(!String.IsNullOrEmpty(@i.GetString("Ecom:Product.PrimaryOrFirstGroupID"))){
           string groupname = Dynamicweb.Ecommerce.Products.GroupService.GetGroup(@groupid).Name;
           }

 

All products are in a group, so it should not be null.

 

I attached the entire template in my post earlier. Hopefully someone can help solve this?

 

Would really appreciate it.

 

/Hans

 

 

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

Hi Hans,

Try this...

@foreach (var line in GetLoop("OrderLines"))
{
    var groupId = line.GetString("Ecom:Product.PrimaryGroupID");
 
    if (!string.IsNullOrEmpty(groupId))
    {
        var group = Dynamicweb.Ecommerce.Services.ProductGroups.GetGroup(groupId);
        if (group != null)
        {
            var groupName = group.Name;
            // TODO: do something with the group name
        }
    }
}

 

Best regards,
Morten

Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Perfect Morten, that works :)

Thank you very mutch

 

 

You must be logged in to post in the forum