Developer forum

Forum » Ecommerce - Standard features » List all items in a group on details view

List all items in a group on details view

Martin Skov Nielsen
Reply

I need to list all other items in a group, when the users clicks on a product.

 

How do I achive this? (I'm using Razor)


Replies

 
Mikkel Ricky
Reply

This piece of code will give you a list of products in the same groups (or primary group) as the current product:

@using Dynamicweb.eCommerce.Products;

@{
    var product = Product.GetProductByID(GetString("Ecom:Product.ID"), GetString("Ecom:Product.LanguageID"), GetString("Ecom:Product.VariantID"));
    var otherProducts = new ProductCollection();
    if (product != null)
    {
        var groups = product.Groups;
        // Uncomment to use only primary group
        /*
        var primaryGroup = Group.GetGroupByID(product.PrimaryGroupID);
        if (primaryGroup != null)
        {
            groups = new GroupCollection() { primaryGroup };
        }
        //*/
        if (groups != null)
        {
            foreach (var group in groups)
            {
                otherProducts.AddRange(group.Products, false);
            }
            // Remove products with same ID as the current product
            var productsToRemove = new ProductCollection();
            foreach (var p in otherProducts)
            {
                if (p.ID == product.ID)
                {
                    productsToRemove.Add(p);
                }
            }
            otherProducts.Remove(productsToRemove);
        }
    }
}

Is this what you need?

 

 
Martin Skov Nielsen
Reply

Without having tested it - I'd say yes! This looks exactly like the code I need. I wish I knew where to find some documentation on the API :(

 
Mikkel Ricky
Reply

Feel free to use the API documentation that I use:

  1. http://developer.dynamicweb-cms.com/ > Documentation
  2. http://developer.dynamicweb-cms.com/documentation/getting-started.aspx > For Developers > API documentation > Dynamicweb 8
  3. http://developer.dynamicweb-cms.com/api8/
 
Martin Skov Nielsen
Reply

Ok - I see that there has been added pretty much documentation, since I last looked @ DW

 

However - I'm unable to located where or how to find properties on Groups... How do I extract a ProductGroupField?

 
Mikkel Ricky
Reply

You can get all product group fields from a group like shows in this example

if (product.Groups.Count > 0) {
    var group = product.Groups[0];
    <pre>
    @foreach (var fieldValue in group.ProductGroupFieldValues) {   
        @: @fieldValue.ProductGroupField.Name: @fieldValue.Value
    }
    </pre>
}

The product Group class API documentation can be found here: http://developer.dynamicweb-cms.com/api8/#Dynamicweb~Dynamicweb.eCommerce.Products.Group.html

 

 

 

You must be logged in to post in the forum