Programmatically create a new category in purplish

I just want to create a new category programmatically in purple. I have some code and tried this, but can't succeed.

The code is here: -

<?php
$parentId = '2';

$category = new Mage_Catalog_Model_Category();
$category->setName('check');
$category->setUrlKey('new-category');
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(0);

$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());              

$category->save();
unset($category);

?>

in fact, the problem with this code is whenever I tried it, than created a new category, but I canโ€™t set is_active yes to admin.

+4
source share
2 answers

Set a Store ID for this category. To encode the side magenta page, use the codes below:

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

Please refer to my guide which explains how to program a category and subcategory.

function addManufacturers( $manufacturers ) {
Mage::register('isSecureArea', 1);
$parentId = '2';
$list = array();

foreach ($manufacturers as  $key => $manufacturer) {
    try {

        $enabled = 1;
        if ($key == 0) {
            $parentId = '2';
        }
        else {
            $parentId = $list[0];
        }

        $category = Mage::getModel('catalog/category');
        $category->setName($manufacturer);
        $category->setMetaTitle($manufacturer);
        $category->setIncludeInMenu(1);
        $category->setUrlKey(strtolower($manufacturer));
        $category->setDescription(strip_tags($manufacturer));
        $category->setMetaDescription($manufacturer);
        $category->setMetaKeywords($manufacturer);
        $category->setIsActive($enabled);
        $category->setDisplayMode('PRODUCTS');
        $category->setIsAnchor(1); //for active anchor
        $category->setStoreId(Mage::app()->getStore()->getId());
        $parentCategory = Mage::getModel('catalog/category')->load($parentId);
        $category->setPath($parentCategory->getPath());
        $category->setCustomUseParentSettings(true);
        $category->save();
        $list[$key] = $category->getId();
        echo 'Category ' . $category->getName() . ' ' . $category->getId() . ' imported successfully' . PHP_EOL;
    } catch (Exception $e) {
        echo 'Something failed for category ' . $manufacturer . PHP_EOL;
        print_r($e);
    }
}
return $list;

}

http://www.pearlbells.co.uk/how-to-create-new-categories-and-assigned-products-to-category-programmatically-magento/

0

Source: https://habr.com/ru/post/1524468/


All Articles