4

I'm working with Magento ver. 1.9.1.1. and I need to update store credit balance for a customer. I know that it's possible to do it in Magento admin interface, but in my case I need to do an HTTP request to a server and actually do the same manipulations as we can do via Magento admin interface.

On the internet I found a code which allows to create a Credit Memo. Does I have to create a credit Memo to update customer store credit balance or it's not necessary?

Does someone has any idea how to do it?

I appreciate any answers. Thank you.

Kalitine
  • 377
  • 1
  • 4
  • 12

2 Answers2

6

Try this

$balance = Mage::getModel('enterprise_customerbalance/balance')
                    ->setCustomer($customer)
                    ->setWebsiteId($websiteId)
                    ->setAmountDelta($anyNumber)
                    ->setComment($data['comment']);

$balance->save();

take more look at function customerSaveAfter() in observer of customerBalance module

BlueWonder
  • 78
  • 6
  • Thank you very much. It works. Just setComment function does not want to work. I'll see why and will post here the fix. – Kalitine Jun 12 '13 at 08:40
  • I still can not make work "setComment" function. When I execute the code all works great, but I just do not have any comment for the action. Do you have any idea why? maybe it is not a good function... – Kalitine Jun 19 '13 at 02:19
  • 1
    I think for setComment to work you also need to declare who is updating the record which is not stated here. When I do this on the Admin side it lists the uid of who is updating plus the comment. – Greg Demetrick Jun 24 '13 at 18:32
  • 1
    Thank you very much Greg. Your tip really helped me. I use code from [here](http://dineshinmca.blogspot.jp/2012/10/magento-how-to-login-to-magento-admin.html) to login to magento admin before update balance and after that comment is set properly. I hope this may be useful for someone else too. – Kalitine Jul 01 '13 at 01:47
  • Instead of `setComment` use `setUpdatedActionAdditionalInfo` – Joshua Pack May 28 '15 at 18:54
4

This will work perfectly

$balance = Mage::getModel('enterprise_customerbalance/balance');
$balance->setCustomerId($customer_id)
        ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
        ->loadByCustomer();

$current_balance = $balance->getAmount();
$comment = 'This is a comment that will appear in the credit update history';

// add store credit
$balance->setAmount($current_balance);
$balance->setAmountDelta($amount_to_be_added);
$balance->setUpdatedActionAdditionalInfo($comment);
$balance->setHistoryAction(1); // 1= updated
$balance->save();
Shadoweb
  • 5,812
  • 1
  • 42
  • 55
user2965205
  • 141
  • 2
  • 7