2012-02-21 73 views
0

.Hi,
我試圖使屬性taxvat唯一爲每個客戶。 (特別是對於我從後端創建的用戶)。即沒有重複。
就像電子郵件屬性一樣,如果電子郵件已被使用,則會通知用戶使用其他電子郵件。
我試圖從eav_attribute更改「is_unique」爲「1」,但什麼都沒有發生。
你能幫我解決這個問題嗎?
感謝Magento獨特TaxVat atrribute爲每個客戶

回答

1

好吧,我找到了解決辦法。在 查找文件/app/code/core/Mage/Customer/Model/Form.php(不要忘了,而不是覆蓋。) 「公共職能是validateData(數組$數據)」

添加代碼的 的foreach($這 - >的getAttributes()作爲$屬性)

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 
} 
0

我一直在尋找該溶液一段時間內。但我注意到你所引用的form.php文件是magento的1.5版本。在版本1.6中,文件是不同的...在1.6版本中放置代碼的位置在哪裏?謝謝。

我找到了該文件。它位於/app/code/core/Mage/Eav/Model/form.php。 但我把代碼,並沒有在這裏工作......我不斷嘗試一個解決方案。

+0

<customer_save_before> <observers> <mymodule_customer_save_before> <class>mymodule/observer</class> <method>checkUniqueAttribute</method> </mymodule_customer_save_before> </observers> </customer_save_before> 

而在你的觀察員 if($ result!== true){error = array_merge($ errors,$ result); } ... – karpa 2012-03-14 14:01:32

1

我修改了哪些Karpa寫道,使其更好地

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('entity_id', array('neq' => $cid)); //exclude current user from results //###### not working...... 
      if (count($cli)>0) { 
       $label = $attribute->getStoreLabel(); 
       $errors = array_merge($errors, array(Mage::helper('customer')->__('"%s" already used!',$label))); 
      } 
0

您可以創建一個事件:後添加

/** 
    * Check customer attribute which must be unique 
    * 
    * @param Varien_Event_Observer $observer 
    * @return $this 
    * @@see customer_save_before 
    */ 
    public function checkUniqueAttribute(Varien_Event_Observer $observer) 
    { 
     /** @var Mage_Customer_Model_Customer $customer */ 
     $customer = $observer->getEvent()->getCustomer(); 
     foreach ($customer->getAttributes() as $attribute) { 
      if ($attribute->getIsUnique()) { 
       $collection = Mage::getModel('customer/customer') 
        ->getCollection() 
        ->addAttributeToFilter($attribute->getAttributeCode(), $customer->getData($attribute->getAttributeCode())) 
        ->addFieldToFilter('entity_id', array('neq' => $customer->getId())); 

       if ($collection->getSize()) { 
        Mage::throwException(sprintf(
         'The value %s for %s is already used', 
         $customer->getData($attribute->getAttributeCode()), 
         $attribute->getStoreLabel() 
        )); 
       } 
      } 
     } 

     return $this;