0
votes

Following a tutorial I'm trying to make an observer that changes the customer's group on registration/edit based on a custom field value. I can't seem to get the event observer to work, I guess it's a syntax thing since the tutorial is for 1.4 and I'm on 1.7. Any help would be apreciated:

/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

1 Answers

1
votes

Please change in the 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>

Also,I have changed the code in 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() );
        }
    }
}