|
My situation is a little more complicated, because I have several Top Level categories, which can each contain next level categories with the same name.
If this is never a possibility for you, (i.e. you are able to have a unique index on the category names) you only need two fields, category number and category name. Refreshing the table when you add/delete/edit categories goes very quickly.
partial code to import categories from the results of a mysql query
(replace the 7 in array(7,$cat_details) with the parent category id of your new category)
$cat_details = array ();
$cat_details["name"] = “name”;
$cat_details["is_active"] = “1”;
$cat_details["available_sort_by"] = “name”;
$cat_details["default_sort_by"] = “name”;
$cat_details["is_anchor"] = 1;
while ($row = mysql_fetch_row($result)) {
$cat_details["name"] = $row[0];
echo $cat_details["name"] . “<br>\n”;
$newCategoryId = $proxy->call($sessionId,’category.create’,array(7,$cat_details));
echo $cat_details["name"] . “newCategoryId = $newCategoryId <br>\n”;
ob_flush();
flush();
usleep(300000);
}
FWIW, I inderstand your wanting to be able to import/assign products to categories by “name” and not bi id. So I created a table that has the name for each category id, and I repoplulate it each time when I add categories. Then I look into that table to find the category id when I assign a product to categories for which I know the name. In my case, I have just a few “parent” categories that are identified by the enum. Your mileage will vary, but you can extend the concept.
CREATE TABLE IF NOT EXISTS `hp_cat_nums` (
`catnum` int(11) NOT NULL,
`class` enum(’B’,’T’,’E’,’S’,’F’) NOT NULL,
`Name` varchar(60) NOT NULL,
PRIMARY KEY (`catnum`),
KEY `class` (`class`),
KEY `Name` (`Name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
And here is my script for updating that table:
if (@mysql_connect(DB_HOST,DB_USER,DB_PWORD))
mysql_select_db(DB_NAME);
else die("Could not connect!");
foreach ($cat_tree["children"] as $cat) {
try{
$ar = $proxy->call($sessionId, ‘category.info’,$cat[’category_id’]);
if ($ar)
{echo “<br>success for “ . $row[0] . “ category “ . $cat["name"] . “<br>\n”;
$class = substr($cat["name"],0,1);
$sub_cat_tree = $proxy->call($sessionId, ‘category.tree’,$cat[’category_id’]);
foreach($sub_cat_tree["children"] as $sub_cat) {
$sql = “Select Name from hp_cat_nums where class = ‘$class’ and Name = ‘“ . $sub_cat["name"] . “‘“;
$result = mysql_query($sql);
if ($rr = mysql_fetch_row($result)) {
$s = “Update hp_cat_nums set catnum = “ . $sub_cat[’category_id’] . “ where class = ‘$class’ AND Name = ‘“ . $sub_cat["name"] . “‘“;
echo “<blockquote>$s</blockquote><br>\n”;
$r = mysql_query($s);
} else {
$s = “Insert into hp_cat_nums values (” . $sub_cat[’category_id’] . “, ‘$class’,’” . $sub_cat["name"] . “‘)”;
echo “<blockquote>$s</blockquote><br>\n”;
$r = mysql_query($s);
}
}
// var_dump($ar);
} else
{echo “<br>failure for “ . $row[0] . “ category “ . $cat["name"] . “<br>\n”;}
}
catch (SoapFault $e) {echo “<br> $e <br>No Category Found for id “ . $cat["category_id"] . “\n”;}
}
rentown - 07 November 2009 08:53 AM hi
pezomatic
doenst the link below import categories?
http://www.magentocommerce.com/wiki/how-to/how_to_import_category_structure_with_products_using_dataflow
can you take a look and see if this is it?
and if yes how can be installed as i did install but its not working at all.
thanks
|