0

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 1

0

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;
    }
}
5
  • 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, 2023 at 14:45
  • Then you need to specify the specific order ID
    – Rana Zain
    May 15, 2023 at 14:46
  • But before placing order, how you will get reward points data?? May 15, 2023 at 14:48
  • Something like this: You would earn 4 Reward points from this order. May 15, 2023 at 14:49
  • If there was already rewards points then it will be possilble to get the rewards points before order placed.
    – Rana Zain
    May 16, 2023 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.