Adding tags to a product

To add tags to a product, you must perform the following:

  1. Get instances of the following managers:
    • Catalog manager
    • Taxonomy manager
  2. Get the product.
    Get an instance of the specified product.
  3. Get the taxonomy.
    Get an instance of the Tags taxonomy.
  4. Get the tag.
    Get an instance of the taxon that represents the specified tag.
  5. Add the tag to the product.
    To add the tag, call the AddTaxa method of the OrganizerBase object of the product and pass the “Tags” value and the taxon’s ID as arguments. The OrganizerBase object is stored in the Organizer property of the Product class.
  6. Save the changes.
    Save the changes to the catalog manager.

Here is a code example:

public static void AddTagsToProduct(Guid productId, string tagTitle)
{
    CatalogManager catalogManager = CatalogManager.GetManager();
    TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
 
    // Get the product
    Product product = catalogManager.GetProduct(productId);
 
    if (product == null)
    {
        return;     // Product does not exist
    }
 
    // Get the taxonomy for "Tags"
    Taxonomy tagTaxonomy = taxonomyManager.GetTaxonomies<Taxonomy>().Where(t => t.Name == "Tags").SingleOrDefault();
 
    if (tagTaxonomy == null)
    {
        return;     // The "Tags" taxonomy does not exist
    }
 
    //Get the tag
    Taxon taxon = tagTaxonomy.Taxa.Where(t => t.Title == tagTitle).SingleOrDefault();
 
    if (taxon == null)
    {
        return;     //Taxon does not exist
    }
 
    product.Organizer.AddTaxa("Tags", taxon.Id);
    catalogManager.SaveChanges();
}

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK