0

Is it possible to create a Date filter to fetch orders created only in last two days using API , without hard-coded values ?

Something like this:

http://<magento_host>/rest/V1/orders?

searchCriteria[filter_groups][0][filters][0][field]=created_at&

searchCriteria[filter_groups][0][filters][0][condition_type]=from&

searchCriteria[filter_groups][0][filters][0][value]=(current date - 2 days)

searchCriteria[filter_groups][1][filters][0][field]=created_at&

searchCriteria[filter_groups][1][filters][0][condition_type]=to&

searchCriteria[filter_groups][1][filters][0][value]=(current date)

1 Answer 1

0

Example in PHP to Fetch Orders

<?php

$endDate = new DateTime();
$startDate = new DateTime();
$startDate->modify('-2 days');

$endDateStr = $endDate->format('Y-m-d H:i:s');
$startDateStr = $startDate->format('Y-m-d H:i:s');

$apiUrl = 'https://your-magento-store.com/rest/V1/orders';
$query = http_build_query([
    'searchCriteria' => [
        'filterGroups' => [
            [
                'filters' => [
                    [
                        'field' => 'created_at',
                        'value' => $startDateStr,
                        'condition_type' => 'from'
                    ],
                    [
                        'field' => 'created_at',
                        'value' => $endDateStr,
                        'condition_type' => 'to'
                    ]
                ]
            ]
        ]
    ]
]);

$fullUrl = $apiUrl . '?' . $query;

// Initialize cURL session
$ch = curl_init($fullUrl);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $accessToken,
    'Content-Type: application/json'
]);

// Execute the cURL session
$response = curl_exec($ch);

// Close the cURL session
curl_close($ch);

// Handle the response
$responseData = json_decode($response, true);
print_r($responseData);

THANKS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.