0

I have created a custom REST API that return all review of products based on store id.

http://hostname/magento/index.php/rest/V1/showreview/allreviews/1

it returns output like-

[{"review_id":"345","created_at":"2018-08-16 08:03:19","entity_id":"1","entity_pk_value":"1705","status_id":"1","detail_id":"345","title":"Huge arm holes??","detail":"I don't know why the arm holes are so big. It looked ok in the photo but in person they're really wide. It's really comfortable but that bugs me.","nickname":"Valorie","customer_id":null},{...}..]

Now my question is how to implement search criteria on custom rest api.? actually i previously applied search criteria on predefined rest api but don't know how to implement on custom api.

Any help appreciated.

  • you can check magento CMS Page /V1/cmsPage/search – kunj Aug 28 '18 at 11:26
1

After lot of time spends,I solve my problem. In url pass parameter as-

http://hostname/magento/index.php/rest/V1/showreview/allreviews/1?productId=1&title=value&....

now, in class where method is created use following code. First use \Magento\Framework\App\Request\Http request and initialize in cosnstructor.

protected $request;
    public function __construct(\Magento\Framework\App\Request\Http $request) {
       $this->request = $request;
    }

Then read all parameters from request.It returns an array of parameters.

//Get All parameters from request
        $allParameters=$this->request->getParams();

Then check each of parameters exist in url or not and apply filter using that parameters like-

if(array_key_exists("productId",$allParameters)){
                $collection=$collection->addFieldToFilter('entity_pk_value', ['eq' => $allParameters['productId']]);
             }

where $collection is contains all result where filter need to apply.

$collection = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory")->create();

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.