0

I have created a customer attribute mobile and include it with customer sign up form. Cutomer sign up is works also for mobile number and customer mobile number is save in database at the time of sign up.

But I want to set email and mobile both unique for customer sign up.

For this I have override createPostAction() function of customer/AccountController.php file.

What condition I have to put in this function to check unique email_id and mobile_number for customer?

2 Answers 2

1

First override Customer Resource model app\code\core\Mage\Customer\Model\Resource\Customer.php in local

After replace code from

protected function _beforeSave(Varien_Object $customer)
    {
        parent::_beforeSave($customer);

        if (!$customer->getEmail()) {
            throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
        }

        $adapter = $this->_getWriteAdapter();
        $bind    = array('email' => $customer->getEmail());

        $select = $adapter->select()
            ->from($this->getEntityTable(), array($this->getEntityIdField()))
            ->where('email = :email');
        if ($customer->getSharingConfig()->isWebsiteScope()) {
            $bind['website_id'] = (int)$customer->getWebsiteId();
            $select->where('website_id = :website_id');
        }
        if ($customer->getId()) {
            $bind['entity_id'] = (int)$customer->getId();
            $select->where('entity_id != :entity_id');
        }

        $result = $adapter->fetchOne($select, $bind);
        if ($result) {
            throw Mage::exception(
                'Mage_Customer', Mage::helper('customer')->__('This customer email already exists'),
                Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
            );
        }


        // set confirmation key logic
        if ($customer->getForceConfirmed()) {
            $customer->setConfirmation(null);
        } elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
            $customer->setConfirmation($customer->getRandomConfirmationKey());
        }
        // remove customer confirmation key from database, if empty
        if (!$customer->getConfirmation()) {
            $customer->setConfirmation(null);
        }

        return $this;
    }

TO

protected function _beforeSave(Varien_Object $customer)
    {
        parent::_beforeSave($customer);

        if (!$customer->getEmail()) {
            throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Customer email is required'));
        }

        $adapter = $this->_getWriteAdapter();
        $bind    = array('email' => $customer->getEmail());

        $select = $adapter->select()
            ->from($this->getEntityTable(), array($this->getEntityIdField()))
            ->where('email = :email');
        if ($customer->getSharingConfig()->isWebsiteScope()) {
            $bind['website_id'] = (int)$customer->getWebsiteId();
            $select->where('website_id = :website_id');
        }
        if ($customer->getId()) {
            $bind['entity_id'] = (int)$customer->getId();
            $select->where('entity_id != :entity_id');
        }

        $result = $adapter->fetchOne($select, $bind);
        if ($result) {
            throw Mage::exception(
                'Mage_Customer', Mage::helper('customer')->__('This customer email already exists'),
                Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
            );
        }

        if($customer->getMobile()) {
            $bind2    = array('mobile' => $customer->getMobile());

            $select2 = $adapter->select()
                ->from($this->getEntityTable(), array($this->getEntityIdField()))
                ->where('mobile = :mobile');
            if ($customer->getSharingConfig()->isWebsiteScope()) {
                $bind2['website_id'] = (int)$customer->getWebsiteId();
                $select2->where('website_id = :website_id');
            }
            if ($customer->getId()) {
                $bind['entity_id'] = (int)$customer->getId();
                $select2 ->where('entity_id != :entity_id');
            }

            $result = $adapter->fetchOne($select2, $bind2);
            if ($result) {
                throw Mage::exception(
                    'Mage_Customer', Mage::helper('customer')->__('This customer mobile already exists'),
                    Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
                );
            }   
        }



        // set confirmation key logic
        if ($customer->getForceConfirmed()) {
            $customer->setConfirmation(null);
        } elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
            $customer->setConfirmation($customer->getRandomConfirmationKey());
        }
        // remove customer confirmation key from database, if empty
        if (!$customer->getConfirmation()) {
            $customer->setConfirmation(null);
        }

        return $this;
    }
3
  • Hello @Abdul, when I have override '_beforeSave' function by your code, it throw me exception "SQLSTATE[42S22]: Column not found: 1054 Unknown column mobile in where clause, query was: SELECT customer_entity.entity_id FROM customer_entity WHERE (mobile = :mobile) AND (website_id = :website_id)" because I have created attribute in eav_attribute table not column in customer_entity table. Jan 23, 2016 at 7:18
  • Pls set table customer _ entity _ var char in query
    – Abdul
    Jan 23, 2016 at 8:03
  • Can you please update your answer using customer_entity_varchar table in place of $this->getEntityTable()? Jan 23, 2016 at 9:43
0
Goto -> phpmysql database
open -> eav_attribute
Find your attribute and Set "1" in "is_unique" field
2
  • Besides the way you are recommending is generally not encouraged on this site, you should add more explanation to the answer about what these changes will impact. You should also add the consequences it can have on a site if not done properly.
    – Prateek
    Jan 22, 2016 at 14:36
  • Hello @Jaydip, I have set 1 for mobile attribute in is_unique column, but it is not work for me. Jan 23, 2016 at 5:11

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.