Get Customer Name for Coupon Codes in Magento
I have searched on the net and on this site for a solution but couldn't find any. I would like to get the customer name associated to a coupon code in Magento. I found some code for the times the coupon code was used but I couldn't get the customer name.
I tried the following code:
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app( 'admin' );
$coupon = Mage::getModel('salesrule/coupon/usage');
$coupon->load('dd_38WX9N', 'code');
if($coupon->getId()) {
$timesUsed = $coupon->getTimesUsed();
$customer = $coupon->getCustomerId();
echo $timesUsed;
echo $customer;
}
?>
Could someone please help me with that and开发者_如何转开发 point me to the right direction.
Thanks a lot for your help. Daniel
Here is the good solution. you need to use the resource and not the model to get access to the table salesrule_coupon_usage and get the customer_id list
/*@var $coupon Mage_SalesRule_Model_Coupon */
$coupon = Mage::getModel('salesrule/coupon');
$coupon->load('dd_38WX9N', 'code');
if($coupon->getId()){
/* @var $resourceCouponUsage Mage_SalesRule_Model_Mysql4_Coupon_Usage */
$resourceCouponUsage = Mage::getResourceModel('salesrule/coupon_usage');
$read = $resourceCouponUsage->getReadConnection();
$select = $read->select()
->from($resourceCouponUsage->getMainTable())
->where('coupon_id = ?', $coupon->getId());
$data = $read->fetchAll($select);
print_r($data);
foreach($data as $couponUsage){
$customer = Mage::getModel('customer/customer')->load($couponUsage['customer_id']);
echo $customer->getName();
}
}
When you are able to get the ID of the customer, you also can get the whole customer object:
$customer_data = Mage::getModel('customer/customer')->load($customer_id);
print_r($customer_data);
There you can get the name as well.
精彩评论