Reputation: 43
How can I apply Parent Category to it's Sub Category Products? (Magento 1.x)
For example:
DEFAULT CATEGORY
CLOTHING
- Tops (shirt1)sku - category
shirt1 - Tops
shirt2 - Tops
shirt3 - Tops
my goal is to apply "CLOTHING" category (which is the parent category) to "shirt1, shirt2 & shirt3" like this:
sku - category
shirt1 - Tops, CLOTHING
shirt2 - Tops, CLOTHING
shirt3 - Tops, CLOTHING
without using csv or going to Admin Panel > Catalog > Manage Categories > Category > Category Products > Manually looking and applying products
Upvotes: 0
Views: 778
Reputation: 300
I have create this code. i have not used yet but definitely works for you.
Just execute below script from root
require_once('app/Mage.php');
Mage::app();
$pro_ids = Mage::getModel('catalog/product')->getCollection()->getAllIds();
foreach($pro_ids as $pro_id){
$product = Mage::getModel('catalog/product')->load($pro_id);
$categories = $product->getCategoryIds(); $save = 0;
foreach($categories as $categorie){
$category = Mage::getModel('catalog/category')->load($categorie);
foreach ($category->getParentCategories() as $parent) {
if(!in_array($parent->getId(), $categories)){
$categories[] = $parent->getId(); $save = 1;
}
}
}
if($save == 1){
$product->setCategoryIds($categories);
$product->save();
}
}
Please check this and let me know in case of any query.
Thanks
Upvotes: 2