Duplicate the functionality of the Department Widget
Is it possible to duplicate the functionality of the Department Widget using the API. Specifically what I want to do, is select all departments that contain products, along with the count of products.
It can be done using SQL by joining the sf_taxa table to the sf_taxonomy_statistic table but this feels like a "hack"
Is there a better way to do this using the API ?
Hello,
Yes, it's possible to obtain the statistics for each Taxonomy. The TaxonomyStatistics object will contain lots of useful data that might help you achieve the desired functionality. You can get the MarkedItemsCount - this is the count of all content items that have been assigned a particular taxon. You can also get the TaxonId, which will allow you to get the specific Department, and form there you can even get all products under that department. I've prepared a small sample demonstrating the usage of the above discussed objects below, hope you find it useful:
public
void
GetTaxonomyStatsAndProductsInDepartment()
var productsManager = CatalogManager.GetManager();
var taxonomyManager = TaxonomyManager.GetManager();
//get the statistics for the usage of Departments taxonomy
var taxStat = taxonomyManager.GetStatistics()
.Where(tS => tS.TaxonomyId == TaxonomyManager.DepartmentsTaxonomyId);
foreach
(var stat
in
taxStat)
//Get the count of items under that Department
var itemsCount = stat.MarkedItemsCount;
//Get the Department ID
var departmentId = stat.TaxonId;
//Get all products for the Department
var productsInDepartment = productsManager.GetProducts().ToArray().Where(p => p.GetValue<TrackedList<Guid>>(
"Department"
).Contains(departmentId));