Try our conversational search powered by Generative AI!

Get category nodes of a specific type

Vote:
 

Hi,

I have a catalog which is structured like this:

Root
  Catalog
     Category_A
       products..
         variants..
      Category_B
        products..
          variants..

Where Category_A and Category_B is two created classes

    [CatalogContentType(DisplayName = "CategoryA", GUID = "ab4b7e72-e55e-43eb-a282-9b286e520ec5", Description = "Category for ...", MetaClassName = "CategoryA")]
    public class CategoryA : NodeContent
    {

    }

And of course Categoty_B is a public class CategoryB : NodeContent....

Now I want to find all products/variants beloning to only Category_A.
And I do not want to do it using the ID - Because we are in the development stage and it changes from time to time.

My question is: Can I get the content of Category_A using its "type"?? So something along the lines of: through the root link, find all categories of type CategoryA and return these, so that I can search it for products and variants?

#143901
Feb 03, 2016 9:54
Vote:
 

Hi,

Will you have multiple, or just one Category_A node?

This sounds like a perfect place for FindCommerce - its powerful features can solve this with ease.

If using FindCommerce (and then Find in general) is not an option for you, then consider using the Code to find it.

in the worst case IContentLoader/IContentRepository.GetChildren/GetDescendents can be used.

/Q

#143902
Feb 03, 2016 10:19
Vote:
 

Hi,

We will only have one Category_A node.

Right now we will not be using Find.

When you write "then consider using the Code to find it" what "Code" are you referring to?

#143903
Feb 03, 2016 10:23
Vote:
 

For every NodeContent there is a Code property which is unique and is supposed to be unchanged. The ContentReference might change as it depends on the primary key id in the table, but Code is not.

(Well technically code can be changed but it is usually not the case)

/Q

#143904
Feb 03, 2016 10:27
Vote:
 

I see..

Well we want to avoid any hard coded strings, so Code is not the best option for us.

I tried your sugestion with the IContentLoader / Get children:

var categoryA = ContentLoader.GetChildren<CategoryA>(ReferenceConverter.GetRootLink()).FirstOrDefault();

But I can't seem to go from this (which is just a category) to the actual products/variants.

Do you have any suggestions for that?

#143906
Feb 03, 2016 10:40
Vote:
 
#143908
Feb 03, 2016 10:55
Vote:
 

Honestly I don't understand why it does work for you :).

GetChildren of the Catalog Root Link should return the Catalog(s), not the NodeContent. Yours categoryA should be null.

It should be something like this:

var catalogs = ContentLoader.GetChildren<CatalogContent>(ReferenceConverter.GetRootLink());
foreach (var catalog in catalog)
{
var categoryA = ContentLoader.GetChildren<CategoryA>(catalog.ContentLink).FirstOrDefault();
if (categoryA != null)
{
    var variants = ContentLoader.GetChildren<Variant>(categoryA.ContentLink);
}
}

If you have only one catalog, you can use First() instead of foreach.

/Q

#143916
Feb 03, 2016 12:15
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.