Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Trine 2 posts 22 karma points
    Jul 30, 2010 @ 13:03
    Trine
    0

    How to retrieve a product's categories

    Hi,

    I have a page in which I show a single product. I'd like to display all the categories which that particular product is a part of. How do I retrieve that information?

    /Trine

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Jul 30, 2010 @ 13:29
    Matt Brailsford
    0

    Hi Trine,

    Something like this should do the trick

     product.CategoryProductRelations.Join(Category.All(),
                    x => x.CategoryId,
                    y => y.CategoryId,
                    (x, y) => y).Where(c => c.Deleted = false).ToList();

    Matt

  • Trine 2 posts 22 karma points
    Aug 03, 2010 @ 14:04
    Trine
    0

    Hi Matt

    Thanks for the advice. I was hoping for an xslt solution - is it impossible to do with xslt alone?

     

    /Trine

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Aug 03, 2010 @ 14:20
    Matt Brailsford
    0

    Hmmm, I'm not sure it's possibly using the standard XSLT extension methods that come with UCommerce. The only methods that are relevant are:

    GetCategory(string catalogName, string categoryName);
    GetCategory(string catalogName, string categoryName, bool includeChildCategories);
    GetRootCategories(string catalogName);

    None of which take a product as the parameter, so the best you could do, is get all categories, and loop through them and save the ones that exist on the product (thought I'm not sure how you'd test it)

    Best I can suggest is to create your own XSLT extension library, and put a .NET helper method similar to the above into it and use that.

    Matt

  • Emil Rasmussen 67 posts 91 karma points
    Aug 09, 2010 @ 11:33
    Emil Rasmussen
    0

    Hi

    Thanks Matt. We will go ahead and develop a XSLT exstention.

    Best regards
    Emil

  • feri 5 posts 25 karma points
    Mar 07, 2011 @ 10:46
    feri
    0

    Hi,

     

    I've created a .Net class to help me get the product category, product parent category and product catalog.

     

    I hopw this is useful for someone out there. And a good tutorial on how to create an xslt extension on Umbraco can be found here

    http://en.wikibooks.org/wiki/Umbraco/Create_xslt_exstension_like_umbraco.Library_in_C

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using UCommerce.Entities;
    using UCommerce.Runtime;
    using SubSonic.Linq;


    namespace xsltHelperUCommerce
    {
    public class ProductHelper
    {
    public static string getCategory(string _sku)
    {
    var query = from relation in CategoryProductRelation.All()
    join product in Product.All() on relation.ProductId equals product.ProductId
    join category in Category.All() on relation.CategoryId equals category.CategoryId
    where product.Sku == _sku
    select relation;
    return (query.Single().Categories.Single().Name);


    }

    public static string getParentCategory(string _sku)
    {
    var query = from relation in CategoryProductRelation.All()
    join product in Product.All() on relation.ProductId equals product.ProductId
    join category in Category.All() on relation.CategoryId equals category.CategoryId
    where product.Sku == _sku
    select relation;
    try
    {
    int parentCatID = query.Single().Categories.Single().ParentCategoryId.Value;
    if (parentCatID != 0)
    {
    var query2 = from category in Category.All()
    where category.CategoryId == parentCatID
    select category;
    return (query2.Single().Name);
    }
    else
    return ("");
    }
    catch (Exception ex)
    {
    return ("");
    }

    }

    public static string getCatalog(string _sku)
    {
    var query = from relation in CategoryProductRelation.All()
    join product in Product.All() on relation.ProductId equals product.ProductId
    join category in Category.All() on relation.CategoryId equals category.CategoryId
    where product.Sku == _sku
    select relation;
    return (query.Single().Categories.Single().ProductCatalogs.Single().Name);
    }

    }
    }
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Mar 28, 2011 @ 15:02
    Jeavon Leopold
    0

    Hi Feri,

    This is extreamly useful! Thanks very much.

    I updated your snippit so that there is no need to register the extension in xsltextensions.config.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using umbraco;
    using umbraco.presentation.umbracobase;
    using UCommerce.Entities;
    using UCommerce.Runtime;
    using SubSonic.Linq;
    
    namespace CommerceExt
    {
        [XsltExtension]
        public class ProductHelper
        {
            public static string getCategory(string _sku)
            {            
                var query = from relation in CategoryProductRelation.All()
                            join product in Product.All() on relation.ProductId equals product.ProductId
                            join category in Category.All() on relation.CategoryId equals category.CategoryId
                            where product.Sku == _sku
                            select relation;
                return (query.Single().Categories.Single().Name);
    
    
            }
            public static string getParentCategory(string _sku)
            {
                var query = from relation in CategoryProductRelation.All()
                            join product in Product.All() on relation.ProductId equals product.ProductId
                            join category in Category.All() on relation.CategoryId equals category.CategoryId
                            where product.Sku == _sku
                            select relation;
                try
                {
                    int parentCatID = query.Single().Categories.Single().ParentCategoryId.Value;
                    if (parentCatID != 0)
                    {
                        var query2 = from category in Category.All()
                                     where category.CategoryId == parentCatID
                                     select category;
                        return (query2.Single().Name);
                    }
                    else
                        return ("");
                }
                catch (Exception ex)
                {
                    return ("");
                }
            }
            public static string getCatalog(string _sku)
            {
                var query = from relation in CategoryProductRelation.All()
                            join product in Product.All() on relation.ProductId equals product.ProductId
                            join category in Category.All() on relation.CategoryId equals category.CategoryId
                            where product.Sku == _sku
                            select relation;
                return (query.Single().Categories.Single().ProductCatalogs.Single().Name);
            }
    
        }
    }

    Thanks very much,

    Jeavon

  • feri 5 posts 25 karma points
    Mar 29, 2011 @ 04:24
    feri
    0

    Thanks Jeavon.

    Feri

     

  • mithun 42 posts 63 karma points
    Jan 09, 2013 @ 07:57
    mithun
    0

    Hi ,

    I think  lambda expression is better 

     public static string getCategory(string _sku)

            {

                int productId = Product.Find(x => x.Sku == _sku).Single().ProductId;

                int categoryId = CategoryProductRelation.Find(x => x.ProductId == productId).Single().CategoryId;

                string categoryName = Category.Find(x => x.CategoryId == categoryId).Single().Name;

                return categoryName;

            }

    regards

    mithun

     

     
     
Please Sign in or register to post replies

Write your reply to:

Draft