你现在的位置:首页 > PHP网站建设知识库 > magento > 正文

批量复制Magento产品分类目录的代码

在使用magento的时候,由于分类越来越多,有时候想复制一下分类,省点儿事,原来的操作有些麻烦,以下代码可以实现复制目录的功能:

<?php
if(!is_numeric($_GET))die('Please specify a category ID');

$catId=$_GET;

$xml=simplexml_load_file('app/etc/local.xml');
$host=$xml->global->resources->default_setup->connection->host;
$username=$xml->global->resources->default_setup->connection->username;
$password=$xml->global->resources->default_setup->connection->password;
$dbname=$xml->global->resources->default_setup->connection->dbname;
$res=mysql_pconnect($host,$username,$password);
mysql_select_db($dbname);

$catsDone=0;
duplicate_entity($catId);
echo$catsDone.' Categories duplicated.';

function duplicate_entity($id,$parent_id=null){
global$catsDone;

// Grab category to copy
$sql="SELECT * FROM catalog_category_entity WHERE entity_id = ".$id;
$query_entity=mysql_query($sql);

$entity=mysql_fetch_object($query_entity);

if(!$parent_id)$parent_id=$entity->parent_id;

mysql_query("INSERT INTO catalog_category_entity (entity_type_id, attribute_set_id, parent_id, created_at, updated_at, path, position, level, children_count)
VALUES ({$entity->entity_type_id}, {$entity->attribute_set_id}, {$parent_id}, NOW(), NOW(), '', {$entity->position}, {$entity->level}, {$entity->children_count})");
$newEntityId=mysql_insert_id();

$query=mysql_query("SELECT path FROM catalog_category_entity WHERE entity_id = ".$parent_id);
$parent=mysql_fetch_object($query);
$path=$parent->path.'/'.$newEntityId;

mysql_query("UPDATE catalog_category_entity SET path='".$path."' WHERE entity_id=".$newEntityId);


foreach(array('datetime','decimal','int','text','varchar')as$dataType){
$sql="SELECT * FROM catalog_category_entity_".$dataType."
WHERE entity_id=".$entity->entity_id;
//die($sql);
$query=mysql_query($sql);
while($value=mysql_fetch_object($query)){
mysql_query("INSERT INTO catalog_category_entity_".$dataType." (entity_type_id, attribute_id, store_id, entity_id, value)
VALUES ({$value->entity_type_id}, {$value->attribute_id}, {$value->store_id}, {$newEntityId}, '{$value->value}')");
}
}

$sql="SELECT entity_id FROM catalog_category_entity WHERE parent_id = ".$id;
$query=mysql_query($sql);

while($entity=mysql_fetch_object($query)){
duplicate_entity($entity->entity_id,$newEntityId);
}
$catsDone++;
}

?>

请把以上代码保存为1.PHP,放置到Magento安装根目录,然后在浏览器里运行:http://www.yourdomain.com/1.php?id=[您想要复制的分类目录ID]
分类目录ID可在Magento后台分类里查看得到