Magento – How to get Customer gender from customer id

magento-1.7magento-1.8magento-1.9MySQLPHP

I tried to extract customer System->Import/Export->dataflow-Profles, due to large no of data it will stop the running and did not retrieve any csv file in the export folder.

I manually tried to get the data by a query,below is my approach for the result.

//resources 
$resource = Mage::getSingleton('core/resource');
// db access
$db_read = $resource->getConnection('core_read');
$db_write = $resource->getConnection('core_write');
// support table prefix if one is being used
$table_prefix = Mage::getConfig()->getTablePrefix();
// count the customers
$numberof_customers = $db_read->fetchOne("SELECT COUNT(*) AS num FROM {$table_prefix}customer_entity WHERE is_active = 1");

$customers = $db_read->fetchAll("SELECT cus.* FROM {$table_prefix}customer_entity AS cus WHERE cus.is_active = '1'");

for($i=0; $i < intval($numberof_customers); $i++){

  $entity_id = $customers[$i]['entity_id'];
  $items = $db_read->fetchAll("Select * from {$table_prefix}customer_entity as CE 
           JOIN {$table_prefix}customer_entity_varchar as CEV
           ON CE.entity_id=CEV.entity_id
           WHERE CEV.attribute_id=162
           AND CE.entity_id=$entity_id");//where 162 is one of attribute id for my selection
foreach ($items AS $item) {
  $cus_id =  $item['entity_id'];
  $obj = Mage::getModel('customer/customer');
  $_custom = $obj->load($cus_id);
  echo $_custom->getId()."-".$_custom->getName()."-".$_custom->getPrimaryBillingAddress()->getCity()."-".$_custom->getEmail()."-".$_custom->getMobile();

My question is , i want to retrieve gender for the specific customer in my out put.
How can i retrieve gender?
Great thanks for your help.. 🙂

Best Answer

Found here

Try this:

$this->htmlEscape($this->__(Mage::getResourceSingleton('catalog/customer')->getAttribute('gender')->getSource()->getOptionText($_custom->getGender()))))