Search code examples
phpmagentoattributesunique

Magento unique TaxVat atrribute for every customer


.Hi,
I'm trying to make the attribute taxvat unique for every customer. (especially for users that i create from the backend). i.e no duplicates.
Just like the email attribute, if the email is already used, user gets notified to use another email.
I tried from the eav_attribute to change "is_unique" to "1" but nothing happened..
Can you please help me how to achieve this..?
Thanks


Solution

  • ok, i found the solution.. Find file /app/code/core/Mage/Customer/Model/Form.php (Don't forget to override instead..) in "public function validateData(array $data)"

    add the code inside the foreach ($this->getAttributes() as $attribute)

    foreach ($this->getAttributes() as $attribute) {
    ...
    ...
    //## code to add
            if ($attribute->getIsUnique()) {
                $cid = $this->getEntity()->getData('entity_id'); //get current customer id
                $cli = Mage::getModel('customer/customer')
                ->getCollection()
                ->addAttributeToFilter($attribute->getAttributeCode(), $data[$attribute->getAttributeCode()]);
                //->addFieldToFilter('customer_id',   array('neq' => $cid)); //exclude current user from results  //###### not working......
                $flag=0;
                foreach ($cli as $customer) {
                     $dataid=$customer->getId();
                     if ($dataid != $cid) //if the value is from another customer_id
                        $flag |= 1;  //we found a dup value
                }
    
                if ($flag) {
                    $label = $attribute->getStoreLabel();
                    $errors = array_merge($errors, Mage::helper('customer')->__('"%s" already used!',$label));
                }
            }
    //## End of code to add
    }