Magento - 检查客户属性是否已存在

时间:2014-10-03 10:33:37

标签: magento attributes

我有一个在customer_save_observer_executed事件中调用的自定义模块,特别是当客户在其帐户页面(名称,密码,电子邮件等)中更新其详细信息时,我添加了一个名为display_name的自定义属性。

当客户提交此表单时,我需要检查当前是否存在任何其他客户的display_name。如果没有,则setDisplayName(...)否则不执行任何操作/显示错误消息。

我希望用这个片段做到这一点:

$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....
}

我目前在模型中的代码 - > 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); 

    }
}

但这只是更新了display_name,即使我故意将其设置为其他客户的副本

更新

进一步研究它看起来模块函数本身没有被运行或者它内部没有任何东西正在生效,因为我放入它的任何东西都不起作用。实际上,默认行为是将displayname设置为my module。我的模块已激活,我的配置文件使用customer_save_commit_after事件,如下所示:

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

1 个答案:

答案 0 :(得分:0)

这可能就是这种情况,因为提交表单中的所有数据都是在Magento的Customer模块中的客户模型上设置的(代码取自Mage_Customer_AccountController

/**
 * Forgot customer account information page
 */
public function editAction()
{
    $this->loadLayout();
    $this->_initLayoutMessages('customer/session');
    $this->_initLayoutMessages('catalog/session');

    $block = $this->getLayout()->getBlock('customer_edit');
    if ($block) {
        $block->setRefererUrl($this->_getRefererUrl());
    }
    $data = $this->_getSession()->getCustomerFormData(true);
    $customer = $this->_getSession()->getCustomer();
    if (!empty($data)) {
        $customer->addData($data);
    }
    if ($this->getRequest()->getParam('changepass') == 1) {
        $customer->setChangePassword(1);
    }

    $this->getLayout()->getBlock('head')->setTitle($this->__('Account Information'));
    $this->getLayout()->getBlock('messages')->setEscapeMessageFlag(true);
    $this->renderLayout();
}

这里在客户模型上调用addData()。为了防止这种情况将表单的输入重命名为临时的,例如<input name="display_name_tmp"/>,然后在控制器中执行类似

的操作
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_tmp'])){

            $current_name = $customer->getDisplayName();
            $new_name = $postData['display_name_tmp'];

            // 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_tmp']);
                }

            }

        }

    }

    Mage::register('customer_save_observer_executed',true); 

}