0

I would like to set-up a new URL for my x% off beauty promotion. Currently, my beauty related products are housed under a combination of sub categories, so I'm trying to bring them all together under one link for this promotion. Eg- Beauty products are under categories Sun care, Skin care, hair care. How can I combine these existing categories/sub-categories into one and create a link for that? (I want to retain them as individual categories on the navigation, but want to combine it with this promotion)

I tried going into : catalog > Categories > manage categories > add sub-category.

But after this step, I need to manually select each product I want as a part of this sub-cat. This is not feasible as there are heaps of products.

Please let me know how you'd do this. Thanks for your help.

0

Category Parent Child relationships in Magento are one to many (one parent category has many children, each child has exactly one parent). The database schema does not allow what you want to do.

  1. Create a category in your admin panel and assign a url you want to
  2. assign subcategories or categories under it by this trick..

First create the list of sub categories in CSV file, in first column with column heading "Category Name" and file name as "category.csv" then run below script

set_time_limit(0);
ini_set('memory_limit', '1024M');
umask(0);
require_once 'app/Mage.php';
Mage::app('admin');
if (($handle = fopen("category.csv", "r")) !== FALSE) {
  $counter = 1;
  while (($data = fgetcsv($handle)) !== FALSE) {
        if ($counter > 1) {
          $cateName = $data[0];
           echo $cateName."\n"; 
           $rootCateId = 559; // here we need to assign root cate Id under which you want to create your new category
          if($cateName){
           createCategory($cateName, $rootCateId);
          }
        }
        $counter++;
        if($counter > 2){  // if want to break the script
            //exit();
        }
    }
}
function createCategory($name, $rootCateId){

 $path = "1/2/".$rootCateId; // here 1/2 is my store root category path you can check this in database table 'catalog_category_entity' table
 $cateName = $name;
 $category = Mage::getModel('catalog/category');
 $category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().

 if ($id) { //if update
   //$category->load($id);
 }

 $general['name'] = $cateName;
 $general['path'] = $path; // catalog path
 //$general['description'] = " Category";
 //$general['meta_title'] = "My Category"; //Page title
 //$general['meta_keywords'] = "";
 //$general['meta_description'] = "";
 //$general['landing_page'] = ""; //has to be created in advance, here comes id
 $general['display_mode'] = "PRODUCTS"; //static block and the products are shown on the page
 $general['is_active'] = 1;
 $general['is_anchor'] = 0;
 //$general['page_layout'] = 'two_columns_left';
 //$general['url_key'] = "cars";//url to be used for this category's page by magento.
 //$general['image'] = "cars.jpg";

 $category->addData($general);
 try {
     $category->save();
     echo "Success! new category Id: ".$category->getId()."\n";
 }
 catch (Exception $e){
     echo $e->getMessage();
 }
}
-1

It is possible with an index controller and a collection with a category filter in array providing the ids or even with a custom attribute to select.

Sadly I'm on mobile and I can't provide any code right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.