1

I am trying to write code for generating Google shopping feed in Magento 1.

I want to get categories assigned to a product which are subcategories of a specific category (Designers).

How do I do this?

I want to populate this in the brand tag in the XML and we do not have any manufacturers but store them in categories with a parent category called 'Designers'

  • Does your Designer category have only one level sub categories? – Sukumar Gorai Aug 9 '18 at 13:34
  • yes, the basically represent the name of the manufacturers. – Sushant Gurjar Aug 9 '18 at 13:48
  • Check my answer which may help you. – Sukumar Gorai Aug 9 '18 at 13:49
1

You can manage to work by following code:

$designer_cat_id = 'YOUR DESIGNER CATEGORY ID';
$brands = array();
$categoryIds = $product->getCategoryIds();
if ($categoryIds)
{
    $categoryCollection = Mage::getModel('catalog/category')->getCollection();
    $categoryCollection->addAttributeToFilter('parent_id',['eq'=>$designer_cat_id]);
    $categoryCollection->addIdFilter($categoryIds);
    $categoryCollection->addNameToResult();
    if ($categoryCollection->getSize() > 0)
    {
        foreach ($categoryCollection as $cat)
        {
            $cat_name = $cat->getName();
            $url = $cat->getUrl();
            // Write your code for xml node creation
        }
    }
}

This code is not tested. This is basically an idea.

  • Yeah this works perfectly. Thank you so much!! – Sushant Gurjar Aug 9 '18 at 14:09
  • Just add one more thing to your answer...re-initialize the $cat_name variable or else when a product does not have a child category of (in this case) 'designers', it still populates with the previous product's child category. – Sushant Gurjar Aug 9 '18 at 14:17
  • 1
    @SushantGurjar How to assign subcategory products to their respective parent category. eg: i.stack.imgur.com/XXyaH.png how assign to parent category, here my parent category Electronics. – zus Dec 9 '19 at 6:41
  • You can get parent categories of the categories your products are tagged in. Just write a query to tag them to their parent categories. – Sushant Gurjar Dec 26 '19 at 12:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.