cancel
Showing results for 
Search instead for 
Did you mean: 

Unique nickname on Customer data

SOLVED

Unique nickname on Customer data

hi there all  Smiley Very Happy

 

i need a UNIQUE Nickname Field in custumers ... and i found this code on internet ....  

 

//define('MAGENTO', realpath('path_donde_este_instalado_magento'));define('MAGENTO', realpath(dirname(__FILE__)));ini_set('memory_limit', '128M'); 
require_once MAGENTO.'/app/Mage.php';umask(0);
Mage::app(); 
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');$setup->startSetup(); 
$entityTypeId     = $setup->getEntityTypeId('customer');$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId); 
$setup->addAttribute('customer', 'nickname', array(
    'input'         => 'text',
    'type'          => 'text',
    'label'         => 'Nickname',
    'visible'       => 1,
    'required'      => 1,
    'user_defined'  => 1,
    'admin_html'    => 1,
    'unique'        => 1
)); 
$setup->addAttributeToGroup(    $entityTypeId,    $attributeSetId,    $attributeGroupId,
    'nickname',
    '1000'  //sort_order
); 
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'nickname');$oAttribute->setData('used_in_forms', array('adminhtml_customer'));$oAttribute->save();

 

i put my codes in REGISTER.phtml  and EDIT.phtml ... 

 

this works perfect.. but when i go to register a new user ..

 

1.- the field dont save anything in my database --- same in EDIT.phtml when the user is already registered.

----SOLVED THIS ... I ADD THIS LINES

       'customer_account_create','customer_account_edit','checkout_register'  AT THE LAST LINE....  

 

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'nickname');$oAttribute->setData('used_in_forms', array('adminhtml_customer', 'customer_account_create','customer_account_edit','checkout_register'));$oAttribute->save();

 

2.- in my admin panel ... in custumer -> clients -> acount information..  the NICKNAME appear .. but when i save one NICKNAME then go to other customer ...  and put the SAME NICKNAME... the record its saved and no message of other client with the same NICKNAME is showed!!!!

 

 

with this code i have this TABLE in my DB "customer_entity_varchar" ... 

 

how can i make this field UNIQUE in mysql ... and get a error message in FRONTEND and BACKEND?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Unique nickname on Customer data

and the before code ... can optimize it like this ....

 

 

/* MODIFICACION KUFF */

        $nickname = strtolower($customer->getNickname());
        $read = Mage::getSingleton('core/resource')->getConnection('core_read');

        $results = $read->fetchAll("select * from kuff16_customer_entity_text where LOWER(value) = '".$nickname."' AND entity_id != '" .$customer->getId(). "'");

        if(count($results) > 0)
        {
            throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Nickname already exists.'));
        }

/* FIN DE MODIFICACION KUFF */

REMEMBER PUT THIS AT _beforeSave METHOD ...  after this line       

         parent::_beforeSave($customer);

 

 

View solution in original post

4 REPLIES 4

Re: Unique nickname on Customer data

For all the people who need to make a unique nickname customer field ... i make this ...

 

copy your file 

 

app/code/core/Mage/Customer/Model/Resource/Customer.php

 

to 

 

app/code/local/Mage/Customer/Model/Resource/Customer.php

 

then....

 

add this lines to _beforeSave method  after  parent::_beforeSave($customer); line.

 

		$nickname = strtolower($customer->getNickname());
		$read = Mage::getSingleton('core/resource')->getConnection('core_read');
		if (Mage::app()->getStore()->isAdmin())
		{
			$results = $read->fetchAll("select * from customer_entity_text where LOWER(value) = '".$nickname."' AND entity_id != '" .$customer->getId(). "'");
		}
		else
		{
			$results = $read->fetchAll("select * from customer_entity_text where LOWER(value) = '".$nickname."'");
		}
		
		if(count($results) > 0)
		{
			throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Username already exists.'));
		}

 

and thats it!!!!   ... all work perfect !!!!

 

have a nice coding!!!!   Smiley Happy

Re: Unique nickname on Customer data

The before code have a problem .... 

 

when i go to buy something ... the USERNAME ALREADY EXIST!  message showed in a alert box !!!

 

i fix this with this code!!!

 

/* MODIFICACION KUFF */

		$nickname = strtolower($customer->getNickname());
		$read = Mage::getSingleton('core/resource')->getConnection('core_read');
		if (Mage::app()->getStore()->isAdmin())
		{
			$results = $read->fetchAll("select * from kuff16_customer_entity_text where LOWER(value) = '".$nickname."' AND entity_id != '" .$customer->getId(). "'");
            if(count($results) > 0)
            {
                throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Username already exists.'));
            }
		}
		else
		{
            if ($customer->getId() == "") {
            $results = $read->fetchAll("select * from kuff16_customer_entity_text where LOWER(value) = '".$nickname."'");
            if(count($results) > 0)
            {
                throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Username already exists.'));
            }
            }
		}



/* FIN DE MODIFICACION KUFF */

BUT .... This code ... can optimize it!!!  like this ... 

 

 

 

        /* MODIFICACION KUFF */
		/* Check Nickname existence */
		$nickname = strtolower($customer->getNickname());
		$read = Mage::getSingleton('core/resource')->getConnection('core_read');
		if (Mage::app()->getStore()->isAdmin())
		{
			$results = $read->fetchAll("select * from kuff16_customer_entity_text where LOWER(value) = '".$nickname."' AND entity_id != '" .$customer->getId(). "'");
		}
		else
		{
			$results = $read->fetchAll("select * from kuff16_customer_entity_text where LOWER(value) = '".$nickname."' AND entity_id != '" .$customer->getId(). "'");
		}
		
		if(count($results) > 0)
		{
			throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Username already exists.'));
		}
		/* FIN DE MODIFICACION KUFF */

 

 

 

 

Re: Unique nickname on Customer data

and the before code ... can optimize it like this ....

 

 

/* MODIFICACION KUFF */

        $nickname = strtolower($customer->getNickname());
        $read = Mage::getSingleton('core/resource')->getConnection('core_read');

        $results = $read->fetchAll("select * from kuff16_customer_entity_text where LOWER(value) = '".$nickname."' AND entity_id != '" .$customer->getId(). "'");

        if(count($results) > 0)
        {
            throw Mage::exception('Mage_Customer', Mage::helper('customer')->__('Nickname already exists.'));
        }

/* FIN DE MODIFICACION KUFF */

REMEMBER PUT THIS AT _beforeSave METHOD ...  after this line       

         parent::_beforeSave($customer);

 

 

Re: Unique nickname on Customer data

You can add field in customer registration page and store it to database by using this tutorial.

 

You can also use extension to add multiple type of field in registration page, few are as under