I have a php script that extracts the products from the XML file and imports them in magento 1.9 assigning products to the parent category and to all child categories. The problem arises when I want to assign the product also to a secondary category that is not part of the tree.


  1. Product --Main Category ---> Subcategory1 ---> Subcategory 2
  2. Product ---> Secondary Category

Step 1 works, while step 2 doesn't.

Below is the part of php code that extracts the data from the XML file and the part of the XML file relating to the categories.


<Id>835</Id><Code />
<Name>EARRINGS</Name><Name lang="it-IT">EARRINGS</Name>
<FullPathName>JEWELS/ EARRINGS / BIJOUX</FullPathName></ProdCategory>
<ProdCategory><Id>843</Id><Code /><Name>CHRISTMAS OFFERS</Name>
<Name lang="it-IT">CHRISTMAS OFFERS</Name><FullPathName>CHRISTMAS OFFERS</FullPathName>


$category_xml = $prod->ProdCategories[0]->ProdCategory[0]->FullPathName[0];
        $cat_split = explode('/',$category_xml);

        foreach ($cat_split as $cat){

            $cat = trim($cat);
            $category = Mage::getResourceModel('catalog/category_collection')
                ->addFieldToFilter('name', $cat)

            $cat_ids[] = intval($category->getId());


Currently in magento the product is assigned only to the JEWELS categories and its sub-categories, EARRINGS and BIJOUX, but it is not assigned to the CHRISTMAS OFFERS category.

Can anyone give me advice on how to modify the script to allow the product to be assigned to secondary categories as well? Thank you so much!

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.