In Magento 2.4, I want to get the Reward-points which going to earn for particular order from cart page into custom API. How to get Reward Points data in Custom API?
1 Answer
Hope this will help you:
In the etc/webapi.xml
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/rewards/points/:customerId" method="GET">
<service class="Vendor\Module\Api\RewardPointsInterface" method="getPoints"/>
<resources>
<resource ref="self"/>
</resources>
<data>
<parameter name="customerId" force="true">%customerId%</parameter>
</data>
</route>
</routes>
In the Vendor/Module/Api/RewardPointsInterface.php
<?php
namespace Vendor\Module\Api;
interface RewardPointsInterface
{
/**
* Get customer reward points
*
* @param int $customerId
* @return int
*/
public function getPoints($customerId);
}
In the Vendor/Module/Model/RewardPoints.php:
<?php
namespace Vendor\Module\Model;
use Vendor\Module\Api\RewardPointsInterface;
use Magento\Customer\Model\CustomerFactory;
use Magento\Reward\Model\ResourceModel\Reward\History\CollectionFactory;
class RewardPoints implements RewardPointsInterface
{
private $customerFactory;
private $rewardHistoryCollectionFactory;
public function __construct(
CustomerFactory $customerFactory,
CollectionFactory $rewardHistoryCollectionFactory
) {
$this->customerFactory = $customerFactory;
$this->rewardHistoryCollectionFactory = $rewardHistoryCollectionFactory;
}
/**
* Get customer reward points
*
* @param int $customerId
* @return int
*/
public function getPoints($customerId)
{
$customer = $this->customerFactory->create()->load($customerId);
$rewardPoints = $this->rewardHistoryCollectionFactory->create()
->addFieldToFilter('customer_id', $customer->getId())
->getFirstItem();
$pointsBalance = $rewardPoints->getPointsBalance();
return $pointsBalance;
}
}
-
Hi @Rana, But the points will be added after invoice generation and also this
getFirstItem
will give last received point. But I need the points for particular order- before placing order, in cart page. May 15 at 14:45 -
-
-
-
If there was already rewards points then it will be possilble to get the rewards points before order placed. May 16 at 5:51