Hello Devs, In this tutorial we will see how to get customer reward points balance programmatically in Magento 2.
In Magento 2, reward points are a powerful feature designed to encourage customer loyalty and engagement. They provide an incentive for customers to make repeat purchases and actively participate in your online store. Reward points can be earned by customers through various actions and can be redeemed for discounts, free products, or other incentives.
You can load reward points information of customers by rewardFactory using Magento\Reward\Model\RewardFactory class.
Here is an example of how you can retrieve customer reward points balance programmatically.
<?php
namespace VendorName\ModuleName\Helper;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Reward\Model\RewardFactory;
class Data {
/** @var CustomerRepositoryInterface */
private $customerRepository;
/** @var RewardFactory */
private $rewardFactory;
public function __construct(
CustomerRepositoryInterface $customerRepository,
RewardFactory $rewardFactory
) {
$this->customerRepository = $customerRepository;
$this->rewardFactory = $rewardFactory;
}
public function getRewardPointBalance()
{
$customerId = 1;
$customerEmail = 'jigarkkarangiya@gmail.com';
/* Load Customer by Id */
$customer = $this->customerRepository->getById($customerId);
/* Load customer by email */
//$customer = $this->customerRepository->get($customerEmail);
$reward = $this->rewardFactory->create();
$reward->setCustomerId($customer->getId());
$reward->setWebsiteId($customer->getWebsiteId());
$reward->loadByCustomer();
/*[
"customer_id" => "1",
"website_id" => "1",
"reward_id" => "1",
"points_balance" => "2099",
"website_currency_code" => null,
]*/
return $reward->getPointsBalance();
}
}
As shown in the above example you can retrieve the customer’s reward points balance and some other information mentioned like customer id, website id, reward id, balance, etc.
You may also like this :
How to assign a customer to a company programmatically in Magento 2 B2B?
Get Company using Customer ID programmatically Magento 2 B2B
How to delete file in magento 2 programmatically
That’s it for this tutorial. Happy Coding !!
Leave a Comment