0

I'm getting the simple products of a configurable with the following code:-

if($_product->getTypeId() == "configurable"):
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
    $simple_collection = $conf->getUsedProductCollection()
        ->addAttributeToSelect('*')
        ->addFilterByRequiredOptions();

endif;

And then with this collection, I'm conditionally displaying a size chart which works perfectly (foreach($simple_collection as $simple_product)) however I need to determine what table headers to use in the chart by another condition (and currently, I can only think of doing this by category unfortunately).

Rather than just loading the above collection by category ID though, is there any way I can build into the existing collection a way to determine what the category ID is of the configurable product and then use an if statement as necessary in the chart to display various data based on the category ID?

I know how to load the collection from a category ID but unsure if it is possible to determine the current category from getUsedProductCollection...

  • This is not an answer but it could be an idea. You should not rely in categories to display information about the product. one product can belong to one or more categories (or none). I would use the attribute set as reference. You already have this handy directly on the configurable product (or simple ones because all simple products must belong to the same attribute set). This way you can make your own crud module to determine what headers to show depending on the attribute set. – Marius Feb 4 '15 at 9:20
  • I knew someone would reply with that :) - I'm aware of the issue you describe @Marius but I am not intending to display data based on the category - I instead want to say 'if product exists in category id 5, display this'. I thought of attribute sets first but it won't work as my attribute sets are Default, Ladies, Men, Kids... And of course, Ladies might contain products dresses, tops, shirts, trousers, shoes (etc). If that makes sense. I would just use second level categories to determine the type of product. – zigojacko Feb 4 '15 at 9:23
  • I've managed to get what I want by adding a category collection as well by the way. I just don't know if there is a better way. – zigojacko Feb 4 '15 at 9:45
  • In this case..There is no way to get the category id using getUsedProductCollection. You have to get the category ids from the configurable product $_product->getCategoryIds() and then go up the tree for each id you get until you get the level 2 that you need. Here is a way to go up the tree without loading the full category collection. But just for the record, this approach seams prone to errors from my point of view because you have to hard code ids. it would be better to have a product attribute linked to the headers in my opinion. – Marius Feb 4 '15 at 9:47
  • I see where you're coming from yeah, initially, there was no requirement to use product type for attribute sets (altering all the catalog now would be problematic). I'm literally only going to display table headers (columns) differently based on which category ID the product exists in else display nothing. Fingers crossed... Thanks for your help Marius :) – zigojacko Feb 4 '15 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.