1

I have created custom EAV attribute for customer called branch which is select field customer need to select one branch which he/she belongs to and its working fine. Note: Customer can change the branch for each order he/she placed.

I have to store it in another table when customer place order with other customer attribute, so I used sales_order_place_after observer method. I am using

$orderInfo       = $observer->getEvent()->getOrder();

Where I can get all the information about order. I am getting all the information using this code.

$customerInfo = array(
            'customer_id'=> $orderInfo->getCustomerId(),
            'first_name' => $orderInfo->getBillingAddress()->getFirstname(),
            'last_name'  => $orderInfo->getBillingAddress()->getLastname(),
            'phone'      => $orderInfo->getBillingAddress()->getTelephone(),
            'email'      => $orderInfo->getCustomerEmail(),
            'address'    => $orderInfo->getBillingAddress()->getData('street'),
            'zip_code'   => $orderInfo->getBillingAddress()->getData('postcode'),
            'city'       => $orderInfo->getBillingAddress()->getCity(),
            'region'     => $orderInfo->getBillingAddress()->getRegion()
            );

But I am not able to get my custom attribute, I used many ways to get it as follows.

1. Mage::log(print_r($orderInfo->getCustomerBranch(), true));
2. Mage::log(print_r($orderInfo->getBranch(), true));
3. Mage::log(print_r($orderInfo->getBillingAddress()->getData('branch'), true));
4. Mage::log(print_r($orderInfo->getBillingAddress()->getBranch(), true));

Here is the code I used to create custom attribute

install.php code

<?php
$installer = $this;
$installer->startSetup();
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', 'branch', array(
    'type' => 'varchar',
    'input' => 'select',
    'label' => 'Branch',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'default' => NULL,
    'visible_on_front' => 1,
    'source' => 'customerbranch/entity_branch',
));
if (version_compare(Mage::getVersion(), '1.6.0', '<='))
{
      $customer = Mage::getModel('customer/customer');
      $attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
      $setup->addAttributeToSet('customer', $attrSetId, 'General', 'branch');
}
if (version_compare(Mage::getVersion(), '1.4.2', '>='))
{
    Mage::getSingleton('eav/config')
    ->getAttribute('customer', 'branch')
    ->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
    ->save();
}
$tablequote = $this->getTable('sales/quote');
$installer->run("
ALTER TABLE  $tablequote ADD  `customer_branch` varchar(255) NULL
");
$installer->endSetup();

Config.xml code

<global>
    <resources>
        <customerbranch_setup>
            <setup>
                <module>Npm_CustomerBranch</module>
                <class>Mage_Customer_Model_Entity_Setup</class>
            </setup>
    <connection>
        <use>core_setup</use>
    </connection>
        </customerbranch_setup>
    </resources>

    <fieldsets>
        <customer_branch>
            <branch><create>1</create><update>1</update><name>1</name></branch>
        </customer_branch>
    </fieldsets>
    <fieldsets>
        <checkout_onepage_quote>
            <customer_branch>
                <to_customer>branch</to_customer>
            </customer_branch>
        </checkout_onepage_quote>
        <customer_account>
            <branch>
                <to_quote>customer_branch</to_quote>
            </branch>
        </customer_account>
    </fieldsets>
    <fieldsets>
        <checkout_onepage_billing>
           <branch>
              <to_customer>*</to_customer>
           </branch>
        </checkout_onepage_billing>
    </fieldsets>
</global>

Nothing is working.:{ How can I get my attribute value in sales_order_place_after observer method?

Thanks in advance

  • How have you 'created custom EAV attribute'? – Jonathan Hussey Nov 17 '14 at 15:40
  • @JonathanHussey, I will add my install.php code – Charlie Nov 17 '14 at 15:44
0

You need to add a column customer_branch on the sales_flat_quote and sales_flat_order tables.
then add this in your config.xml under the fieldsets tag.

<customer_account>
    <branch>
        <to_quote>customer_branch</to_quote>
    </branch>
</customer_account>
<sales_convert_quote>
    <customer_branch>
        <to_order>*</to_order>
    </customer_branch>
</sales_convert_quote>

Clear the cache.
For the next orders you whould be able to get the branch like this $orderInfo->getCustomerBranch().

As a side effect, you will see the branch value in the order view page in the admin panel, below the name and e-mail address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.