Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Customer Name for Coupon Codes in Magento

Tags:

php

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

like image 450
Daniel Waser Avatar asked Dec 06 '25 10:12

Daniel Waser


2 Answers

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();
    }
}
like image 94
Sylvain Rayé Avatar answered Dec 07 '25 23:12

Sylvain Rayé


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.

like image 21
Keenora Fluffball Avatar answered Dec 08 '25 00:12

Keenora Fluffball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!