Created
February 6, 2015 10:27
-
-
Save sprankhub/aaec76fa1bb24a6f633f to your computer and use it in GitHub Desktop.
Magento Script To Activate Products of Child Categories in the Parent Category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A candidate for your first n98-magerun module with a "category:child:activate-products" command?