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>