Magento 为每位客户提供独特的 TaxVat 属性

发布于 2025-01-07 23:06:13 字数 184 浏览 1 评论 0原文

.嗨,
我正在努力使每个客户的税金属性都独一无二。 (特别是对于我从后端创建的用户)。即没有重复项。
就像电子邮件属性一样,如果该电子邮件已被使用,用户会收到使用其他电子邮件的通知。
我尝试从 eav_attribute 将“is_unique”更改为“1”,但什么也没发生..
你能帮我如何实现这个目标吗?
谢谢

.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏日浅笑〃 2025-01-14 23:06:13

好的,我找到了解决方案..
查找文件 /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
}

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
}
全部不再 2025-01-14 23:06:13

我修改了 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)));
            }

I've modified what Karpa wrote to make it better

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)));
            }
一片旧的回忆 2025-01-14 23:06:13

我寻找这个解决方案有一段时间了。但我注意到您引用的 form.php 文件是 magento 的 1.5 版本。在 1.6 版本中,文件有所不同...在 1.6 版本中将该代码放在哪里?谢谢。

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

I was looking for this solution for some time. But I noticed that the form.php file that you reference is the 1.5 version of magento. In version 1.6 the file is different ... Where to put this code in version 1.6? Thank you.

I found the file. It is in /app/code/core/Mage/Eav/Model/form.php.
But I put the code and did not work here ... I keep trying a solution.

小鸟爱天空丶 2025-01-14 23:06:13

您可以创建一个事件:

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

You can create an event :

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

And in your observer :

/**
     * 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文