问题
We would like to be able to pull the name and email address of customers who purchased items as a guest. Has anyone had any luck in doing so?
I've seen a few posts of how to pull the names of customers who have used the coupon code, but nothing about non-customers!
Thank you guys very much for your help!
-Jeff
回答1:
This should work:
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('customer_firstname')
->addAttributeToSelect('customer_lastname')
->addAttributeToSelect('customer_email')
->addAttributeToSelect('customer_group_id')
->addAttributeToFilter('customer_group_id', Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
;
You can then access the values by iterating over the collection...
foreach($orderCollection as $order) {
$customerFirstname = $order->getData('customer_firstname');
$customerLastname = $order->getData('customer_lastname');
$customerEmail = $order->getData('customer_email');
//Do what you want with the values here
}
EDIT:
The above code answers the first paragraph of your question. The second paragraph suggests you are looking for something different though. Are you looking for guest customers who have used a particular coupon code? If so then you should load the collection like this:
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('customer_firstname')
->addAttributeToSelect('customer_lastname')
->addAttributeToSelect('customer_email')
->addAttributeToSelect('customer_group_id')
->addAttributeToSelect('coupon_code')
->addAttributeToFilter('customer_group_id', Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)
->addAttributeToFilter('coupon_code', 'your_awesome_coupon_code')
;
回答2:
Drew gave me a SQL statement for names and emails of the existing customers in another similar post. To get all orders including registered users and guests you can use this SQL:
SELECT `customer_firstname`, `customer_lastname`, `customer_email` FROM `sales_flat_order` WHERE `coupon_code` = 'your_awesome_coupon_code'
Worked like a charm! Double-checked in 'salesrule_coupon' and the rows returned in the sql matches the number of times the coupon was used.
I will leave Drew's answer marked as correct, since it is probably better to do it in magento. Anyone looking for the SQL though, use this one!
The SQL statement for registered users only can be found here: How can I see full order details for each time a certain coupon code was used in magento?
来源:https://stackoverflow.com/questions/11144725/find-the-name-and-email-address-of-non-members-who-used-coupon-code-in-magento