2

I want to get product information using API call. I have product id of that product is it possible to do that.

4

To get product information using API call with product id you can use following API with searchCriteria :

GET /V1/products

http://domain.com/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][condition_type]=eq&searchCriteria[filterGroups][0][filters][0][value]=5325

 {'searchCriteria':{'filterGroups':[{'filters':[{'field':'entity_id','value':"1",'condition_type':'eq'}]}]}}

In searchCriteria add filter with product id as above.

3

You need to define a new API.

So, let's define a new interface:

<?php

namespace XXX\Productby\Api;

interface ProductByInterface
{
    /**
     * GET product by its ID
     *
     * @api
     * @param string $id
     * @return \Magento\Catalog\Api\Data\ProductInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getProductById($id);
}

The definition is similar to the core API, and it throws the same exception.

Now, let's define a model to implement this:

namespace XXX\Productby\Model;

use XXX\Productby\Api\ProductByInterface;

class ProductBy implements ProductByInterface
{

    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;

    public function __construct(
       \Magento\Catalog\Api\ProductRepositoryInterface $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function getProductById($id)
    {
        return $this->productRepository->getById($id);
    }
}

Then, simply use your webapi.xml file to use this method when accessing a given route, adding something like:

<route url="/V1/xxx-productbyid/:id" method="GET">
    <service class="XXX\ProductBy\Api\ProductByInterface" method="getProductById"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

and to bind the model to the interface in the di.xml file:

<preference for="XXX\Productby\Api\ProductByInterface"
               type="Wow\Productby\Model\ProductBy"/>

Now you can access your product through APIs with a simple GET call:

GET /V1/xxx-productbyid/12345

Source: I had to already do this and even accessing the product with its URL key. Blog article here.

  • 1
    We don't need to define new API in this case, we can re-use the already function. See my answer. – Khoa TruongDinh Jul 18 '17 at 4:10
1

Do you mean SKU when you say you have product ID?

If so, yes, you can call https://domain.com/rest/V1/products/:sku where :sku is the product SKU.

You can see a full list of APIs here.

  • 1
    No i have product id not product sku. – Nitin Pawar Apr 1 '17 at 4:58
1

Take a look: \Magento\Catalog\Api\ProductRepositoryInterface

public function getById($productId, $editMode = false, $storeId = null, $forceReload = false);

As we can see, Magento already provides the function to get product by Id. We can re-use it.

Create a new module, declare the new api url:

app/code/Vendor/CatalogProduct/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">
    <route url="/V1/products/id/:productId" method="GET">
        <service class="Magento\Catalog\Api\ProductRepositoryInterface" method="getById"/>
        <resources>
            <resource ref="Magento_Catalog::products" />
        </resources>
    </route>
</routes>

Now, we can get: http://magesitd.loc/rest/V1/products/id/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.