Pages

Tuesday, June 25, 2013

How to import CRE Loaded Categories to Magento Database

Transferring one opensource database to other is a common headache.Recently i faced this problem when i need to move the CRE Loaded database to Magento Database.
Here i am giving the code by that you can load the CRE Loaded categories to Magento Database.
//define(‘MAGENTO’, realpath(dirname(__FILE__)));
require_once ‘/app/Mage.php’;
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
/////Cre Loaded Data Base Connection////////////
$conn = mysql_connect(‘localhost’,'root’,”) ;
mysql_select_db(‘shnoop_cre’,$conn);
//////////////////////////////////
$select_categories = @mysql_query(“select * from categories where categories_id=1 or parent_id=1 order by parent_id,categories_id”);
while($fetch_category = @mysql_fetch_object($select_categories))
{
/////Cre Loaded Data Base Connection////////////
$conn = mysql_connect(‘localhost’,'root’,”) ;
mysql_select_db(‘shnoop_cre’,$conn);
////////////////////////////////
$fetch_category_description = @mysql_fetch_object(mysql_query(“select * from categories_description where categories_id=”.$fetch_category->categories_id));
$uri_key = createPageName($fetch_category_description->categories_name);
$data['general']['path'] = ”;
$data['general']['name'] = $fetch_category_description->categories_name;
$data['general']['meta_title'] = “”;
$data['general']['meta_description'] = “”;
$data['general']['is_active'] = 1;
$data['general']['url_key'] = $uri_key;
$data['general']['display_mode'] = “PRODUCTS”;
$data['general']['is_anchor'] = 0;
if($fetch_category->parent_id==0)
$data['category']['parent'] = 4;//$line[0]; // 3 top level
else
$data['category']['parent'] = creLoadedCategoryName($fetch_category->parent_id);
$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);
# Fix must be applied to run script
#http://www.magentocommerce.com/boards/appserv/main.php/viewreply/157328/
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;
}
}
if(!is_int($parentId))
$parentId = getMagentoCateogryParentId($parentId);
$parentCategory = Mage::getModel(‘catalog/category’)->load($parentId);
$category->setPath($parentCategory->getPath());
}
/**
* Check “Use Default Value” checkboxes values
*/
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->setIsActive(true);
$category->save();
echo “Suceeded
“;
}
catch (Exception $e){
echo “Failed
“;
}
}
}
function createPageName($Title)
{
$Title = str_split(strtolower($Title));
$allowableArr = array(’0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’a',’b',’c',’d',’e',’f',’g',’h',’i',’j',’k',’l',’m',’n',’o',’p',’q',’r',’s',’t',’u',’v',’w',’x',’y',’z',’-',’ ‘);
$pagename = ”;
for($ii=0;$iicategories_name;
}
function getMagentoCateogryParentId($parname)
{
/////Magento Data Base Connection////////////
$conn2 = mysql_connect(‘localhost’,'root’,”) ;
mysql_select_db(‘mage_maint_151′,$conn2);
//////////////////////////////////
$tableName = Mage::getSingleton(‘core/resource’)->getTableName(‘catalog_category_entity_varchar’);
$fetch_mage_category_details = @mysql_fetch_object(mysql_query(“select * from $tableName where value=’”.addslashes($parname).”‘”));
mysql_close($conn2);
return $fetch_mage_category_details->entity_id;
}
THANKS

No comments:

Post a Comment