Skip to content

Instantly share code, notes, and snippets.

@tuanphpvn
Last active August 3, 2016 15:29
Show Gist options
  • Save tuanphpvn/94f2368b5c8c1725bed1 to your computer and use it in GitHub Desktop.
Save tuanphpvn/94f2368b5c8c1725bed1 to your computer and use it in GitHub Desktop.
Random n product to category #Magento, #Category, #Product, #CreateData
<?php
$addRamdomProductToCategory = function($n, $arrCategoryId) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productModel = Mage::getModel('catalog/product');
$productIds = Mage::getModel('catalog/product')->getCollection()->getAllIds();
foreach($arrCategoryId as $catId) {
$arrProductIdIndexs = array_rand($productIds, $n);
foreach($arrProductIdIndexs as $index) {
$productObj = $productModel->load($productIds[$index]);
$productObj->setCategoryIds(array($catId));
$productObj->setIsMassupdate(true);
$productObj->setExcludeUrlRewrite(true);
$productObj->save();
}
}
};
$addRamdomProductToCategory(20, $arrCategoryId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment