How to get a customer's store credit balance in Magento using PHP

Showing customer's store credit balance in the Magento header.

Getting a customer's store credit balance in Magento with Magecredit using PHP is simple. Here's some sample code that gets the store credit balance in the BASE currency of the store for the customer with the ID #321 in the website with the ID #1 (default website).

            $balance = Mage::getModel('wf_customerbalance/balance')
                ->setCustomerId(321)
                ->setWebsiteId(1)
                ->loadByCustomer();
            echo "This customer's store credit balance is {$balance->getAmount()}.";
            // Output: This customer's store credit balance is 123.00.
        

Get customer's store credit balance for the current website

If you want to get the customer balance for the customer in the current website that you are in (in the frontend), then this is the code:

            $currentWebsiteId = Mage::app()->getWebsite()->getId();
            $balance = Mage::getModel('wf_customerbalance/balance')
                ->setCustomerId(321)
                ->setWebsiteId($currentWebsiteId)
                ->loadByCustomer();
            echo "This customer's store credit balance for this website is {$balance->getAmount()}.";
            // Output: This customer's store credit balance for this website is 123.00.
        

Get formatted store credit balance

Let's say you want the balance formatted to the currency of the current store view.

            $balanceFormatted = Mage::helper('core')->currency($balance->getAmount(), true, false);
            echo "This customer's store credit balance (formatted) is {$balanceFormatted}.";
            // Output: This customer's store credit balance (formatted) is $123.00 in a USA store.
        

Get store credit for currently logged-in customer

Let's say you want the store credit balance for the currently logged-in customer in Magento. Here's what you need to do:

            $currentCustomerId = Mage::getSingleton('customer/session')->getCustomerId();
            $balance = Mage::getModel('wf_customerbalance/balance')
                ->setCustomerId($currentCustomerId)
                ->setWebsiteId($currentWebsiteId)
                ->loadByCustomer();
            echo "This current customer's store credit balance is {$balance->getAmount()}.";
            // Output: The current customer's store credit balance is 123.00.
        

Alltogether now...

So let's put it alltogether. We want to output the currently logged-in customer's current store credit balance for the current website, in the current store's currency. Here's how we do it:

            $currentCustomerId = Mage::getSingleton('customer/session')->getCustomerId();
            $currentWebsiteId = Mage::app()->getWebsite()->getId();
            $balance = Mage::getModel('wf_customerbalance/balance')
                ->setCustomerId($currentCustomerId)
                ->setWebsiteId($currentWebsiteId)
                ->loadByCustomer();
            $balanceFormatted = Mage::helper('core')->currency($balance->getAmount(), true, false);
            echo "The store credit balance of the current customer in the current store's currency for the current website is {$balanceFormatted}.";
            // Output: The store credit balance of the current customer in the current store's currency for the current website is $123.00.
        

Or if you're working your templates we can break this up into 2 parts:

(1/2) Get the current customer's balance in a PHTML file

Put this at the top of your PHTML file, just after the comments but before the closing ?> tag.

            /* BEGIN magecredit.com store credit balance code 1/2 */
            if (Mage::helper('core')->isModuleEnabled("Wf_CustomerBalance")) {
                $currentCustomerId = Mage::getSingleton('customer/session')->getCustomerId();
                if ($currentCustomerId) {
                    $currentWebsiteId = Mage::app()->getWebsite()->getId();
                    $balance = Mage::getModel('wf_customerbalance/balance')
                        ->setCustomerId($currentCustomerId)
                        ->setWebsiteId($currentWebsiteId)
                        ->loadByCustomer();
                    $_balanceFormatted = Mage::helper('core')->currency($balance->getAmount(), true, false);
                } else {
                    $_balanceFormatted = "";
                }
            } else {
                $_balanceFormatted = "";
            }
            /* END magecredit.com store credit balance code 1/2 */
        

(2/2) Display Customer's Balance in a PHTML file

            <?php /* BEGIN magecredit.com store credit balance code 2/2 */ ?>
            <?php if (Mage::getSingleton('customer/session')->isLoggedIn()): ?>
                <?php echo $this->__("Credit Balance:"); ?>
                <a href="<?php echo $this->getUrl('storecredit/info'); ?>">
                    <?php echo $_balanceFormatted; ?>
                </a>
            <?php endif; ?>
            <?php /* END magecredit.com store credit balance code 2/2 */ ?>
        

Show customer's store credit balance in the header

If you're using the standard Magento theme (or a theme based on that) you can use this sample header.phtml file code to output the customer's store credit balance after the welcome message.

If you're using the rwd theme (or a theme based on that) you can use this sample header.phtml file code to output the customer's store credit balance after the welcome message.