Skip to content

Instantly share code, notes, and snippets.

@tshabatyn
Created April 11, 2023 15:02
Show Gist options
  • Save tshabatyn/b237b22fed4f656d6bc51bf2530dee4d to your computer and use it in GitHub Desktop.
Save tshabatyn/b237b22fed4f656d6bc51bf2530dee4d to your computer and use it in GitHub Desktop.
Get Magento 2 Order details by REST API
#!/usr/bin/env bash
set -e
# Determining path to the current shell script
THIS_FILE_DIR=$(dirname $(cd $(dirname $0); pwd))
STORE_URL=''
USER='admin'
PASS='123123q'
ORDER_ID='?searchCriteria[sortOrders][0][field]=created_at&searchCriteria[sortOrders][0][direction]=desc&searchCriteria[pageSize]=1'
if [ $# -lt 1 ]; then
echo "You may provide one of the following flags for providing additional options:"
echo -e "--url or -url\t\t- BASE Magento URL for instance https://store.magento.test"
echo -e "--user or -user\t\t- Magento admin username"
echo -e "--pass or -pass\t\t- Magento admin password"
echo -e "--order_id or -order_id\t- Magento order_id, is optional. The call will return the latest order entity, in case this flag is omitted."
fi
while [ $# -gt 0 ]; do
case "$1" in
--url*|-url*)
export STORE_URL="${2}"
shift
;;
--user*|-user*)
export USER="${2}"
shift
;;
--pass*|-pass*)
export PASS="${2}"
shift
;;
--order_id*|-order_id*)
export ORDER_ID="/${2}"
shift
;;
*)
echo "Please use on of the following flags for providing additional options:"
echo -e "--url or -url\t\t- BASE Magento URL for instance https://store.magento.test"
echo -e "--user or -user\t\t- Magento admin username"
echo -e "--pass or -pass\t\t- Magento admin password"
echo -e "--order_id or -order_id\t- Magento order_id"
shift
;;
esac
shift
done
if [ -z "$STORE_URL" ]; then
# Trying to get store URL from Magento env file
MAGENT_CONFIG_FILE_PATH=$(realpath ${THIS_FILE_DIR}/../../../../app/etc/env.php)
STORE_URL=$(grep "'base_url'" ${MAGENT_CONFIG_FILE_PATH} | tail -n1 | awk '{print $3}' | sed "s#[',]##g" | cut -f1)
STORE_URL=${STORE_URL%/}
fi
if [ -z "$STORE_URL" ]; then
### Trying to read store URL name from env file
PROJECT_ENV_FILE_PATH=$(realpath ${THIS_FILE_DIR}/../../../../.env)
source ${PROJECT_ENV_FILE_PATH}
STORE_URL="https://$TRAEFIK_SUBDOMAIN.$TRAEFIK_DOMAIN"
fi
CREDENTIALS=$(printf '{"username":"%s", "password":"%s"}' $USER $PASS)
echo "Store URL: ${STORE_URL}"
echo "CREDENTIALS: ${CREDENTIALS}"
TOKEN=$(curl -sS -g -X POST "${STORE_URL}/index.php/rest/V1/integration/admin/token" \
-H "Content-Type:application/json" \
-d "${CREDENTIALS}" | sed 's#"##g')
echo "TOKEN: ${TOKEN}"
curl -sS -g -X GET \
-H "Authorization: Bearer ${TOKEN}" \
"${STORE_URL}/index.php/rest/V1/orders/${ORDER_ID}" | json_pp
# You may add the following flag to the curl call, for running Magento with xDebug.
# -b XDEBUG_SESSION=PHPSTORM
########################################################################################################################
# Call example:
# ./MagentoGetOrderRestApi.sh \
# --url <MAGENTO_BASE_URL> \
# --user <ADMIN_USERNAME> \
# --pass <ADMIN_PASSWORD> \
# --order_id <ORDER_ID>
#
# Where:
# MAGENTO_BASE_URL - Magento store base URL, for instance https://store.magento.test
# ADMIN_USERNAME - Magento store admin username
# ADMIN_PASSWORD - Magento store admin password
# ORDER_ID - ID of the Magento order (order_id), not increment_id
########################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment