Search code examples
phpfilterakeneo

How can I add the filter so that API call is returning just the results that I want?


This is what I got so far, but it doesn't work.

public function getCompleteness(){
    $searchBuilder = new AkeneoSearchBuilder();
    $searchBuilder->addFilter('completeness', '=', 100);

    try {
        $products = $this->apiClient->getProductApi()->all(
            50,
            [
                'search' => $searchBuilder->getFilters(),
                'scope' => 'ecommerce'
            ]
        );
    } catch (HttpException $e){
        echo "Message: " . $e->getMessage();
        echo "Code: " . $e->getCode();
    }

    return $products;
}

}

This is the call that I am trying to use from Akeneo API.

{{url}}/api/rest/v1/products?search={"completeness":[{"operator":"=","value":100,"scope":"ecommerce"}]}

How can I use GetCompleteness() method in order that it produces some results ? I have problem with using AkeneoSearchBuilder();


Solution

  • When you do:

    $searchBuilder = new AkeneoSearchBuilder();
    $searchBuilder->addFilter('completeness', '=', 100);
    $products = $this->apiClient->getProductApi()->all(
        50,
        [
            'search' => $searchBuilder->getFilters(),
            'scope' => 'ecommerce'
        ]
    );
    

    It basically means: "For all products complete on scope "undefined", give me all their "ecommerce" values".

    What you are trying to achieve is (I guess): "For all products complete on scope "ecommerce", give me all their values".

    As you can read on the official documentation about completeness filter, you need to specify a scope:

    $searchBuilder = new AkeneoSearchBuilder();
    $searchBuilder->addFilter('completeness', '=', 100, ['scope' => 'ecommerce']);
    

    Now you can retrieve their values, by calling:

    $products = $this->apiClient->getProductApi()->all(
        50,
        [
            'search' => $searchBuilder->getFilters()
        ]
    );
    

    Note that I removed the "scope" parameter here.

    To resume: