I have to assign all subcategory products to parent category in bulk. I searched a lot but i didn't get any option in backend.
parent category
Sub category -1
- product-1
- product-2
Sub category -2
- product-1
- product-2
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityLets say that the main category id is $categoryId
.
You need to do this:
Create a file called update.php
on the same level as index.php
with this content
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(0));
$category = Mage::getModel('catalog/category')->load($categoryId);
$allProducts = $category->getProductsPosition();
foreach ($category->getChildrenCategories() as $child) {
$childProducts = $child->getProductsPosition();
$allProducts = array_merge($allProducts, $childProducts);
}
$category->setPostedProducts($allProducts)->save();
Then just call the file in your browser. http://magentoroot.com/update.php
.
Make sure you backup your database first, in case I'm wrong.
And make sure to remove the script from your webserver again afterwards.
all subcategory products
?
May 13, 2015 at 7:45
is anchor
to yes
for the main categories and it will display on frontend the products from all subcategories.
Here the perfect code for assign products in a specific category.
main category id is $categoryId
<?php
define('MAGENTO_ROOT', getcwd());
include_once "app/Mage.php";
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
Mage::init();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
umask(0);
Mage::app('admin');
set_time_limit(0);
ini_set('memory_limit', '2048M');
$category = Mage::getModel('catalog/category')->load($categoryId);
$allProducts = $category->getProductsPosition();
foreach ($category->getChildrenCategories() as $child)
{
$childProducts = $child->getProductsPosition();
foreach($childProducts as $productId=>$position)
{
if(!array_key_exists($productId,$allProducts))
{
$allProducts[$productId] = $position;
}
}
}
$category->setPostedProducts($allProducts)->save();
?>
show cms block
only. Also try reindexing and clearing the cache, the usual stuff. I'm not sure what else could be wrong here