Product Alert API: Get Magento 2 Product Alert Using API

In this article, I am going to explain how we can get the notification of product alerts of subscribed products. I am explaining the steps to create a custom module for product alert so by installing this module you will get product notification for customers subscribing to out-of-stock products through REST API.

Product Alert API in Magento 2

How to Override Block, Controller, and Model in Magento 2

Wishusucess Magento 2 Product Alerts custom API extension is a  solution to keep in touch with customers about their products and make them notified by using this module. So when you will change any information about the product availability and price changes then this module sends the notification to the user.

This is a great Magento 2 extension to maintain a good relationship between the customer and the store owner. This will also help you to improve your shopping experience and especially contribute to increasing your product selling conversion rates.

 

Magento 2 Product Alert Through REST API

There are some basic steps to create Magento 2 Product alert API. In Magento2 customers can see the newly added product alert notification. If the customer can subscribe to the out-of-stock products then when the product will be available the customer gets notifications when back in stock.

Wishusucess/ProductAlertApi/registration.php

Wishusucess/ProductAlertApi/etc/module.xml

Wishusucess/ProductAlertApi/etc/di.xml

Wishusucess/ProductAlertApi/etc/webapi.xml

Wishusucess/ProductAlertApi/Model/ProductAlert.php

Wishusucess/ProductAlertApi/Api/ProductAlertManagementInterface.php

Factory Class: What is Factory Design Pattern in Magento 2

 

Step 1: Create Wishusucess_ProductAlertApi

