Magento - 通过订单查询获取客户性别

时间:2014-09-25 10:15:06

标签: 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);

1 个答案:

答案 0 :(得分: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());
}