Tuesday, October 27, 2015

Export Magento Product Categories to custom table

[sourcecode language="php"]
<?php

ini_set('max_execution_time', 1500);
error_reporting(-1);
ini_set('display_errors', 1);

require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);


$categories = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToSelect('id')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('url_key')
        ->addAttributeToSelect('url')
        ->addAttributeToSelect('is_active');

foreach ($categories as $category) {

    $entity_id = $category->getId();
    $name = mysql_real_escape_string($category->getName());
    $url_key = $category->getUrlKey();
    $url_path = $category->getUrl();
    $is_active = $category->getIsActive();
    $now = time();

    $catids = array();
    foreach ($category->getParentCategories() as $parent) {
        $catids[] = $parent->getId();
    }
    unset($catids[count($catids) - 1]);

    $parent = NULL;
    if (empty($catids)) {
        $parent = NULL;
    } else {
        $parent = array_pop($catids);
    }

$con = mysql_connect("HOST", "USER", "PASSWORD");
mysql_select_db("DB", $con) or die("");

mysql_query("INSERT INTO `categories`(`id`, `name`, `slug`, `parent_id`, `enabled`, `modified_at`, `modified_by`) VALUES ('$entity_id', '$name', '$url_key', '$parent' ,'$is_active', '$now', 1)")  or die(mysql_error());
}

[/sourcecode]

No comments:

Post a Comment

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...