<?php
/**
* Category: Wishusucess Product Alert API 
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_ProductAlertApi',
__DIR__
);

 

Step 2: Module Information File

<?xml version="1.0"?>
<!--
/**
* Category: Wishusucess Product Alert API 
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Wishusucess_ProductAlertApi" setup_version="1.0.4" />
</config>

 

Step 3: Dependency Injection File

So when we add custom functionality then this file helps us to change the existing functionality in Magento 2. So this file contains the information of existing classes which refer to the newly added classes.

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Category: Wishusucess Product Alert API 
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Wishusucess\ProductAlertApi\Api\ProductAlertManagementInterface" type="Wishusucess\ProductAlertApi\Model\ProductAlert"/>
</config>

 

Step 4: Route URL for Product Alert API

I have defined the product alert API endpoint in the Wishusucess/ProductAlertApi/etc/webapi.xml file.

So when we need to add the product then we have /V1/productalertstock/add/:productId and if we need to delete the product then we can use /V1/productalertstock/delete/:productId.

The complete URL will be

http://wishusucess.com/rest/V1/productalertstock/add/productid.

So when we need to access the data of the Magento 2 store then we have to create the customer token then only we can access the data.

http://wishusucess.com/rest/V1/integration/customer/token
<?xml version="1.0" ?>
<!--
/**
* Category: Wishusucess Product Alert API 
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<!-- Add product stock alert start-->
<route url="/V1/productalertstock/add/:productId" method="POST">
<service class="Wishusucess\ProductAlertApi\Api\ProductAlertManagementInterface" method="addProductAlertStock"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
<!-- Add product stock alert ends-->
<!-- Delete product stock alert start-->
<route url="/V1/productalertstock/delete/:productId" method="POST">
<service class="Wishusucess\ProductAlertApi\Api\ProductAlertManagementInterface" method="deleteProductAlertStock"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
<!-- Delete product stock alert ends-->
<!-- Add product price alert start-->
<route url="/V1/productalertprice/add/:productId" method="POST">
<service class="Wishusucess\ProductAlertApi\Api\ProductAlertManagementInterface" method="addProductAlertPrice"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
<!-- Add product price alert ends-->
<!-- Delete product price alert start-->
<route url="/V1/productalertprice/delete/:productId" method="POST">
<service class="Wishusucess\ProductAlertApi\Api\ProductAlertManagementInterface" method="deleteProductAlertPrice"/>
<resources>
<resource ref="self" />
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
</route>
<!-- Delete product price alert ends-->
</routes>

 

Step 5: Model Class Product Alert API

<?php
/**
* Category: Wishusucess Product Alert API 
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
namespace Wishusucess\ProductAlertApi\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\ProductAlert\Model\StockFactory;
use Magento\ProductAlert\Model\PriceFactory;

use Pawan\ProductAlertApi\Api\ProductAlertManagementInterface;

class ProductAlert implements ProductAlertManagementInterface
{
protected $productRepository;
private $storeManager;
protected $stockFactory;
protected $priceFactory;

public function __construct(
ProductRepositoryInterface $productRepository,
StoreManagerInterface $storeManager,
StockFactory $stockFactory,
PriceFactory $priceFactory
) {
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
$this->stockFactory = $stockFactory;
$this->priceFactory = $priceFactory;
}

public function addProductAlertStock($customerId, $productId)
{
try {

/* @var $product \Magento\Catalog\Model\Product */
$product = $this->productRepository->getById($productId);
$store = $this->storeManager->getStore();
/** @var \Magento\ProductAlert\Model\Stock $model */
$model = $this->stockFactory->create()
->setCustomerId($customerId)
->setProductId($product->getId())
->setWebsiteId($store->getWebsiteId())
->setStoreId($store->getId());
$model->save();
return true;
} catch (NoSuchEntityException $noEntityException) {
return false;
}
}
public function deleteProductAlertStock ($customerId, $productId)
{
try {
/* @var $product \Magento\Catalog\Model\Product */
$product = $this->productRepository->getById($productId);
$model = $this->stockFactory->create()
->setCustomerId($customerId)
->setProductId($product->getId())
->setWebsiteId(
$this->storeManager
->getStore()
->getWebsiteId()
)->setStoreId(
$this->storeManager
->getStore()
->getId()
)
->loadByParam();
if ($model->getId()) {
$model->delete();
}
return true;
} catch (NoSuchEntityException $noEntityException) {
return false;
}
}
public function addProductAlertPrice($customerId, $productId)
{
try {

/* @var $product \Magento\Catalog\Model\Product */
$product = $this->productRepository->getById($productId);
$store = $this->storeManager->getStore();
/** @var \Magento\ProductAlert\Model\Price $model */
$model = $this->priceFactory->create()
->setCustomerId($customerId)
->setProductId($product->getId())
->setPrice($product->getPrice())
->setWebsiteId($store->getWebsiteId())
->setStoreId($store->getId());
$model->save();
return true;
} catch (NoSuchEntityException $noEntityException) {
return false;
}
}
public function deleteProductAlertPrice ($customerId, $productId)
{
try {
/* @var $product \Magento\Catalog\Model\Price */
$product = $this->productRepository->getById($productId);
$model = $this->priceFactory->create()
->setCustomerId($customerId)
->setProductId($product->getId())
->setWebsiteId(
$this->storeManager
->getStore()
->getWebsiteId()
)->setStoreId(
$this->storeManager
->getStore()
->getId()
)
->loadByParam();
if ($model->getId()) {
$model->delete();
}
return true;
} catch (NoSuchEntityException $noEntityException) {
return false;
}
}

}

 

Step 6: Product Alert Management Interface

<?php
/**
* Category: Wishusucess Product Alert API 
* Developer: Hemant Singh Magento 2x Developer
* Website: http://wwww.wishusucess.com
*/
namespace Wishusucess\ProductAlertApi\Api;
use Exception;

/**
* Interface ProductAlertManagementInterface
* @api
*/
interface ProductAlertManagementInterface
{
/**
* Return true if product Added to Alert.
*
* @param int $customerId
* @param int $productId
* @return bool true on success
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function addProductAlertStock($customerId, $productId);
/**
* Return true if product Added to Alert.
*
* @param int $customerId
* @param int $productId
* @return bool true on success
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function deleteProductAlertStock($customerId, $productId);
/**
* Return true if product Added to Alert.
*
* @param int $customerId
* @param int $productId
* @return bool true on success
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function addProductAlertPrice($customerId, $productId);
/**
* Return true if product Added to Alert.
*
* @param int $customerId
* @param int $productId
* @return bool true on success
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function deleteProductAlertPrice($customerId, $productId);
}

GitHub

 

Recommended Post:

Magento 2 REST API: How To Call REST API in Magento 2

Category ID: How To Get Current Category ID in Magento 2