Magento – PHP – How to get custom customer attribute in order

event-observerPHP

i am using Magento 1.9.0.1 and i am developing a custom extension.

Right now i am stuck at the following thing.

Like this i use to get the customers name from an order:

$CustomerName = $observer->getOrder()->getBillingAddress()->getName();

I have custom customer attribute called sms_on_order_change and i am trying to get the value of it like that:

$SMSOnStatusChange = $observer->getResource()->getAttribute('sms_on_order_change')->getFrontend()->getValue();

But it is not working.

Here is my config file:

<?xml version="1.0"?>
<config>
  <modules>
    <VivasIndustries_SmsNotification>
      <version>1.0.0</version>
    </VivasIndustries_SmsNotification>
  </modules>
  <global>
    <models>
        <smsnotification>
            <class>VivasIndustries_SmsNotification_Model</class>
            <resourceModel>vivasindustries_smsnotification_resource</resourceModel>
        </smsnotification>
        <vivasindustries_smsnotification_resource>
        <class>VivasIndustries_SmsNotification_Model_Resource</class>
        <entities>
            <smsnotification>
            <table>VivasIndustries_SmsNotification</table>
            </smsnotification>
            <smsnotificationhistory>
            <table>VivasIndustries_SmsHistory</table>
            </smsnotificationhistory>
        </entities>
        </vivasindustries_smsnotification_resource>
    </models>
    <resources>
        <smsnotification_setup>
            <setup>
                <module>VivasIndustries_SmsNotification</module>
            </setup>
            <connection>
                 <use>core_setup</use>
             </connection>
        </smsnotification_setup>
        <smsnotification_read>
            <connection>
                <use>core_read</use>
            </connection>
        </smsnotification_read>
        <smsnotification_write>
            <connection>
                <use>core_write</use>
            </connection>
        </smsnotification_write>
    </resources>    
    <events>
        <sales_order_save_after>
            <observers>
                <vivasindustries_smsnotification>
                    <class>smsnotification/observer</class>
                    <method>orderSaved</method>
                </vivasindustries_smsnotification>
            </observers>
        </sales_order_save_after>
    </events>
    <helpers>
        <smsnotification>
            <class>VivasIndustries_SmsNotification_Helper</class>
        </smsnotification>
    </helpers>
    <blocks>
        <smsnotification>
             <class>VivasIndustries_SmsNotification_Block</class>
        </smsnotification>
    </blocks>
  </global>
  <adminhtml>
    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <vivas>
                                        <title>Vivas - All</title>
                                    </vivas>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
    <layout>
        <updates>
            <smsnotification>
                <file>smsnotification.xml</file>
            </smsnotification>
        </updates>
    </layout>   
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <VivasIndustries_SmsNotification before="Mage_Adminhtml">VivasIndustries_SmsNotification_Adminhtml</VivasIndustries_SmsNotification>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>  

Here is the extension that i am using for the creation of custom customer attributes: http://www.magentocommerce.com/magento-connect/manage-customer-attributes.html

So guys, can you help me out so i can get the value of this custom attribute ?

Best Answer

It's very easy but not simple to understand.

First at all you should create the attribute for a customer. After it check with this code

$customer   = Mage::getModel('customer/customer')->load($customerId);
$customerAttr = $customer->getSmsOnOrderChange();

Then you should update tables sales_flat_quote and sales_flat_order, please add a new column to the tables 'sms_on_order_change'.

Now some magic in your module!

In config.xml you should add this code:

<global>
    <fieldsets>

        <customer_account>
            <sms_on_order_change>
                <to_quote>*</to_quote>
            </sms_on_order_change>
        </customer_account>

        <sales_convert_quote>
            <sms_on_order_change>
                <to_order>*</to_order>
            </sms_on_order_change>
        </sales_convert_quote>

        <sales_convert_order>
            <sms_on_order_change>
                <to_quote>*</to_quote>
            </sms_on_order_change>
        </sales_convert_order>

    </fieldsets>
</global>

No events, no observer. It doesn't require.

Related Topic