1

I am trying to get product collection using Rest Api Below is my code file: ProductsManagementInterface.php

interface ProductsManagementInterface {

/**
 * GET for Products api
 * @param string $param
 * @return array
 */
public function getProducts($param); }

webservice define in 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 method="GET" url="/V1/productcollection/products/:param">
        <service class="Vendor\ProductCollection\Api\ProductsManagementInterface" method="getProducts"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

model file

<?php


namespace Vendor\ProductCollection\Model;

class ProductsManagement implements \Vendor\ProductCollection\Api\ProductsManagementInterface
{

    protected $_categoryFactory;
    protected $_category;
    protected $_categoryRepository;
    protected $_storeManager;
    protected $jsonHelper;

    public function __constructor(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Catalog\Helper\Category $category,
    \Magento\Catalog\Model\CategoryRepository $categoryRepository,
    \Magento\Framework\Json\Helper\Data $jsonHelper,
    Magento\Store\Model\StoreManagerInterface $storeManager
    ){
        $this->_categoryFactory = $categoryFactory;
        $this->_category = $category;
        $this->_categoryRepository = $categoryRepository;
        $this->jsonHelper = $jsonHelper;
        $this->_storeManager = $storeManager;
    }
    /**
     * Return mixed.
     *
     * @api
     * @param string $param.
     * @return array.
     */
    public function getProducts($param)
    {
        $json = $this->getJsonArrayProductFromCategory($param);
        $decodedData = $this->jsonHelper->jsonDecode($json);
        return $decodedData;
        //return 'hello api GET return the $param ' . $param;
    }

    public function getJsonArrayProductFromCategory($id){
        $category =  $this->_categoryFactory->create()->load($id);
        $categoryProducts = $category->getProductCollection()
                             ->addAttributeToSelect('*');
        $json= array();
        $i=0;

        foreach ($categoryProducts as $product) 
        {
                $productData['sku'] = $product->getSku();
                $productData['name'] = $product->getName();
                $productData['price'] = $product->getPrice();
                $productData['url'] = $product->getProductUrl();
                $json[$i]=$productData;
                $i++;
        }
        return $json; 
    }
}

My url is

http://domian.com/rest/V1/productcollection/products/5

but it give enter image description here

2

There are 2 problems in your code first one is you define the wrong __constructor

Change your __constructor to __construct like,

public function __construct(
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Catalog\Helper\Category $category,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository,
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ){
        $this->_categoryFactory = $categoryFactory;
        $this->_category = $category;
        $this->_categoryRepository = $categoryRepository;
        $this->jsonHelper = $jsonHelper;
        $this->_storeManager = $storeManager;
    }

and run the di:compile from your Magento 2 root command line,

php bin/magento setup:di:compile

and the second problem is you are passing the Array instead of string to this line,

    $decodedData = $this->jsonHelper->jsonDecode($json);

I am not sure what kind of output format you want but to check if it's working for you can remove this like and just return $json like this,

    /**
     * Return mixed.
     *
     * @api
     * @param integer $param.
     * @return array.
     */
    public function getProducts($param)
    {
        $json = $this->getJsonArrayProductFromCategory($param);
        return $json;
    }

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.