3

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
5
  • 1
    cant you just set anchor to yes in your root category? will that do for you?
    – Yorrd
    May 13, 2015 at 7:43
  • no it is not working May 13, 2015 at 7:47
  • make sure you didn't set your category to show cms block only. Also try reindexing and clearing the cache, the usual stuff. I'm not sure what else could be wrong here
    – Yorrd
    May 13, 2015 at 7:53
  • @Yorrd how to do reindexing? what do mean root category in my case (parent category or above parent (first category)? It is showing in backend only menas ( parent category (200) ). May 13, 2015 at 8:22
  • 1
    system > index (or something along these lines) > select all > refresh index (or something along these lines ;) )
    – Yorrd
    May 13, 2015 at 8:26

2 Answers 2

2

Lets 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.

4
  • can i do with backend? how can i run this script? May 13, 2015 at 7:38
  • You can also do it manually from the backend. I thought you need an automated solution. See my updated answer anyway.
    – Marius
    May 13, 2015 at 7:41
  • thanks marius. but i dont want automated solution because i am beginner in magento. Can u tell me how to do manually from backend for all subcategory products? May 13, 2015 at 7:45
  • 1
    There is no quick solution to do it from the backend. You have to open 2 tabs. In one edit the main category, in the other one edit each subcategory. Search for each product in each subcategory and assign it to the main category. But I if you need this only for display on frontend you don't need to assign all products manually. You can just set the field is anchor to yes for the main categories and it will display on frontend the products from all subcategories.
    – Marius
    May 13, 2015 at 7:52
2

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();

?>

Your Answer

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

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