In Magento2 customers can subscribe to the out-of-stock
products and get notifications when back in stock.
I would like to know how can a customer subscribe to out-of-stock products through REST API
.
-
I can't see any api available. Also there is no code available related to api in module-product-alert. So, probably you will have to create custom api for it.– Rahul BarotCommented May 12, 2021 at 7:08
-
@RahulBarot Can you help to create an API for it?– Reshad ZazaiCommented May 12, 2021 at 7:24
2 Answers
You need to create a custom API.For Implement Custom API, We need to create a custom Module.
Description: We have define our API end point in etc/webapi.xml
file. For Add product we have /V1/productalertstock/add/:productId
and for delete /V1/productalertstock/delete/:productId
. The complete URL will be http://{Magento2Root}/rest/V1/productalertstock/add/123
For Accessing above API You need Customer token, which can be get by http://{Magento2Root}/rest/V1/integration/customer/token
Where:
- 123 is the product ID
- {Magento2Root} is root folder/Url of magento 2
Create folowing File OR module can be downloaded from GitHub
app/code/Pawan/ProductAlertApi/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Pawan_ProductAlertApi',
__DIR__
);
/var/www/html/magento237/app/code/Pawan/ProductAlertApi/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Pawan_ProductAlertApi" setup_version="1.0.2" />
</config>
app/code/Pawan/ProductAlertApi/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Pawan\ProductAlertApi\Api\ProductAlertManagementInterface" type="Pawan\ProductAlertApi\Model\ProductAlert"/>
</config>
app/code/Pawan/ProductAlertApi/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">
<!-- Add product stock alert start-->
<route url="/V1/productalertstock/add/:productId" method="POST">
<service class="Pawan\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="Pawan\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-->
</routes>
app/code/Pawan/ProductAlertApi/Api/ProductAlertManagementInterface.php
<?php
namespace Pawan\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);
}
app/code/Pawan/ProductAlertApi/Model/ProductAlert.php
<?php
namespace Pawan\ProductAlertApi\Model;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\ProductAlert\Model\StockFactory;
use Pawan\ProductAlertApi\Api\ProductAlertManagementInterface;
class ProductAlert implements ProductAlertManagementInterface
{
protected $productRepository;
private $storeManager;
protected $stockFactory;
public function __construct(
ProductRepositoryInterface $productRepository,
StoreManagerInterface $storeManager,
StockFactory $stockFactory
) {
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
$this->stockFactory = $stockFactory;
}
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;
}
}
}
-
Thanks, @Pawan nicely explained and a great learning asset for Magento2 junior developers. Commented May 22, 2021 at 18:10
-
-
I have created API support for the price alert and have created a pull request for that in GitHub kindly check it. Commented Jun 12, 2021 at 11:54
-