mirakl / sdk-php-shop

Mirakl PHP SDK for sellers
30 stars 16 forks source link

getOffers : Max results and pagination parameters for the query? #51

Closed Maxtab closed 1 year ago

Maxtab commented 1 year ago

Hello.

Thank you for the SDK for which I took care to read the documentation, but I am facing an issue regarding the use of the API OF21: List offers of a shop. I use the PHP SDK in version 1.18.1

I want to retrieve all the offers from my shop (there are around 700) to store them once a day in a database on my side (and thus not systematically call the marketplace API). In particular, I need to access the Offers > Active and _Offers > Inactivityreasons information that is not found in the API OF51 Get offers CSV file.

I'm using the PHP SDK so I understand there is a limit to the number of results returned, but I don't see a way to handle pagination when using the SDK getOffers function

How to proceed to be able to retrieve and store on my side the list of all the offers via this API OF21, because I don't see anything about the possibility of using pagination in the getOffers function (expected parameters in particular)?

Thank you for your help.

For reference, here's, in a simplified way, what I was headed for


$api = new ShopApiClient($apiUrl, $apiKey, $shopId);

function fetchAllOffers(ShopApiClient $api, $shopId) {
    $allOffers = [];
    $offset = 0;
    $max = 100; // Max query results

    $totalOffersCount = 0; // Initialiser à 0
    do {
        $request = new GetOffersRequest($shopId);
        $request->set('max', $max);
        $request->set('offset', $offset);

        $promise = $api->async()->getOffers($request);
        $response = $promise->wait();

        // Read and json_decode the response
        $body = (string) $response->getBody();
        $data = json_decode($body, true);

        // Get the total offers count
        $totalOffersCount = $data['total_count'] ?? 0;

        $offers = $data['offers'] ?? [];

        $allOffers = array_merge($allOffers, $offers);
        $offset += $max;

    } while (count($allOffers) < $totalOffersCount);

    return $allOffers;
}

function displayOffers($allOffers) {
    echo '<table border="1">';
    echo '<tr><th>SKU</th></tr>';

    foreach ($allOffers as $offer) {
        echo '<tr>';
        echo '<td>' . $offer['product_sku'] . '</td>';
        echo '</tr>';
    }

    echo '</table>';
}

try {
    $allOffers = fetchAllOffers($api, $shopId);
    displayOffers($allOffers);
} catch (ClientException $e) {
    echo 'Error : ' . $e->getMessage();
}```
Maxtab commented 1 year ago

Maybe related to this #33 ?

amarie75 commented 1 year ago

Hello @Maxtab,

The problem is the method set() who doesn't exist on request objects. You should use setMax() and setOffset() methods. In your exemple, I think you retrieve the 10 first offers 7 or 8 times.

Here another way to write your exemple:

$api = new ShopApiClient($apiUrl, $apiKey, $shopId);

function fetchAllOffers(ShopApiClient $api, $shopId) {
    $allOffers = [];
    $offset = 0;
    $max = 100; // Max query results

    $totalOffersCount = 0; // Initialiser à 0
    do {
        $request = new GetOffersRequest($shopId);
        $request->setMax($max);
        $request->setOffset($offset);

        $response = $api->getOffers($request);

        // Get the total offers count
        $totalOffersCount = $response->getTotalCount();

        $offers = $response->getItems();

        $allOffers = array_merge($allOffers, $offers);
        $offset += $max;

    } while (count($allOffers) < $totalOffersCount);

    return $allOffers;
}

function displayOffers($allOffers) {
    echo '<table border="1">';
    echo '<tr><th>SKU</th></tr>';

    foreach ($allOffers as $offer) {
        echo '<tr>';
        echo '<td>' . $offer->getSku() . '</td>';
        echo '</tr>';
    }

    echo '</table>';
}

try {
    $allOffers = fetchAllOffers($api, $shopId);
    displayOffers($allOffers);
} catch (ClientException $e) {
    echo 'Error : ' . $e->getMessage();
}
Maxtab commented 1 year ago

Hi @amarie75 , You are right, thank you for your help, this indeed solves the problem. Sorry to have missed that!