0

I have an observer that listens for when a customer is successfully registered! Using the Magento Event: customer_register_success

I have a custom table called "am_perm" I'm able to manually go into my database and write the SQL query:

INSERT INTO `am_perm`(`uid`, `cid`) VALUES (25, 139) 

and it works perfectly! However in my event observer it does not work!

Here is my code in the event listener the params passed in are:

$userId = 25 & $customerId = 139

public function assignDealer(Varien_Event_Observer $observer)
{
    function assignOneCustomer($userId, $customerId)
    {
        try {
            $userId = intVal($userId);
            $customerId = intVal($customerId);

            $resource = Mage::getSingleton('core/resource');
            $writeConnection = $resource->getConnection('core_write');
            $themeTable = $resource->getTableName('am_perm');
            $sql = "INSERT INTO `am_perm`(`uid`, `cid`) VALUES ($userId, $customerId)";
            $writeConnection->query($sql);//insert
            return true;
        }
        catch(Exception $e){
            die($e);
        }
    }


    $registerParams = Mage::app()->getRequest()->getParams();
    $paramMerchantCode = $registerParams['merchant_code'];

    if (isset($paramMerchantCode)) {

        $customerId = $observer->getCustomer()->getId();
        $merchantIdArray = array();

        //Grab the Admin_Role Table from SQL
        $model = Mage::getModel('admin/role');
        //Grab all roles with Merchant but only need the first one for ID
        //Table will be massive unless we use relational database ID versus primary ID
        $role = $model->getCollection()->addFieldToFilter('role_name', ['eq' => "Merchant"])->getFirstItem();

        //Loop through database and grab all ID's for Merchants and place into $mcArray
        if ($roleId = $role->getId()) {
            $dealerArray = $model->getCollection()->addFieldToFilter('parent_id', ['eq' => $roleId]);
            foreach ($dealerArray as $currentDealer) {
                array_push($merchantIdArray, $currentDealer['user_id']);
            }
        }

        //Look up Merchants and compare code to Merchant Code
        //If Code matches we store the $merchantId
        foreach ($merchantIdArray as $id) {
            $adminUser = Mage::getModel('admin/user')->load($id);
            $userMerchantCode = $adminUser->getData("merchant_code");
            if (strtolower($paramMerchantCode) == strtolower($userMerchantCode)) {
                $uid = $id;
            }
        }
        if ($uid && $customerId) {
            Mage::getModel('amperm/perm')->assignOneCustomer($uid, $customerId);
        }
    }


}

It never catches the Exception $e but it also never saves to my database? What am I missing?

3 Answers 3

0

This exception is thrown in file Mage_Customer_AccountController . As this error is coming when you are creating a new customer so this error must be thrown in createPostAction.go to catch block in this action and you will see that cannot save customer data message is being set in session on line no 319 . So in that block you may print $e->getMessage() like this

print_r($e->getMessage());
die(get_class($this));//this will help you to find the class where you have written die , if in case you forget the file where you have placed die;

make sure you take a backup of this file before making any changes in it. you will see the error message which may be helpfull to solve the error message.

8
  • Did you mean line 419? I receive the following message "Parse error: syntax error, unexpected '$this' (T_VARIABLE) in /home/makeyou/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php on line 423" Feb 22, 2017 at 5:43
  • the magento i checked had this on line 319. file path is app/code/core/Mage/Customer/controllers/AccountController.php Feb 22, 2017 at 5:46
  • also if i copy what you typed I receive: "Parse error: syntax error, unexpected ';' in /home/makeyou/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php on line 422" Feb 22, 2017 at 5:46
  • What is the exact file path? Feb 22, 2017 at 5:47
  • i edited my answer. by mistaked i missed last ) in second line of my code. exact file path is app/code/core/Mage/Customer/controllers/AccountController.ph‌​p Feb 22, 2017 at 5:48
0

Use below code:

function assignOneCustomer($userId, $customerId)
        {
            try
            {
                $userId = intVal($userId);
                $customerId = intVal($customerId);

                $resource = Mage::getSingleton('core/resource');
                $writeConnection = $resource->getConnection('core_write');
                $themeTable = 'am_perm';
                $sql = "INSERT INTO " . $themeTable ."(uid, cid) VALUES (".$userId.",". $customerId.")";//insert only
                $writeConnection->query($sql);//insert
                return true;
            }
            catch(Exception $e)
            {
            }
        }
9
  • If I take out $writeConnection->update($sql);//insert it has no error and creates the customer normally however they have no "dealer" which is what this observer is trying to do it's attempting to link the uid(userId) with the cid(customerId) Feb 22, 2017 at 6:21
  • Please let me know what you want to do i can't getting whats your exact approch Feb 22, 2017 at 6:30
  • I want to create in the table am_perm a row thats fills in for uid and cid then saves it Feb 22, 2017 at 6:31
  • Right now if I don't have the line: $writeConnection->update($sql); It doesn't save it to the table but it also doesn't throw an error Feb 22, 2017 at 6:32
  • Update code please check it Feb 22, 2017 at 7:22
0

I only have a semi answer for people who have a similar problem to me. I was able to fix the above by using:

Mage::getModel('amperm/perm')->assignOneCustomer($userid, $customerId);

I have no idea ultimately though as to why I could manually write to the SQL database table, "am_perm" yet couldn't do it through an observer

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.