0

I am trying to copy my categories from the old site to the new one it's on different domains but it shares the same hosting server that's why i use the real path.

i already know how to get the categories and how to add them.

the problem here is that i declare the Mage class from domain and get the categories with this code:

$mageFilename = '/chroot/home/stagingv/staging.vatrin.com/html/app/Mage.php';
include($mageFilename);

Mage::init();
Mage::app()->setCurrentStore(1);


$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories();

but when i'm trying to call the Mage class from the new domain with this code in the same page to add the categories to the new domain:

$mageFilename = '/chroot/home/stagingv/gostorestaging.vatrin.com/html/app/Mage.php';
include($mageFilename);

Mage::init();

it return this error:

Fatal error: Cannot redeclare class Mage in /chroot/home/stagingv/gostorestaging.vatrin.com/html/app/Mage.php on line 61

of course i can't use the first one when adding it will add the new categories on the old domain and i don't need that. how i can solve it or any better solution i appreciate?

UPDATE

This is my code:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
//header('Content-type: text/plain; charset=utf-8');
error_reporting(E_ALL);
$mageFilename = '/chroot/home/stagingv/staging.vatrin.com/html/app/Mage.php';
include($mageFilename);

Mage::app()->setCurrentStore(1);
// this what get all categories
getListOfCats();



$mageFilename = '/chroot/home/stagingv/gostorestaging.vatrin.com/html/app/Mage.php';
include($mageFilename);

Mage::app();

// this what get all categories after changing the mage file.
getListOfCats();

function getListOfCats() {   
    $_helper = Mage::helper('catalog/category');
    $_categories = $_helper->getStoreCategories();
    $currentCategory = Mage::registry('current_category');
    $listCats = array();
    $model = Mage::getModel('catalog/category');
    if (count($_categories) > 0) {
        foreach($_categories as $_category) {
            $listCatsTemp = array();
            $listCatsTemp['id'] = $_category->getId();
            $listCatsTemp['name'] = $_category->getName();
            $listCatsTemp['child'] = array();
            $_category = $model->load($_category->getId()); 
            $_subcategories = $_category->getChildrenCategories();
            if (count($_subcategories) > 0) {
                $listSubCats = array();
                foreach($_subcategories as $_subcategory) {
                    $listSubCatsTemp = array();
                    $listSubCatsTemp['id'] = $_subcategory->getId();
                    $listSubCatsTemp['name'] = $_subcategory->getName();
                    $listSubCatsTemp['child'] = array();
                    $_category1 = $model->load($_subcategory->getId());
                    $_subcategories1 = $_category1->getChildrenCategories();
                    if (count($_subcategories1) > 0) {
                        $listSubSubCat1 = array();
                        foreach($_subcategories1 as $_subcategory1) {
                            $listSubSubCatTemp = array();
                            $listSubSubCatTemp['id'] = $_subcategory1->getId();
                            $listSubSubCatTemp['name'] = $_subcategory1->getName();  
                            $listSubSubCatTemp['child'] = array();
                            $_category2 = $model->load($_subcategory1->getId());
                            $_subcategories2 = $_category2->getChildrenCategories();
                            if (count($_subcategories2) > 0) {
                                $listSubSubCat2 = array();
                                foreach($_subcategories2 as $_subcategory2) {
                                    $listSubSubCatTemp1 = array();
                                    $listSubSubCatTemp1['id'] = $_subcategory2->getId();
                                    $listSubSubCatTemp1['name'] = $_subcategory2->getName();
                                    $listSubSubCatTemp1['child'] = array();
                                    $_category3 = $model->load($_subcategory2->getId());
                                    $_subcategories3 = $_category3->getChildrenCategories();
                                    if (count($_subcategories3) > 0) {
                                        $listSubSubCat3 = array();
                                        foreach($_subcategories3 as $_subcategory3) {
                                            $listSubSubCatTemp2 = array();
                                            $listSubSubCatTemp2['id'] = $_subcategory3->getId();
                                            $listSubSubCatTemp2['name'] = $_subcategory3->getName();
                                            $listSubSubCat3[] = $listSubSubCatTemp2;

                                        } // end foreach sub cat 2
                                        $listSubSubCatTemp1['child'] = $listSubSubCat3;
                                    } else {
                                        $listSubSubCat3 = array();

                                    }  // end if sub cat 2
                                    $listSubSubCat2[] = $listSubSubCatTemp1;

                                } // end foreach sub cat 2

                            } else {
                                $listSubSubCat2 = array();

                            }  // end if sub cat 2
                            $listSubSubCatTemp['child'] = $listSubSubCat2;
                            $listSubSubCat1[] = $listSubSubCatTemp;

                        } // end foreach sub cat 1

                    } else {
                        $listSubSubCat1 = array();

                    }  // end if sub cat 1
                    $listSubCatsTemp['child'] = $listSubSubCat1;
                    $listSubCats[] = $listSubCatsTemp;

                } // end foreach sub cat

            } else {
                $listSubCats = array();

            }  // end if sub cat
            $listCatsTemp['child'] = $listSubCats;
            $listCats[] = $listCatsTemp;

        }  // end foreach cats

    }  // end if cats  
    echo '<hr>';
    echo '<pre>';
    print_r($listCats);
    echo '</pre>';
}
?>

Thanks in Advance.

2
  • 1
    note: you still don't need to declare full path if set up is correct - makes code more dynamic and easy to use elsewhere :)
    – treyBake
    Nov 6, 2017 at 13:47
  • yeah i know that but this was an issue coz i'm using same server and same ip for both domains. that's why i gave the actual path for it. thanks for mention that btw. Nov 8, 2017 at 10:04

1 Answer 1

4

If you're using Mage::app() anyway then you don't need Mage::init() too. Mage::app() calls init so maybe that's why you're getting the error.

From app/Mage.php:

public static function app($code = '', $type = 'store', $options = array())
   {
   if (null === self::$_app) {
       self::$_app = new Mage_Core_Model_App();
       self::setRoot();
       self::$_events = new Varien_Event_Collection();
       self::_setIsInstalled($options);
       self::_setConfigModel($options);

       Varien_Profiler::start('self::app::init');
       self::$_app->init($code, $type, $options);
       Varien_Profiler::stop('self::app::init');
           self::$_app->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);
       }
       return self::$_app;
   }

Note: I couldn't duplicate your problem regardless of how I called Mage::app() or Mage::init().

9
  • Thanks for your time . i don't get it, if what u say is right then how i will pass the location of the new Mage file ?! Nov 5, 2017 at 12:07
  • seanbreeden man how i could use the two mage files if i'm using your way ?! Nov 5, 2017 at 12:20
  • I see what you're trying to do now. You have both of those includes, Mage::app() and Mage::init() in the same file. I ran your code locally including everything in one file and it didn't error out for me. Can you paste your entire file and I'll attempt to run it using two local Magento installations. Nov 5, 2017 at 18:04
  • check the update please Nov 6, 2017 at 7:59
  • 1
    The approach you are trying may not be possible in Magento out of the box. You will need to either use the API to get the data from site 2 or export the data from site 2, since you can't redeclare the Mage class from app/Mage.php the way that you are trying. Even if you did get both Mage classes to not error out somehow, it would only pull data from the first declared Mage class, so you're going to have to use the API or break this up into multiple files. Personally, I'd use the API to get the categories from site 2. Nov 6, 2017 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.