Is there any API to get all categories detail with images as well? I am using the below API but it won't return image information
/rest/default/V1/categories
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityUsing postman:
{{magento_url}}rest/default/V1/categories/list?searchCriteria[pageSize]=100&searchCriteria[currentPage]=1
with bearer token method on my local, I was able to find the image data. See screenshot below
You can create your own custom API.
I created a custom API to see the product details with productID. Below is the code for that. You can make changes to it and customize it to show you the category details and its image.
In my example, My vendor is M2 and my module is Apis.
Vendor/Module/registration.php
<?php
declare(strict_types=1);
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'M2_Apis', __DIR__);
Vendor/Module/etc/di.xml
<?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="M2\Apis\Api\ProductRepositoryInterface" type="M2\Apis\Model\ProductRepository" />
<preference for="M2\Apis\Api\Data\ProductInterface" type="M2\Apis\Model\Data\Product" />
</config>
Vendor/Module/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="M2_Apis" />
</config>
Vendor/Module/etc/webapi.xml
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V2/product/getProductsById/:id" method="GET">
<service class="M2\Apis\Api\ProductRepositoryInterface" method="getProductsById"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
Vendor/Module/Api/ProductRepositoryInterface.php
<?php
namespace M2\Apis\Api;
use Magento\TestFramework\Exception\NoSuchActionException;
interface ProductRepositoryInterface
{
/**
* @param int $id
* @return \M2\Apis\Api\Data\ProductInterface
* @throws NoSuchActionException
*/
public function getProductsById(int $id);
}
Vendor/Module/Api/Data/ProductInterface.php
<?php
namespace M2\Apis\Api\Data;
interface ProductInterface{
/**
* @return int
*/
public function getId(): int;
/**
* @param int $id
* @return $this
*/
public function setId($id);
/**
* @return string
*/
public function getSku(): string;
/**
* @param string $sku
* @return $this
*/
public function setSku($sku);
/**
* @return string
*/
public function getName(): string;
/**
* @param string $name
* @return $this
*/
public function setName($name);
/**
* @return string
*/
public function getDescription(): string;
/**
* @param string $description
* @return $this
*/
public function setDescription($description);
/**
* @return string
*/
public function getPrice(): string;
/**
* @param string $price
* @return $this
*/
public function setPrice($price);
/**
* @return string[]
*/
public function getImages(): array;
/**
* @param string[] $productImagesArray
* @return $this
*/
public function setImages($productImagesArray);
}
Vendor/Module/Helper/ProductHelper.php
This class is used as a helper class for product price and images.
<?php
namespace M2\Apis\Helper;
use Magento\Catalog\Model\Product;
use Magento\Framework\Pricing\Helper\Data;
class ProductHelper{
/**
* @var Data
*/
private $priceHelper;
/**
* @param Data $priceHelper
*/
public function __construct(Data $priceHelper)
{
$this->priceHelper = $priceHelper;
}
/**
* @param string $price
* @return float|string
*/
public function formatPrice($price){
return $this->priceHelper->currency($price,true,false);
}
/**
* @param Product $product
* @return array
*/
public function getProductImagesArray($product){
$images = $product->geTMediaGalleryImages();
$imagesArray = array();
foreach ($images as $image){
$imagesArray[] = $image->getUrl();
}
return $imagesArray;
}
}
Vendor/Module/Model/ProductRepository.php
<?php
namespace M2\Apis\Model;
use M2\Apis\Api\ProductRepositoryInterface;
use M2\Apis\Helper\ProductHelper;
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\TestFramework\Exception\NoSuchActionException;
class ProductRepository implements ProductRepositoryInterface{
/**
* @var ProductInterfaceFactory
*/
private ProductInterfaceFactory $productInterfaceFactory;
/**
* @var ProductHelper
*/
private ProductHelper $productHelper;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
private \Magento\Catalog\Api\ProductRepositoryInterface $productRepository;
/**
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param ProductInterfaceFactory $productInterfaceFactory
* @param ProductHelper $productHelper
*/
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
ProductInterfaceFactory $productInterfaceFactory,
ProductHelper $productHelper
)
{
$this->productInterfaceFactory = $productInterfaceFactory;
$this->productRepository = $productRepository;
$this->productHelper = $productHelper;
}
/**
* @param int $id
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getProductsById(int $id)
{
$productInterface = $this->productInterfaceFactory->create();
try {
//echo ("inside");
$product = $this->productRepository->getById($id);
$productInterface->setId($product->getId());
$productInterface->setSku($product->getSku());
$productInterface->setName($product->getName());
$desc = $product->getDescription();
$desc = str_replace(array("\r", "\n"), '', $desc);
$productInterface->setDescription($desc ? strip_tags($desc) : "");
$productInterface->setPrice($this->productHelper->formatPrice($product->getPrice()));
$productInterface->setImages($this->productHelper->getProductImagesArray($product));
return $productInterface;
}
catch (NoSuchActionException $e){
throw NoSuchEntityException::singleField("id",$id);
}
}
}
Vendor/Module/Model/Data/Product.php
<?php
namespace M2\Apis\Model\Data;
use M2\Apis\Api\Data\ProductInterface;
use Magento\Framework\DataObject;
use Magento\Tests\NamingConvention\true\string;
class Product extends DataObject implements ProductInterface{
/**
* @return int
*/
public function getId() : int
{
return $this->getData('id');
}
/**
* @param int $id
* @return Product
*/
public function setId($id)
{
return $this->setData('id',$id);
}
/**
* @return string
*/
public function getSku() : string
{
return $this->getData('sku');
}
/**
* @param string $sku
* @return Product
*/
public function setSku($sku)
{
return $this->setData('sku',$sku);
}
/**
* @return string
*/
public function getName() : string
{
return $this->getData('name');
}
/**
* @param string $name
* @return Product
*/
public function setName($name)
{
return $this->setData('name',$name);
}
/**
* @return string
*/
public function getDescription() : string
{
return $this->getData('description');
}
/**
* @param string $description
* @return Product
*/
public function setDescription($description)
{
return $this->setData('description',$description);
}
/**
* @return string
*/
public function getPrice() : string
{
return $this->getData('price');
}
/**
* @param string $price
* @return Product
*/
public function setPrice($price)
{
return $this->setData('price',$price);
}
/**
* @return array|string[]
*/
public function getImages() : array
{
return $this->getData('images');
}
/**
* @param string[] $productImagesArray
* @return Product
*/
public function setImages($productImagesArray)
{
return $this->setData('images',$productImagesArray);
}
}