Showing posts with label Magento categories. Show all posts
Showing posts with label Magento categories. Show all posts

Programmatically Assign Products from one category to other category in Magento


Programmatically Assign Products from one category to other category in Magento

Create a PHP script file in root directory of Magento directory structure and put below code in it.
Adjust category from ID and to ID according to need.
Execute script.
 To
//  5 => 10
$from = 5;
$to = 10;
 
$category = Mage::getModel('catalog/category')->load($from); 
 
$productCollection = $category->setStoreId(1)->getProductCollection();
 
foreach($productCollection as $_product) {
  $product = Mage::getModel('catalog/product')->load($_product->getId());
 
  $newCategories = $origCats = $product->getCategoryIds();
  if(!in_array($to, $origCats)) {
    $newCategories = array_merge($origCats, array($to));
    $product->setCategoryIds($newCategories)->save();
    echo 'Assigned -- ' . $product->getId() . '
'; } } echo 'Completed Execution!!!'; ?>

Categories of store other than running store in Magento


Sometimes there is need to display categories of store other than running store in Magento.

Below code will achieve it to get categories of store other than running store.

getStore($storeId)->getRootCategoryId();
 
$category_model = Mage::getModel('catalog/category');
 
$_category = $category_model->load($root_cat); // category id for which the child categories to be found
$all_child_categories = $category_model->getResource()->getAllChildren($_category); //array consisting of all child categories id
echo '
';
print_r($all_child_categories);
echo '
'; ?>