[英]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.
我想获得与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
您需要使用资源而不是模型来访问salesrule_coupon_usage表并获取customer_id列表
/*@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: 当您能够获得客户的ID时,您还可以获得整个客户对象:
$customer_data = Mage::getModel('customer/customer')->load($customer_id);
print_r($customer_data);
There you can get the name as well. 那里你也可以得到这个名字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.