2014-09-25 47 views
0

我已經提出了一個查詢來獲取客戶的訂單。我可以根據此查詢獲取客戶性別嗎?如果不是,我如何獲得客戶性別?這是我的代碼至今:Magento - 從訂單查詢中獲取客戶性別

<?php 

require_once('app/Mage.php'); //Path to Magento 
umask(0); 
Mage::app(); 

$productId = 297; 

$orders = Mage::getModel('sales/order')->getCollection(); 
$orders 
    ->getSelect() 
    ->joinLeft(
    array(
      'order_item' =>Mage::getSingleton('core/resource')->getTableName('sales/order_item')), 
      'order_item.order_id = main_table.entity_id', 
      array('product_id'=>'product_id')) 
    ->where('product_id=?', $productId) 
    ->group('main_table.entity_id') 
    ->order('main_table.entity_id DESC'); 

$fields = "date,company,name,country,city,address,postcode,itemname,quantity,price,sku \n"; 
foreach($orders as $order): 

    $order->getData(); 
    $order->getIncrementId(); 
    $billingAddress = $order->getBillingAddress(); 
    $shippingAddress = $order->getShippingAddress(); 
    $billingStreet = $billingAddress->getStreet(); 

    $countryCode = $billingAddress->getCountry(); 
    $country = Mage::getModel('directory/country')->loadByCode($countryCode); 

    $fields .= 
      date('Y-m-d',strtotime($order->getCreatedAt())).','. 
      $billingAddress->getCompany().','. 
      $billingAddress->getName().','. 
      $country->getName().','. 
      $billingAddress->getCity().','. 
      $billingStreet[0].','. 
      $billingAddress->getPostcode().','. 
      $order->getShippingDescription() .','; 

      foreach ($order->getAllItems() as $itemId => $item): 
       $itemname = $item->getName(); 
       $itemname = str_replace('&', " ", $itemname); 
       $fields .= 
        $itemname. ','. 
        $item->getQtyOrdered().','. 
        $item->getPrice().','. 
        $item->getSku().','; 

      endforeach; 
    $fields = rtrim($fields, ','); 
    $fields .= "\n"; 

endforeach; 

$name = date('d-m-Y-H-i-s'); 
$output = fopen('backend/assets/csv/' . $name . '.csv', 'w'); 
fwrite ($output,$fields); 
fclose ($output); 

回答

0
foreach($orders as $order){ 
    $customer = $order->getCustomer(); 
    Mage::log($customer->getGender()); 
} 

如果不工作,那麼:

foreach($orders as $order){ 
    $customer = $order->getCustomer(); 
    $customerObj = Mage::getModel('customer/customer')->load($customer->getEntityId()); 
    Mage::log($customerObj->getGender()); 
}