Import Customers' Statistic Data From CSV to Magento Backend

Import Customers’ Statistic Data From CSV to Magento Backend

Hi, my name is Gon. Today, I would like to share with you guys an exciting task relating to importing data from CVS to Magento admin. My work is to add 4 customer items, load them from .xls file and show them in 4 fields in the backend.


Attribute

I have been fixing numerous sites recently, so I thought I’d share.

1. Even following the case “don’t have the module” unfortunately, my code wasn’t active. And I fixed it:

 <?php  
 $setup = Mage::getModel('customer/entity_setup', 'resourcename');  
 $setup->addAttribute('customer', 'school', array(  
   'type' => 'int',  
   'input' => 'select',  
   'label' => 'School',  
   'global' => 1,  
   'visible' => 1,  
   'required' => 0,  
   'user_defined' => 1,  
   'default' => '0',  
   'visible_on_front' => 1,  
     'source' => 'profile/entity_school',  
 ));  
 if (version_compare(Mage::getVersion(), '1.6.0', '<='))  
 {  
    $customer = Mage::getModel('customer/customer');  
    $attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();  
    $setup->addAttributeToSet('customer', $attrSetId, 'General', 'school');  
 }  
 if (version_compare(Mage::getVersion(), '1.4.2', '>='))  
 {  
   Mage::getSingleton('eav/config')  
   ->getAttribute('customer', 'school')  
   ->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))  
   ->save();  
 }  
 ?>  

I pasted the code in PHP file, uploaded to hosting and ran it. My new attribute was saved in the table ‘eav_attribute.’ Replaced by my values and I get in the backend.

attribute-2

2. Showing it in tab Personal Information

I had to edit a phtml file to show it, but I didn’t know what it is. I enabled the backend template hint by SQL query in phpmyadmin.

 INSERT INTO `core_config_data` (`scope`, `scope_id`, `path`, `value`)  
     VALUES ('websites', '0', 'dev/debug/template_hints', '1');  

My new attributes is customer_id, first_ultracast_orders, last_ultracast_order, ultracast_grand_total.

I can show it by call function $this->getCustomer()->getData(‘first_ultracart_order’); or this->getCustomer()->getFirst_ultracast_order();.
Note: you can be in trouble if your attribute names contain uppercase characters.

3. Editing customer’s data

I used this function to load customers data by email.

 function getCustomerByEmail($email) {   
 $customer=Mage::getModel("customer/customer");  
 $customer->setWebsiteId(Mage::app()->getWebsite()->getId());   
      $customer->loadByEmail($email);       
 }  

I added new customer values by this function.

 function update($attribute_id,$value) {       
 $customer->setData($attribute_id,$value)->save();  
 }  

Note: Customers’ personal information have to be loaded before setting data. If my attribute is ‘first_ultracart_order’ I can set with function setFirst_ultracart_order($value);

4. Loading from CSV I and update my customer’s data.

Gon

We welcome your queries and comments on our products and services.

Write A Comment

Name