1

In Magento 2 Enterprise edition how can i add the reward point for a customer?

Thank you very much.

1

For example we want to assign the reward points when customer save event (customer_save_after_data_object) observer is called then it will be like this

namespace Aizar\CustomerStatus\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Reward\Model\Reward;
use Magento\Reward\Model\Reward\HistoryFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Reward\Model\RewardFactory;

class CustomerSaveObserver implements ObserverInterface
{

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * Reward factory
     *
     * @var \Magento\Reward\Model\RewardFactory
     */
    protected $_rewardFactory;


    /**
     * Store manager
     *
     * @var StoreManagerInterface
     */
    protected $_storeManager;


    public function __construct(RewardFactory $rewardFactory,
                                StoreManagerInterface $storeManager,
                                ManagerInterface $messageManager)
    {
        $this->messageManager = $messageManager;
        $this->_rewardFactory = $rewardFactory;
        $this->_storeManager = $storeManager;
    }

    /**
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        /* @var $reward \Magento\Reward\Model\Reward */
        $newCustomerData = $observer->getCustomerDataObject();
        $websiteId = $this->_storeManager->getWebsite()->getId();
        $reward = $this->_rewardFactory->create()->setCustomer($newCustomerData)
                       ->setCustomerId($newCustomerData->getId())
                       ->setWebsiteId($websiteId)
                       ->loadByCustomer()
                       ->setAction(\Magento\Reward\Model\Reward::REWARD_ACTION_ADMIN)
                    ->setComment(__('Additional Information Reward'))
                    ->updateRewardPoints();
     $this->messageManager->addNoticeMessage(__('You have got %1 reward pints', $reward->getPointsDelta()));

    }

}

I hope it will help someone. Please vote up my answer if it will help you. Thank you very much.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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