Magento 1.7 customer_save_before observer

时间:2014-02-18 11:55:27

标签: magento events magento-1.7

根据教程,我试图建立一个观察者,根据自定义字段值更改注册/编辑客户的组。我似乎无法让事件观察者工作,我想这是一个语法的东西,因为教程是1.4和我在1.7。任何帮助都会被贬低:

/app/etc/modules/Nonsintetic_All.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
      <Nonsintetic_Membership>
          <active>true</active>
          <codePool>local</codePool>
      </Nonsintetic_Membership>
  </modules>
</config>

/app/code/local/Nonsintetic/Membership/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Nonsintetic_Membership>
            <version>1.0</version>
        </Nonsintetic_Membership>
    </modules>
    <global>
        <events>
            <customer_save_before>
                <observers>
                    <nonsintetic_membership_save_observer>
                        <type>singleton</type>
                        <class>Nonsintetic_Membership_Model_Observer</class>
                        <method>customer_save_before</method>
                    </nonsintetic_membership_save_observer>
                </observers>
            </customer_save_before>
        </events>
    </global>
</config>

/app/code/local/Nonsintetic/Membership/Model/Observer.php

<?php

class Nonsintetic_Membership_Model_Observer extends Mage_Core_Model_Abstract
{
    /*
     * observer for the customer saved event
     */
    public function customer_save_before( $observer )
    {
        try {
            $customer = $observer->getCustomer();
                    Mage::log( "customer_save_before TRIGGERED" );
            if( $customer->getCardmembru() == "TEST123" ) {
                $customer->setData( 'group_id', 4 ); // Set the new customer group
            } else {
                $customer->setData( 'group_id', 1); // Set to the default customer group
            }
        } catch ( Exception $e ) {
            Mage::log( "customer_save_before observer failed: " . $e->getMessage() );
        }
    }
}

?>

1 个答案:

答案 0 :(得分:1)

请更改config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Nonsintetic_Membership>
            <version>1.0</version>
        </Nonsintetic_Membership>
    </modules>
    <global>
        <models>
            <membership>
        <class>Nonsintetic_Membership_Model</class>
        </membership>
        </models>
        <events>
            <customer_save_before>
                <observers>
                    <nonsintetic_membership_save_observer>
                        <type>singleton</type>
                        <class>membership/Observer</class>
                        <method>customer_save_before</method>
                    </nonsintetic_membership_save_observer>
                </observers>
            </customer_save_before>
        </events>
</global>
</config>

另外,我更改了Observer.php中的代码

class Nonsintetic_Membership_Model_Observer 
{
    /*
     * observer for the customer saved event
     */
    public function customer_save_before( $observer )
    {



        try {
            $customer = $observer->getCustomer();
//please check code in if condition
            if( $customer->getCardmembru() == "TEST123" ) {
                $customer->setData( 'group_id', 4 ); // Set the new customer group
            } else {
                $customer->setData( 'group_id', 1); // Set to the default customer group
            }
        } catch ( Exception $e ) {
            Mage::log( "customer_save_before observer failed: " . $e->getMessage() );
        }
    }
}