How to give customers store credit with PHP in Magento

Issuing store credit in Magento using magecredit is extremely simple and very similar to the way you'd do it with Magento Enterprise. We kept it close to the Magento Enterprise style so that you wouldn't have to do much different coding to support both Magento enterprise and Magecredit for your store credit functionality.

Isusing store credit with PHP code

Here's a sample code that issues 123.00 (in the website's base currency) to the first customer in the default website.

            // Get the first customer in the store's ID
            $firstCustomerId = Mage::getModel('customer/customer')->getCollection()->getFirstItem()->getId();

            $balance = Mage::getModel('wf_customerbalance/balance')
                ->setCustomerId($firstCustomerId)
                ->setWebsiteId(Mage::app()->getWebsite()->getId())
                ->setAmountDelta(123.00)
                ->setUpdatedActionAdditionalInfo("This was a test to see how it easy it is to add store credit to a customer's account."); // This field is optional but recomemnded.
            
            $balance->save();
        

Deducting store credit from a customer's account with PHP code

Here's a sample code that subtracts 123.00 (in the website's base currency) from a customer's account. The ID of the customer we're dealing with is the first customer in the default website.

        // Get the first customer in the store's ID
        $firstCustomerId = Mage::getModel('customer/customer')->getCollection()->getFirstItem()->getId();

        $balance = Mage::getModel('wf_customerbalance/balance')
            ->setCustomerId($firstCustomerId)
            ->setWebsiteId(Mage::app()->getWebsite()->getId())
            ->setAmountDelta(-123.00) // Notice the negative
            ->setUpdatedActionAdditionalInfo("This was a test to see how it easy it is to deduct store credit to a customer's account."); // This field is optional but recomemnded.
        
        $balance->save();