Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update customer store credit programmatically

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.

like image 802
Kalitine Avatar asked Dec 27 '22 02:12

Kalitine


2 Answers

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

like image 195
BlueWonder Avatar answered Jan 10 '23 14:01

BlueWonder


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();
like image 21
user2965205 Avatar answered Jan 10 '23 15:01

user2965205