Rest API Magento 2 Product list by Postman

Manifest Desires

Billionaire Brain Wave

The Billionaire Brain Wave

The Divine Praye

The Divine Prayer

The Genius Wave

The Genius Wave

Wealth DNA Code

Wealth DNA Code

Rest API Magento 2 Product list by Postman

  • By admin
  • Magento 2
  • Comments Off on Rest API Magento 2 Product list by Postman

Rest API Magento 2 Product list by Postman

Get product list need to create simple module usign api,model,webapi.xml and di.xml
Create method in api class.

<?php
namespace Vendor\Module\Api;

/**
* Interface ProductStockInterface
*/
interface ProductDataInterface
{
/**
* get products.
*
* @return array
*/
public function getProData();

}

Create webapi.xml under etc folder
webapi.xml

<route url="/V1/products" method="GET">
<service class="Vendor\Module\Api\ProductDataInterface" method="getProData"/>
<resources>
<resource ref="Vendor_Module::products_data"/>
</resources>
</route>

Create di.xml under etc folder.
di.xml

<preference for="Vendor\Module\Api\ProductDataInterface" type="Vendor\Module\Model\Api\ProductData" />

Model\Api\ProductData

<?php
namespace Vendor\Module\Model\Api;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

/**
* Model class ProductData
*/
class ProductData implements \Vendor\Module\Api\ProductDataInterface
{

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $productCollection;

protected $product;

protected $categoryCollection;

protected $_currencyinterface;

/**
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection
*/
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,Product $product,CollectionFactory $categoryCollection,\Magento\Framework\Pricing\PriceCurrencyInterface $currencyinterface
)
{
$this->productCollection = $productCollection;
$this->product = $product;
$this->categoryCollection = $categoryCollection;
$this->_currencyinterface = $currencyinterface;
}

/**
* get products.
*
* @return array
*/
public function getProData()
{
$returnArray = [];
try{
$productCollection = $this->productCollection->create()
->addAttributeToSelect('*')
// ->setPageSize(500)
->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->addAttributeToFilter('type_id', \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->joinField(
'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
);
foreach ($productCollection as $product) {

$returnArray[] = [
"sku" => $product->getSku(),
//add data accordingly

];
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$returnArray['error'] = $e->getMessage();
$returnArray['status'] = 0;
} catch (\Exception $e) {
$returnArray['error'] = __('unable to process request');
$returnArray['status'] = 2;
}
return $returnArray;
}

}
?>

Return data postman.

Manifest Desires-Click Here