1

Here is my controller

class T2_Store_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

    public function registerAction()
    {
        $data = $this->getRequest()->getParams();
        //$data['root_category_id'] = createRootCategory($data);
        //$firephp->log($data);
        //var_dump($data);die();

        try {
            createNewStore($data);
        } catch (Exception $e) {
            echo $e->getMessage();
        }

        $this->_redirect('store/index');
    }

    public function createNewStore($data)
    {
        Mage::registry('isSecureArea');

        $multiWebEnable = Mage::getStoreConfig('web/multi_web_general/multi_web');

        //#add Website
        /** @var $website Mage_Core_Model_Website */
        $website = Mage::getModel('core/website');
        $website->setCode($data['store_url'])
                ->setName($data['store_name'])
                ->save();

        //#add StoreGroup
        /** @var $storeGroup Mage_Core_Model_Store_Group */
        $storeGroup = Mage::getModel('core/store_group');
        $storeGroup->setWebsiteId($website->getId())
                   ->setName($data['store_name'])
                   ->setRootCategoryId($data['root_category_id'])
                   ->save();

        //#add Store
        /** @var $store Mage_Core_Model_Store */
        $store = Mage::getModel('core/store');
        $store->setCode($data['store_url'] . '_default')
              ->setWebsiteId($storeGroup->getWebsiteId())
              ->setGroupId($storeGroup->getId())
              ->setName($data['store_name'])
              ->setIsActive(1)
              ->save();
    }

    public function createRootCategory($data)
    {
        Mage::registry('isSecureArea');

        // Create category object
        $category = Mage::getModel('catalog/category');
        $category->setStoreId(0);

        $rootCategory['name'] = $data['store_name'];
        $rootCategory['path'] = "1";
        $rootCategory['display_mode'] = "PRODUCTS";
        $rootCategory['is_active'] = 1;

        $category->addData($rootCategory);

        try {
            $category->save();
            $rootCategoryId = $category->getId();

            return $rootCategoryId;
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}

And whenever I submit the form to registerAction() I will receive an error: Fatal error: Call to undefined function @ this line $data['root_category_id'] = createRootCategory($data);

This is the most strange error I ever met, please help.

1

Use $this->createNewStore($data); instead of createNewStore($data);

for more detail visit

  • Thanks, I solved this problem myself but I still mark this as an answer, forgot $this is unacceptable in PHP, don't know why I make it... – Toan Nguyen Mar 16 '15 at 6:13
0

Maybe you've just overlooked it, or perhaps you're not familiar with Object-oriented programming. Either way, it seems to me that you need to change that line to

$data['root_category_id'] = $this->createRootCategory($data);

Prefixing the function name with $this-> means that it will look for a function with that name WITHIN the class that the code is executed.

EDIT: As pointed out by Sohel Rana, you'll also want to change createNewStore($data); to

$this->createNewStore($data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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