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?
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.
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); }
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);
}
}
}
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
Hi Trine,
Something like this should do the trick
Matt
Hi Matt
Thanks for the advice. I was hoping for an xslt solution - is it impossible to do with xslt alone?
/Trine
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:
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
Hi
Thanks Matt. We will go ahead and develop a XSLT exstention.
Best regards
Emil
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
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.
Thanks very much,
Jeavon
Thanks Jeavon.
Feri
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
is working on a reply...