Magento – Check if Customer Attribute Already Exists

attributescustomermagento-community

I have a custom module which is called on the customer_save_observer_executed event and specifically when a customer updates their details in their account page (name, password, email etc) I have added a custom attribute called display_name.

When a customer submits this form i need to check if the display_name currently exists for any other customer. If it doesn't then setDisplayName(...) else do nothing/display error message.

I was hoping to do this with this snippet:

$customer_check = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('display_name')
->addAttributeToFilter('display_name',$new_name)->load();

if ( is_object($customer_check) && count($customer_check) >= 2) {
    // dont allow - duplicate customer displayname
}
else {
    // allow update....
}

My current code in Model -> Observer.php

class xxxxxxx_Model_Observer
{
    public function xxxxxxxx(Varien_Event_Observer $observer)
    {
        // to stop event being fired twice
        if(Mage::registry('customer_save_observer_executed')){
            return $this;
        }

        $postData = Mage::app()->getRequest()->getPost();
        $customer = $observer->getCustomer();

        // if updating NOT a new customer
        if($customer instanceof Mage_Customer_Model_Customer && !$customer->isObjectNew()) {

            // check display name is posted
            if(!empty($postData['display_name'])){

                $current_name = $customer->getDisplayName();
                $new_name = $postData['display_name'];

                // duplicate check
                $customer_check = Mage::getModel('customer/customer')
                ->getCollection()
                ->addAttributeToSelect('display_name')
                ->addAttributeToFilter('display_name',$new_name)->load();

                if ( is_object($customer_check) && count($customer_check) >= 2) {
                    // dont allow - duplicate customer displayname
                }
                else {

                    if( $postData['display_name'] !== $current_name ) {
                        $customer->setDisplayName($postData['display_name']);
                    }

                }

            }

        }

        Mage::register('customer_save_observer_executed',true); 

    }
}

but this just updates the display_name even if i deliberately set it to a duplicate of another customers

UPDATE

Looking into this further it looks like the module function itself is not being run or nothing inside it is taking affect, as whatever i put in it doesn't work. It is infact the default behaviour that is setting the displayname not my module. My module is activated and my config file uses the customer_save_commit_after event as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <xxx_xxxxx>
            <version>0.0.1</version>
        </xxx_xxxxx>
    </modules>
    <global>
        <models>
            <xxx_xxxxx>
                <class>xxx_xxxxx_Model</class>
            </xxx_xxxxx>
        </models>
        <events>
            <customer_save_commit_after>
                <observers>
                    <xxx_xxxxx>
                        <class>xxx_xxxxx/observer</class>
                        <method>xxxxxx</method>
                        <type>singleton</type>
                    </xxx_xxxxx>
                </observers>
            </customer_save_commit_after>
        </events>
    </global>
</config>

Best Answer

Try this

Step1: trigger an event on basic of customer_save_before

<global>
    <events>
        <customer_save_before>
            <observers>
                <stockalert>
                    <type>singleton</type>
                    <class>magento37890/observer</class>
                    <method>checkDisplay</method>
                </stockalert>
            </observers>
        </customer_save_before>
    </events>
</global>       

Step2:

In before event give you can get customer id if customer is old,then checking current value and past value match or not. If not match then checking this value exit in Rest of collection. If exit then send a flag (false) and

throw error   using throw Mage::exception
which prevent data save

.....

if($flagofmy==false){

                throw Mage::exception(
                    'Mage_Customer', Mage::helper('customer')->__('This customer lastname already exists'),
                    Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
                );
}

full Logic

<?php
class Stackexchange_Magento37890_Model_Observer
{
public function checkDisplay(Varien_Event_Observer $observer){
    $flagofmy=true;
    //eeeeee
    $customer=$observer->getEvent()->getCustomer();
    Mage::log('My log entry'.$customer->getId(), null, 'Magento37890.log');
    /* if customer  is old customer */
    if($customer->getId()){
            if($customer->getOrigData('lastname')!=$customer->getData('lastname')){

            Mage::log($customer->getData('lastname').'My log entry'.$customer->getId(), null, 'Magento37890.log');

            $flagofmy=$this->HasLastName($customer->getData('lastname'));
            if($flagofmy==false){

                throw Mage::exception(
                    'Mage_Customer', Mage::helper('customer')->__('This customer lastname already exists'),
                    Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
                );

                }
            }else{
                    Mage::log($customer->getData('lastname').' Step A _1'.$customer->getId(), null, 'Magento37890.log');

            }

    }else{
        /* New Customer */

            $flagofmy=$this->NewHasLastName($customer->getData('lastname'));

                throw Mage::exception(
                    'Mage_Customer', Mage::helper('customer')->__('This customer lastname already exists'),
                    Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS
                );

    }

}
protected function HasLastName($lastname){


 $customer_check = Mage::getModel('customer/customer')
                ->getCollection()
                ->addAttributeToSelect('lastname')
                ->addAttributeToFilter('lastname',$lastname);

         if(count($customer_check)>0){
                return false;
         }  
         return true;     
}
}

Note: I have done basic of lastname field juts need to change field name to display_name