每个客户都有Magento独特的Tax Vat属性

时间:2012-02-21 20:14:20

标签: magento attributes unique

.Hi,
我正在努力使每个客户都具有独特的属性taxvat。 (特别是对于我从后端创建的用户)。即没有重复 就像电子邮件属性一样,如果电子邮件已被使用,则会通知用户使用其他电子邮件 我尝试从eav_attribute将“is_unique”改为“1”,但没有发生任何事情。
你能帮助我如何实现这个目标..? 感谢

4 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案.. 查找文件/app/code/core/Mage/Customer/Model/Form.php(别忘了覆盖..) 在“public function validateData(array $ data)”

在里面添加代码 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
}

答案 1 :(得分: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)));
            }

答案 2 :(得分:0)

我一直在寻找这个解决方案。但是我注意到你引用的form.php文件是magento的1.5版本。在版本1.6中,文件是不同的...在1.6版本中将此代码放在何处?谢谢。

我找到了这个文件。它位于/app/code/core/Mage/Eav/Model/form.php中。 但我把代码放在这里并没有起作用......我一直在尝试解决方案。

答案 3 :(得分:0)

您可以创建一个活动:

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

在你的观察者中:

/**
     * 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;