2014-10-03 90 views
0

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

当客户提交此表单时,我需要检查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到另一个客户的副本

UPDATE

进一步研究它看起来像模块函数本身没有运行或它里面没有任何东西正在影响,因为无论我把它放在哪里都不起作用。事实上,默认行为是将displayname设置为不是我的模块。我的模块被激活,我的配置文件使用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> 
+0

与“加密”是什么?什么可以隐藏?你可能有一个案例问题,你宣布你的模块(又名应用程序/ etc/modules/Xxx_Xxxxx.xml)? – OSdave 2014-10-03 12:59:37

回答

0

这可能是因为提交坐落于Magento的客户模块的客户模型形式的所有数据(来自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); 

} 
+0

已经尝试过,它不工作,我已经回顾了代码,它似乎没有在模块的功能正在运行/使用。我是否使用了错误的事件?我已更新我的问题并提供更多信息 – 2014-10-03 12:36:05

+0

您确定您的模块处于活动状态吗?为此,您需要在'app/etc/modules'中放置一个YournameSpace_Yourmodule.xml文件(查看其他文件以获取正确的语法)。 此外,我认为'customer_save_commit_after'事件是错误的,因为数据已经保存在那里。我建议你改用'customer_save_before'。 如果你没有在配置中用“xxx”替换实际值,那么我们可以看看配置本身是否存在错误。 – Rinda 2014-10-03 12:55:28

+0

该模块绝对有效。它使用相同的设置作为一些其他自定义模块,所有工作,我可以看到它在管理>配置>高级启用。你知道我应该使用什么事件吗?我认为这不是事件的“前”版本? – 2014-10-03 12:57:34