I have implement simple product from Rest Api in Magento 2. I have make curd opration for simple product api.
First You Need to Create Custom module then follow this steps. I have shared a step by step guide to add qty increment buttons on product page.
Step 1: Create register.php
app/code/Bluethinkinc/Productapi/registration.php
1 2 3 4 5 6 7 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethinkinc_Productapi', __DIR__ ); ?> |
Step 2: Create Module.xml
app/code/Bluethinkinc/Productapi/etc/module.xml
1 2 3 4 5 |
<?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="Bluethinkinc_Productapi" setup_version="1.0.0"> </module> </config> |
Step 3: Create webapi.xml
app/code/Bluethinkinc/Productapi/etc/webapi.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0"?> <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd"> <route method="POST" url="/V1/productadd/"> <service class="Bluethinkinc\Productapi\Api\ProductaddInterface" method="getPost"/> <resources> <resource ref="anonymous"/> </resources> </route> <route method="GET" url="/V1/productlist/"> <service class="Bluethinkinc\Productapi\Api\ProductlistInterface" method="getData"/> <resources> <resource ref="anonymous"/> </resources> </route> <route method="POST" url="/V1/productupdate/"> <service class="Bluethinkinc\Productapi\Api\ProductupdateInterface" method="getPost"/> <resources> <resource ref="anonymous"/> </resources> </route> <route method="POST" url="/V1/productdelete/"> <service class="Bluethinkinc\Productapi\Api\ProductdeleteInterface" method="getPost"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes> |
Step 4: Create di.xml
app/code/Bluethinkinc/Productapi/etc/di.xml
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Bluethinkinc\Productapi\Api\ProductaddInterface" type="Bluethinkinc\Productapi\Model\Api\Productadd"/> <preference for="Bluethinkinc\Productapi\Api\ProductupdateInterface" type="Bluethinkinc\Productapi\Model\Api\Productupdate"/> <preference for="Bluethinkinc\Productapi\Api\ProductlistInterface" type="Bluethinkinc\Productapi\Model\Api\Productlist"/> <preference for="Bluethinkinc\Productapi\Api\ProductdeleteInterface" type="Bluethinkinc\Productapi\Model\Api\Productdelete"/> </config> |
Step 5: Create ProductaddInterface.php
app/code/Bluethinkinc/Productapi/Api/ProductaddInterface.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace Bluethinkinc\Productapi\Api; interface ProductaddInterface { /** * GET for Post api * @param string $productname * @param string $sku * @param string $price * @param string $producttype * @return string */ public function getPost($productname,$sku,$price,$producttype); } ?> |
Step 6: Create ProductupdateInterface.php
app/code/Bluethinkinc/Productapi/Api/ProductupdateInterface.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace Bluethinkinc\Productapi\Api; interface ProductupdateInterface { /** * GET for Post api * @param string $id * @param string $pname * @param string $price * @return string */ public function getPost($id,$pname,$price); } ?> |
Step 7: Create ProductlistInterface.php
app/code/Bluethinkinc/Productapi/Api/ProductlistInterface.php
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace Bluethinkinc\Productapi\Api; interface ProductlistInterface { /** * GET for Post api * @return string */ public function getData(); } ?> |
Step 8: Create ProductdeleteInterface.php
app/code/Bluethinkinc/Productapi/Api/ProductdeleteInterface.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace Bluethinkinc\Productapi\Api; interface ProductdeleteInterface { /** * GET for Post api * @param string $id * @return string */ public function getPost($id); } ?> |
Step 9: Create Productadd.php
app/code/Bluethinkinc/Productapi/Model/Api/Productadd.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php namespace Bluethinkinc\Productapi\Model\Api; use Psr\Log\LoggerInterface; class Productadd { protected $logger; public function __construct( LoggerInterface $logger, \Magento\Catalog\Model\ProductFactory $product ) { $this->logger = $logger; $this->product = $product; } /** * @inheritdoc */ public function getPost($productname,$sku,$price,$producttype) { $response = ['success' => false]; try { $product = $this->product->create(); $product->setSku($sku); // Set your sku here $product->setName($productname); // Name of Product $product->setAttributeSetId(4); // Attribute set id $product->setStatus(1); // Status on product enabled/ disabled 1/0 $product->setWeight(10); // weight of product $product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually) $product->setTaxClassId(0); // Tax class id $product->setTypeId($producttype); // type of product (simple/virtual/downloadable/configurable) $product->setPrice($price); // price of product $product->setStockData( array( 'use_config_manage_stock' => 0, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 1000 ) ); $product->save(); $message=__('Product Created'); $response = ['success' => true, 'message' => $message]; } catch (\Exception $e) { $response = ['success' => false, 'message' => $e->getMessage()]; $this->logger->info($e->getMessage()); } $returnArray = json_encode($response); return $returnArray; } } ?> |
Step 10: Create Productupdate.php
app/code/Bluethinkinc/Productapi/Model/Api/Productupdate.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php namespace Bluethinkinc\Productapi\Model\Api; use Psr\Log\LoggerInterface; class Productupdate { protected $logger; public function __construct( LoggerInterface $logger, \Magento\Catalog\Model\ProductFactory $product ) { $this->logger = $logger; $this->product = $product; } /** * @inheritdoc */ public function getPost($id,$pname,$price) { $response = ['success' => false]; try { $product = $this->product->create()->load($id); $id=$product->getId(); //Get Product Name if($id) { $product->setData('store_id', 0); $product->setName($pname); // Set your Name here $product->setPrice($price); // price of product $product->save(); $message=__('Product Update successfully'); }else { $message=__('Product Not found'); } $response = ['success' => true, 'message' => $message]; } catch (\Exception $e) { $response = ['success' => false, 'message' => $e->getMessage()]; $this->logger->info($e->getMessage()); } $returnArray = json_encode($response); return $returnArray; } } ?> |
Step 11: Create Productlist.php
app/code/Bluethinkinc/Productapi/Model/Api/Productlist.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php namespace Bluethinkinc\Productapi\Model\Api; use Psr\Log\LoggerInterface; class Productlist { protected $logger; public function __construct( LoggerInterface $logger, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactor ) { $this->logger = $logger; $this->productCollectionFactory = $productCollectionFactor; } /** * @inheritdoc */ public function getData() { $response = ['success' => false]; try { $productdata=[]; $i=0; $collection = $this->productCollectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->addAttributeToFilter('type_id', ['eq' => 'simple']); $collection->getSelect()->order('created_at', \Magento\Framework\DB\Select::SQL_DESC); foreach ($collection as $product) { $productdata[$i]['product_id']=$product->getId(); $productdata[$i]['product_name']=$product->getName(); $productdata[$i]['price']=$product->getPrice(); $i++; } $response = ['success' => true, 'data' => $productdata]; } catch (\Exception $e) { $response = ['success' => false, 'message' => $e->getMessage()]; $this->logger->info($e->getMessage()); } $returnArray = json_encode($response); return $returnArray; } } ?> |
Step 12: Create Productdelete.php
app/code/Bluethinkinc/Productapi/Model/Api/Productdelete.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php namespace Bluethinkinc\Productapi\Model\Api; use Psr\Log\LoggerInterface; class Productdelete { protected $logger; public function __construct( LoggerInterface $logger, \Magento\Catalog\Model\ProductRepository $productRepository ) { $this->logger = $logger; $this->productRepository = $productRepository; } /** * @inheritdoc */ public function getPost($id) { $response = ['success' => false]; try { $product = $this->productRepository->getById($id); $this->productRepository->delete($product); $message=__('Product Deleted successfully'); $response = ['success' => true, 'message' => $message]; } catch (\Exception $e) { $response = ['success' => false, 'message' => $e->getMessage()]; $this->logger->info($e->getMessage()); } $returnArray = json_encode($response); return $returnArray; } } ?> |
Postman Request and Responce
Simple Product Add Request Url:
{Base_url}/rest/V1/productadd/
Method: POST
Request Body:
1 2 3 4 5 6 |
{ "productname":"Bluethinkinc", "sku":"bt-1004", "price":"456", "producttype":"simple" } |
Responce:
Simple Product Update Request Url:
{Base_url}/rest/V1/productupdate/
Method: POST
Request Body:
1 2 3 4 5 |
{ "id":"2047", "pname":"Bluethinkinc USA", "price":"4567" } |
Responce:
Simple Product List Request Url:
{Base_url}/rest/V1/productlist/
Method: GET
Responce:
Simple Product List Request Url:
{Base_url}/rest/V1/productdelete/
Method: POST
Request Body:
{
“id”:”2047″
}
Responce:
bluethinkinc_blog
2023-10-03