1

Having problems with setting up Google Shopping integration. A majority of my products have a parent product which holds the general information about it, other than size or colour. I have have associated products that include the information for the product, as well as size ect.. These products are set as non visible, however, the parent products are set to Catalog and search.

All of my child products do not have their categories set to the same as the parent product. How can I assign these attributes after the product has been created?

I have over 1000 products which need the category information updating to the same as its parent product, so I need something that will do this all in one.

Thanks.

1 Answer 1

2

You can put a script in your shell folder, e.g. update_categories.php with:

Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
    ->addAttributeToFilter('category_ids','')
    ->addAttributeToSelect('*');
$products->load();

foreach($products as $product) { 
    $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
    foreach($parentIds as $parentId) {
        $parent = Mage::getModel('catalog/product')->load($parentId);
        $product->setCategoryIds($parent->getCategoryIds());
        $product->save();
        continue;
    }
}

If you then run this then it should iterate over the simple products that do not belong to a category, then (if they have a configurable parent) update those products so that they now have the category_ids of that parent.

You could run this script on a cron or other job to 'tidy' the products before you submit your data.

Note this code loads what could be a large product collection and may need more work to be memory efficient.

3
  • So if I place this into a php file, and then go to something like example.com/jobs/update_categories.php This script will run? Commented Mar 11, 2015 at 16:00
  • Best from the command line, change to shell folder and run php update_categories.php so that things get updated and you can see error messages. Plus you don't want this to be run by anyone, or do you? Commented Mar 11, 2015 at 16:58
  • Depending on the number of products, this needs some performance optimization. Star with omittting ->addAttributeToSelect('*');, you don't need to load all the product attributes. Commented Jul 13, 2015 at 14:22

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.