Um dos nossos clientes precisou de uma loja com muitas categorias e subcategorias, encontramos esta solução:
Você deve criar um csv e colocá-lo em:
'/var/import/importarCategoria.csv'
Você precisa apenas utilizar duas colunas no CSV como mostrado abaixo, aonde o número é respectivo a sua categoria mestre e a segunda coluna será a categoria que será adicionada dentro desta.
2 categoria1 3 categoria2 4 categoria3
Crie um arquivo no diretório raiz da sua loja Magento chamado importarcategoria.php com o seguinte código:
<?php define('MAGENTO', realpath(dirname(__FILE__))); require_once MAGENTO . '/app/Mage.php'; umask(0); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $count = 0; $file = fopen('./var/import/importarCategoria.csv', 'r'); while (($line = fgetcsv($file)) !== FALSE) { $count++; //$line is an array of the csv elements if (!empty($line[0]) && !empty($line[1])) { $data['general']['path'] = $line[0]; $data['general']['name'] = $line[1]; $data['general']['meta_title'] = ""; $data['general']['meta_description'] = ""; $data['general']['is_active'] = ""; $data['general']['url_key'] = ""; $data['general']['display_mode'] = "PRODUCTS"; $data['general']['is_anchor'] = 0; $data['category']['parent'] = $line[0]; // 3 top level $storeId = 0; createCategory($data,$storeId); sleep(0.5); unset($data); } } function createCategory($data,$storeId) { echo "Starting {$data['general']['name']} [{$data['category']['parent']}] ..."; $category = Mage::getModel('catalog/category'); $category->setStoreId($storeId); if (is_array($data)) { $category->addData($data['general']); if (!$category->getId()) { $parentId = $data['category']['parent']; if (!$parentId) { if ($storeId) { $parentId = Mage::app()->getStore($storeId)->getRootCategoryId(); } else { $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID; } } $parentCategory = Mage::getModel('catalog/category')->load($parentId); $category->setPath($parentCategory->getPath()); } if ($useDefaults = $data['use_default']) { foreach ($useDefaults as $attributeCode) { $category->setData($attributeCode, null); } } $category->setAttributeSetId($category->getDefaultAttributeSetId()); if (isset($data['category_products']) && !$category->getProductsReadonly()) { $products = array(); parse_str($data['category_products'], $products); $category->setPostedProducts($products); } try { $category->save(); echo "Suceeded <br /> "; } catch (Exception $e){ echo "Failed <br />"; } } }
Agora rode o script no respectivo endereço:
http://seusite.com.br/importarcategoria.php
Para importar utilize o seguinte script:
<?php define('MAGENTO', realpath(dirname(__FILE__))); require_once MAGENTO . '/app/Mage.php'; Mage::app(); $category = Mage::getModel ( 'catalog/category' ); $tree = $category->getTreeModel (); $tree->load (); $ids = $tree->getCollection ()->getAllIds (); if ($ids) { $file = "var/import/catlist.csv"; file_put_contents($file,"catId, catName\n"); foreach ( $ids as $id ) { $string = $id . ', ' .$category->load($id)->getName() . "\n"; file_put_contents($file,$string,FILE_APPEND); } } ?>
Src: http://www.magentoworks.net/import-bulk-category-in-magento
3 Comentários
fiz tudo só faltou a ultima parte: “Para importar utilize o seguinte script” como faço isso ?
Desculpe o ocorrido Fábio, os códigos não estavam exibindo corretamente no site, atualizei.
<?php
define(‘MAGENTO’, realpath(dirname(__FILE__)));
require_once MAGENTO . ‘/app/Mage.php’;
Mage::app();
$category = Mage::getModel ( ‘catalog/category’ );
$tree = $category->getTreeModel ();
$tree->load ();
$ids = $tree->getCollection ()->getAllIds ();
if ($ids) {
$file = "var/import/catlist.csv";
file_put_contents($file,"catId, catName\n");
foreach ( $ids as $id ) {
$string = $id . ‘, ‘ .$category->load($id)->getName() . "\n";
file_put_contents($file,$string,FILE_APPEND);
}
}
?>