Skip to content

Instantly share code, notes, and snippets.

@sprankhub
Created February 6, 2015 10:27
Show Gist options
  • Save sprankhub/aaec76fa1bb24a6f633f to your computer and use it in GitHub Desktop.
Save sprankhub/aaec76fa1bb24a6f633f to your computer and use it in GitHub Desktop.
Magento Script To Activate Products of Child Categories in the Parent Category
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// only allow php CLI execution
if (php_sapi_name() !== 'cli') {
exit;
}
require_once 'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);
// parent categories on the left, child categories from which products should be taken on the right
$categoryMapping = array(
1 => array(2, 3, 4)
);
foreach ($categoryMapping as $destCategoryId => $sourceCategoryIds) {
$destCategory = Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->load($destCategoryId);
$postedProducts = $destCategory->getProductsPosition();
foreach ($sourceCategoryIds as $sourceCategoryId) {
$sourceCategory = Mage::getModel('catalog/category')->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)->load($sourceCategoryId);
$productsToCopy = $sourceCategory->getProductsPosition();
foreach ($productsToCopy as $id => $position) {
$postedProducts[$id] = $position;
}
}
$destCategory->setPostedProducts($postedProducts);
$destCategory->save();
}
@cmuench
Copy link

cmuench commented Feb 6, 2015

A candidate for your first n98-magerun module with a "category:child:activate-products" command?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment