Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI all, I'm trying add categories and subcategories in magento with this code:

PHP
function stringtourlKey($collectionName, $separator = '-'){
          $accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
          $special_cases = array('&' => 'and');
          $string = mb_strtolower(trim($collectionName), 'UTF-8');
          $string = str_replace(array_keys($special_cases), array_values($special_cases), $string);
          $string = preg_replace($accents_regex, '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'));
          $string = preg_replace("/[^a-z0-9]/u", "$separator", $string);
          $string = preg_replace("/[$separator]+/u", "$separator", $string);
          return trim($string, "-");
        }


        $allGenre = array("Suits & Suit Separates", "Shirts", "Pants", "Sportcoats & Blazers", "Swimwear", "Athletic Clothing", "Loungewear", "Outerwear", "Underwear", "All Clothing");


        foreach($allGenre as $categoryStr) {
        $collectionName = $categoryStr;

        $urlKey = stringtourlKey($collectionName);


        try{
        $category = Mage::getModel('catalog/category');
        $category->setName($collectionName);
        $category->setUrlKey($urlKey);
        $category->setIsActive(1);
        $category->setDisplayMode('PRODUCTS');
        $category->setIsAnchor(1); //for active achor
        $category->setStoreId(Mage::app()->getStore()->getId());
        $parentCategory = Mage::getModel('catalog/category')->load(205);
        $category->setPath($parentCategory->getPath());
        $var = $category->save();
        } catch(Exception $e) {
        var_dump($e);
        }
        }


but through this code, I'm adding categories and sub-categories one by one with parent Id. But i want to add all categories at once. I have an idea through array we can manage it, like

PHP
$testingFlow = array( "first" => array("Monitors", "Oversized Displays", "Monitor Accessories"));


but no idea how to manage this array for category and subcategory, anyone help me?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900