how to get parent category

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
15 years ago
how to get parent category

e.g. Category1/Category2/Products

i want to get Category1's identification and other things
15 years ago
You can check the parent category id of "Category2" in order to retrieve "Category1". You can loop through each category hierarchically determining if it has a parent category and therefore determining the top level category.
15 years ago
i've done like this:

Category category = null;
Product product = ProductManager.GetByProductID(productVariant.ProductID);
ProductCategoryCollection existingProductCategoryCollection = product.ProductCategories;


if (existingProductCategoryCollection != null)
    {
    foreach (ProductCategory pcat in existingProductCategoryCollection)
    {
        if (pcat.Category.ParentCategory != null)
        {
            if (pcat.Category.ParentCategory.ParentCategoryID != 0)
                category = CategoryManager.GetByCategoryID(pcat.Category.ParentCategory.ParentCategoryID);
            else
                category = CategoryManager.GetByCategoryID(pcat.Category.ParentCategory.CategoryID);
        }
        if (category != null && category.Name.Equals("SAP Live Access"))
        {
            SapOptions.Visible = true;
        }
        else
        {
            SapOptions.Visible = false;
        }
    }
}

this is working fine!
12 years ago
Little problem.

I have Product 1 set up to appear in Category X and Category Y.

Customer has ordered Product 1 and paid for it. When I look at the order, how can I determine what category the customer ordered Product 1 from, for Management Information purposes?

Regards,
Si
12 years ago
You might be able to look at Google Analytics if you know the customer's IP address.  I don't think there is anything in the DB that indicates what Category they were looking at when they clicked Add to Cart.
12 years ago
Thanks for replying, do you happen to know if there is a programmatic way of doing this?

I am currently getting from the OrderProductVariant table (in the DB) and using the field ProductVariantId getting to the ProductVariant table (for that order to see what product was ordered) which I=is fine and working great. However from there, I can only use the ProductId field on that table to get to the say the Product_Category_Mapping table but then I end up with all categories in which that product appears.

Ordinarily this wouldn't be a product if each product could only be added to ONE category. However nopCommerce does not enforce this and allows a product to appear in many categories at once. So from the order it doesn't look possible to get the category of the product you ordered.

To me, the CategoryId should have been placed on the ProductVariant table along with the already present ProductId field, then it would be possible to obtain the category in which that product appeared when the user selected it.

Without that, information has been lost :(

Do you see where I am coming from?

Regards,
Si
12 years ago
Yes but I think more people wanted the ability to list a product in multiple categories rather than determining what category a product was in when it was ordered.  

The multiple categories feature was a goal, not an oversight.

You could modify the source and add a CategoryId to the ShoppingCartItem and OrderProductVariant tables and then fill it when someone clicks Add to Cart.
12 years ago
Hi Andy,

Yes I appreciate what you are saying and having reread my post, I did make it sound like it was an oversight rather than a goal but this was unintentional on my part as I am using, and do *need* the ability to have a product in multiple categories.

I think I will perhaps go with your suggestion to modify the code and update the entities.

Thanks for corresponding on this.

Si
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.