Copied to clipboard

Flag this post as spam?

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


  • Daniel Nutu 9 posts 30 karma points
    Mar 23, 2015 @ 12:35
    Daniel Nutu
    0

    Product in multiple Categories

    Hi,

    First of all congrats for this great piece software.

    Second, how can I add a product to multiple categories? or what structure should I have to support a scenario like this, because the Bazar example has a product per category.

    The structure I think is best for this scenario looks like:

    • Home - root
      • About
      • Contact
      • Cart
    • Categories - no url
      • Category 1
        • Sub category 1
      • Category 2
    • Products - no url
      • Product 1
      • Product 2
    But how do I make this happen in umbraco?
    Regards,
    Daniel
  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Mar 23, 2015 @ 18:07
    Rusty Swayne
    0

    So that you don't wind up with duplicate content penalties, putting all of the products under a single node and then either associate the product with the categories using an MNTP or something.

    How many products do you see yourself dealing with?

  • Daniel Nutu 9 posts 30 karma points
    Mar 24, 2015 @ 08:57
    Daniel Nutu
    0

    I have 200+ products with different options (color, perfume, packing ...).

    The issue I'm having is how I associate the product with categories? Do I use a multinode content picker?  My problem is not with Merchello (I think), but with Umbraco: how do I query products that have the current cateogry associated. The multinode picker returns content ids separated by a comma.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 24, 2015 @ 16:05
    Andy Butland
    0

    If I've understood right you have a category document type with a picker for products on it, and so you can split those comma seperated Ids and turn them into full IPublishedContent instances using Umbraco.TypedContent(id).

    Or, probably better, install Umbraco Core Property Value Converters with which you'll find that you can get back an IEnumerable<IPublishedContent> directly.

    Hope that helps

    Andy

  • Daniel Nutu 9 posts 30 karma points
    Mar 24, 2015 @ 16:12
    Daniel Nutu
    0

    It's actually the other way around. The product has a category picker. So when I display a category i should get all the products that have this category assigned. But your solution sounds a lot easier.

    Thanks!

  • Tom Steer 161 posts 596 karma points
    Mar 24, 2015 @ 19:03
    Tom Steer
    1

    Hey Daniel, I've done this before using examine to query the products by category. below is the two snippets of code if it helps :)

    Querying:

    public IEnumerable<Product> GetProductsByCategory(IPublishedContent category)
        {
            var query = ExamineManager.Instance.CreateSearchCriteria()
                    .NodeTypeAlias(AppConstants.DocTypeProduct)
                    .And()
                    .Field(AppConstants.PropProductCategoriesSearchable, category.Id.ToString(CultureInfo.InvariantCulture))
                    .Compile();
            return Umbraco.TypedSearch(query).Select(p => new Product(p));
        }
    

    OnGatheringNodeData Event to make the MNTP searchable:

     if (e.Fields.ContainsKey(AppConstants.PropProductCategories))
            {
                var categories = e.Fields[AppConstants.PropProductCategories];
                categories = categories.Replace(",", " ");
                e.Fields.Add(AppConstants.PropProductCategoriesSearchable, categories);
            }
    

    Hope this helps.

    Cheers,

    Tom

  • Daniel Nutu 9 posts 30 karma points
    Mar 30, 2015 @ 10:45
    Daniel Nutu
    0

    Hi all,

    Thanks for all your help.

    After some digging I managed to get the products assigned to a category like this:

    var productContainerId = 1063; // the 'Products' parent node
    var categoryId = page.Id.ToString(); // the current 'Category' node Id
    var products = Umbraco.TypedContent(productContainerId).Children.Where(p => p.GetPropertyValue("category").Contains(categoryId)); // get all children of the 'Products' node where the MNTP property 'category' contains the 'Category' node id

    What I don't know is how will this perform in a high traffic website. But until then let's hope it does well.

    Tom's Examine implementation did not work and I don't know why and I haven't tried to know why. I've impemented the 'GatheringNodeData' event in a derived class from 'ApplicationBase', but the query didn't return rows.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 30, 2015 @ 11:04
    Andy Butland
    0

    Should be OK - everything you are doing looks to be within the Umbraco content cache, you aren''t hitting the database.

    Just to note though, it's possible you'd find your queries is returing some false positives.  If say you were looking for a category with Id of 1000, it would match one against category 10000 (given it's a string match).  Quite unlikely and you'd need a lot of nodes of course(!), but might be safer to do p.GetPropertyValue("category").Split(',').Contains(categoryId); You may also need to check for null too, not sure (if an empty string is returned with no categories selected, might not be needed).

    Cheers

    Andy


  • Daniel Nutu 9 posts 30 karma points
    Mar 30, 2015 @ 16:24
    Daniel Nutu
    0

    Thanks, but apparently the 

    p.GetPropertyValue("category").Split(',').Contains(categoryId)

    gives an error. 

    Delegate 'System.Func' does not take 1 arguments

    Eighter I forgot how to use 'Where' or there's something i'm missing. If I omit the 'Split' method then all works.

    Later edit:

    And I spoke to soon... 

    Don't know why but

    var categoryId = page.Id.ToString();

    is not the same as

    string categoryId = page.Id.ToString();

     

Please Sign in or register to post replies

Write your reply to:

Draft