4

we created 4 categories.

Category 1, 2 , 3, 4

all have 50 + 50 + 50 + 50 products,

I created category 5 and category 6 ,

I want to add products of "category 1 & 2" to Category 5

I want to add products of "category 3 & 4" to Category 6.

is there any solution for this in magento site.

please hel pme to find solution.

thanks in advance.

3 Answers 3

5

I would use a sql query, something like:

INSERT INTO catalog_category_product (category_id, product_id, position) (
SELECT 5, product_id, position FROM catalog_category_product WHERE category_id IN (1,2)
);

INSERT INTO catalog_category_product (category_id, product_id, position) (
SELECT 6, product_id, position FROM catalog_category_product WHERE category_id IN (3,4)
);

But I'm not sure whether this is enough

10
  • 1
    I don't understand the question. Why do you want to change aynthing in files? You need to change database content Commented Feb 23, 2015 at 14:14
  • 1
    The product is already part of the category. You can use INSERT IGNORE which ignores these errors (but all others too) Commented Mar 2, 2015 at 7:36
  • 1
    Marius has a solution with code. And no with But I'm not sure whether this is enough I mean I don't know what side effects this has ;-) Commented Mar 2, 2015 at 8:00
  • 1
    Just read \Mage_Catalog_Model_Resource_Category::_saveCategoryProducts to make sure you do everything this method is doing Commented Mar 2, 2015 at 8:06
  • 1
    Did you reindex? Commented Sep 25, 2015 at 8:36
5

Try this:

Create a file called clone.php on the same level as index.php with this content

error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);//just so you see the errors if any
Mage::app();//instantiate the application
$category1Id = 1;
$category2Id = 2;
$category5Id = 5;
//get the category instances
$category1 = Mage::getModel('catalog/category')->load($category1Id);
$category2 = Mage::getModel('catalog/category')->load($category2Id);
$category5 = Mage::getModel('catalog/category')->load($category5Id);

//get products from the first 2 categories
$category1Products = $category1->getProductsPosition();
$category2Products = $category2->getProductsPosition();
//merge the products into one big array
$merged = array_merge($category1Products, $category2Products);
//assigned the merged products to category 5.

$category5->setPostedProducts($merged);
//save the category 5;
$category5->save();

When you are done call clone.php in the browser.

5
  • 1
    hi @Marius, thanks for reply. can you please tell me where i have to add this code in which file and in which folder? Commented Feb 23, 2015 at 7:29
  • @user9434334345. See my update.
    – Marius
    Commented Feb 23, 2015 at 7:50
  • thanks @Marius clone.php => pastebin.com/vGJvPKVR , i put this in root of the site.... see here : demo.kidsdial.com/clone.php Commented Feb 23, 2015 at 7:58
  • 1
    You should change the values of the category ids to reflect your real ids. I just added 1, 2 and 5 for demo purposes.
    – Marius
    Commented Feb 23, 2015 at 7:59
  • sorry, i changed the category ids and placed clone.php in root of the site =>pastebin.com/QD84T0mj cleared the cache. than what else i have to do? Commented Feb 23, 2015 at 9:15
1

Use this module:

https://github.com/klein0r/magento-category-merge

Helps a lot.

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.