Adding departments to a product

To add a department 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 Departments taxonomy.
  4. Get the department.
    Get an instance of the hierarchical taxon that represents the specified department. For more information, see Querying departments.
  5. Add the taxon to the product.
    To add the department, call the AddTaxa method of the OrganizerBase object of the product and pass the “Department” value and the department’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 AssignProductToDepartment(Guid productId, Guid departmentId)
{
    CatalogManager catalogManager = CatalogManager.GetManager();
    TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
 
    var product = catalogManager.GetProducts().Where(x => x.Id == productId).SingleOrDefault();
 
    if (product == null)
    {
        return;        // Product does not exist
    }
 
    HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(x => x.Name == "Departments").Single();
 
    HierarchicalTaxon department = departments.Taxa.Where(x => x.Id == departmentId).SingleOrDefault() as HierarchicalTaxon;
    if (department == null)
    {
        return;        // Department does not exist
    }
 
    if (product.Organizer.TaxonExists("Department", department.Id) == true)
    {
        return;        // Product already linked to department
    }
 
    product.Organizer.AddTaxa("Department", department.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