1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Sitefinity - Ecommerce - How to get all tags currently associated with any given product.

Sitefinity - Ecommerce - How to get all tags currently associated with any given product.

3 posts, 2 answered
  1. James
    James avatar
    15 posts
    Registered:
    02 Apr 2012
    03 Aug 2012
    Link to this post
    I have been searching for solutions or hints and can't seem to find any on this. I have a custom set of drop down menus on my master file. Those drop down menus assign a value to session variables that I want to use to filter results from default "Product List" widget. I have considered options to pass a query string like one of the telerik support technicians suggested. The only query string that I see that can filter products, do so on a SINGLE tag like this: http://localhost:60393/EC/the-store/-in-Tags/Tags/blue

    Although not sure there is a version of that querystring that can use a "OR" for example show me all products with tag1 OR tag2 OR tag3 attached to them.

    So, this all led me to research a way to manually databind product list with required tags in a repeater or something. But then I can't figure out how to filter tags. SOOOO......(drum-roll)

    My question is: What line of code can I use to return a list of tags already associated with any given Product.id? (In the ecommerce context)

    I figure, I would have to use these two managers below:
    CatalogManager cmanager = CatalogManager.GetManager();
    TaxonomyManager tmanager = TaxonomyManager.GetManager();

    Then I figure I can do something like the below snippet to retrieve all products.
    IQueryable<Product>  allProducts = cmanager.GetProducts();

    When I have all products I can foreach loop "allProducts" and input a product ID to retrieve all tags associated with that product ID. Is that possible?

    If anyone has anything that they think would benefit me, i would greatly appreciate the help!

    Thanks,
    James
  2. Randy Hodge
    Randy Hodge avatar
    96 posts
    Registered:
    03 Nov 2014
    06 Aug 2012
    Link to this post
    Hello, James.

    Yes it is possible and here is how:
    CatalogManager catalogManager = CatalogManager.GetManager();
    TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
     
    var products = catalogManager.GetProducts();
     
    // Get the taxonomy for "Tags"
    FlatTaxonomy tagTaxonomy = taxonomyManager.GetTaxonomies<FlatTaxonomy>().Where(t => t.Name == "Tags").SingleOrDefault();
     
    foreach (Product product in products)
    {
        // get IDs of all tags associated with this product
        var tagIds = product.GetValue<TrackedList<Guid>>("Tags");
     
        foreach (Guid tagId in tagIds)
        {
            // get the Tag taxon from its Id
            Taxon tag = tagTaxonomy.Taxa.Where(x => x.Id == tagId).SingleOrDefault();
     
            // your code to manipulate the tag goes here
        }
    }

    This will get a list of all the Ids of the Tags associated with a product and then get the tag Taxon if you need more info such as the Tag's title, etc.

    You will need to add these using statements:
    using Telerik.Sitefinity.Modules.Ecommerce.Catalog;
    using Telerik.Sitefinity.Taxonomies;
    using Telerik.Sitefinity.Taxonomies.Model;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.Ecommerce.Catalog.Model;
    using Telerik.OpenAccess;

    I hope this helps you. Please let us know if you have more questions.

    Regards,
    Randy Hodge
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  3. James
    James avatar
    15 posts
    Registered:
    02 Apr 2012
    06 Aug 2012
    Link to this post
    Randy,
    I was sooo close. I really appreciate your help! That is exactly what I needed!
    This tiny snippet of code will allow me to do many things with my Sitefinity website, so it is very much appreciated. Thanks again man!
    James
    Answered
3 posts, 2 answered