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'

3
  • Does your Designer category have only one level sub categories? Commented Aug 9, 2018 at 13:34
  • yes, the basically represent the name of the manufacturers. Commented Aug 9, 2018 at 13:48
  • Check my answer which may help you. Commented Aug 9, 2018 at 13:49

1 Answer 1

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.

4
  • Yeah this works perfectly. Thank you so much!! Commented Aug 9, 2018 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. Commented Aug 9, 2018 at 14:17
  • 1
    @SushantGurjar How to assign subcategory products to their respective parent category. eg: i.sstatic.net/XXyaH.png how assign to parent category, here my parent category Electronics.
    – zus
    Commented Dec 9, 2019 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. Commented Dec 26, 2019 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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