5

Get list of all previous orders of the customer using Customer token in API.

3
+50

Solution 1

You can make your custom API for the same;

Using filter Builder you can use search criteria

$customerId = $this->userContext->getUserId();

$filter = $this->filterBuilder
               ->setField('customer_id')
               ->setValue($customerId)
               ->setConditionType('eq')
               ->create();
$this->searchCriteriaBuilder->addFilters([$filter]);

$searchCriteria = $this->searchCriteriaBuilder->create();

Solution 2

Magento is using resource Magento_Sales::sales because of this we can't access it using Customer's token so for the same we need to override Specific API with

<resource ref="self" />

And using this we can access all orders of specific customer using customer's Token in Magento API.

<route url="/V1/orders" method="GET">
        <service class="Magento\Sales\Api\OrderRepositoryInterface" method="getList"/>
        <resources>
            <resource ref="self" />
        </resources>
    </route>
  • I tried second solution before but I don't want to override Magento's default API. – Yann Martel Nov 13 '18 at 13:45
  • I will try 1st sol and get back to you. – Yann Martel Nov 13 '18 at 13:46
  • What to PUT in resource ? Anonymous ? – Yann Martel Nov 13 '18 at 13:47
  • No, you can not use Anonymous, Because you're using customer's data. – Aditya Shah Nov 13 '18 at 13:48
  • So it should be self – Aditya Shah Nov 13 '18 at 13:48
3

Magento own API does not have an endpoint to fetch orders with customer token, it only has one for integration or admin token.

You can use or look at Deity as they prepared magento extension that provides such endpoint: https://github.com/deity-io/falcon-magento2-module/blob/master/src/etc/webapi.xml#L132

Fur disclosure, I have written that piece of code but I am no longer a developer of that module.

There is an alternative if you are using some middleware layer to talk to Magento2 API of which you have control and can use admin token there. First query customers/me endpoint using customer token to fetch current customer data and then query /orders endpoint with admin token providing received customer id in searchCriteria.

1

For the get Customer Order history use searchCriteria and passed customer_email id.

Follow below Magento 2 API to get the customer order history.

Request URL: https://magento.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=customer_email&searchCriteria[filter_groups][0][filters][0][value]=test@gmail.com

Method: GET

Response:

{
    "items": [
        {
            "base_currency_code": "KWD",
            "base_discount_amount": 0,
            "base_grand_total": 40,
            "base_discount_tax_compensation_amount": 0,
            "base_shipping_amount": 10,
            "base_shipping_discount_amount": 0,
            "base_shipping_discount_tax_compensation_amnt": 0,
            "base_shipping_incl_tax": 10,
            "base_shipping_tax_amount": 0,
            "base_subtotal": 30,
            "base_subtotal_incl_tax": 30,
            "base_tax_amount": 0,
            "base_total_due": 40,
            "base_to_global_rate": 1,
            "base_to_order_rate": 1,
            "billing_address_id": 250,
            "created_at": "2019-08-14 11:26:48",
            "customer_email": "test@gmail.com",
            "customer_firstname": "Test",
            "customer_group_id": 1,
            "customer_id": 51,
            "customer_is_guest": 0,
            "customer_lastname": "Customer",
            "customer_note_notify": 1,
            "discount_amount": 0,
            "email_sent": 1,
            "entity_id": 125,
            "global_currency_code": "KWD",
            "grand_total": 40,
            "discount_tax_compensation_amount": 0,
            "increment_id": "000000119",
            "is_virtual": 0,
            "order_currency_code": "KWD",
            "protect_code": "9a7b171724410d42aec193ba5206901e",
            "quote_id": 398,
            "remote_ip": "202.131.115.180",
            "shipping_amount": 10,
            "shipping_description": "Flat Rate - Fixed",
            "shipping_discount_amount": 0,
            "shipping_discount_tax_compensation_amount": 0,
            "shipping_incl_tax": 10,
            "shipping_tax_amount": 0,
            "state": "new",
            "status": "pending",
            "store_currency_code": "KWD",
            "store_id": 1,
            "store_name": "Default Store",
            "store_to_base_rate": 0,
            "store_to_order_rate": 0,
            "subtotal": 30,
            "subtotal_incl_tax": 30,
            "tax_amount": 0,
            "total_due": 40,
            "total_item_count": 1,
            "total_qty_ordered": 2,
            "updated_at": "2019-08-14 11:26:49",
            "weight": 0,
            "items": [
                {
                    "amount_refunded": 0,
                    "base_amount_refunded": 0,
                    "base_cost": 8.5,
                    "base_discount_amount": 0,
                    "base_discount_invoiced": 0,
                    "base_discount_tax_compensation_amount": 0,
                    "base_original_price": 15,
                    "base_price": 15,
                    "base_price_incl_tax": 15,
                    "base_row_invoiced": 0,
                    "base_row_total": 30,
                    "base_row_total_incl_tax": 30,
                    "base_tax_amount": 0,
                    "base_tax_invoiced": 0,
                    "created_at": "2019-08-14 11:26:48",
                    "discount_amount": 0,
                    "discount_invoiced": 0,
                    "discount_percent": 0,
                    "free_shipping": 0,
                    "discount_tax_compensation_amount": 0,
                    "is_qty_decimal": 0,
                    "is_virtual": 0,
                    "item_id": 341,
                    "name": "DIAMOND GLITTER GRAY",
                    "no_discount": 0,
                    "order_id": 125,
                    "original_price": 15,
                    "price": 15,
                    "price_incl_tax": 15,
                    "product_id": 228,
                    "product_type": "simple",
                    "qty_canceled": 0,
                    "qty_invoiced": 0,
                    "qty_ordered": 2,
                    "qty_refunded": 0,
                    "qty_shipped": 0,
                    "quote_item_id": 1281,
                    "row_invoiced": 0,
                    "row_total": 30,
                    "row_total_incl_tax": 30,
                    "row_weight": 0,
                    "sku": "CLBELGLG0175DCMO",
                    "store_id": 1,
                    "tax_amount": 0,
                    "tax_invoiced": 0,
                    "tax_percent": 0,
                    "updated_at": "2019-08-14 11:26:48"
                }
            ],
            "billing_address": {
                "address_type": "billing",
                "city": "California",
                "company": "Company",
                "country_id": "US",
                "email": "test@gmail.com",
                "entity_id": 250,
                "firstname": "john",
                "lastname": "harrison",
                "parent_id": 125,
                "postcode": "12345",
                "region": "New York",
                "region_code": "NY",
                "region_id": 43,
                "street": [
                    "Street Address"
                ],
                "telephone": "2313131312"
            },
            "payment": {
                "account_status": null,
                "additional_information": [
                    "Check / Money order"
                ],
                "amount_ordered": 40,
                "base_amount_ordered": 40,
                "base_shipping_amount": 10,
                "cc_exp_year": "0",
                "cc_last4": null,
                "cc_ss_start_month": "0",
                "cc_ss_start_year": "0",
                "entity_id": 125,
                "method": "checkmo",
                "parent_id": 125,
                "shipping_amount": 10
            },
            "status_histories": [],
            "extension_attributes": {
                "shipping_assignments": [
                    {
                        "shipping": {
                            "address": {
                                "address_type": "shipping",
                                "city": "California",
                                "company": "Company",
                                "country_id": "US",
                                "email": "test@gmail.com",
                                "entity_id": 249,
                                "firstname": "john",
                                "lastname": "harrison",
                                "parent_id": 125,
                                "postcode": "12345",
                                "region": "New York",
                                "region_code": "NY",
                                "region_id": 43,
                                "street": [
                                    "Street Address"
                                ],
                                "telephone": "2313131312"
                            },
                            "method": "flatrate_flatrate",
                            "total": {
                                "base_shipping_amount": 10,
                                "base_shipping_discount_amount": 0,
                                "base_shipping_discount_tax_compensation_amnt": 0,
                                "base_shipping_incl_tax": 10,
                                "base_shipping_tax_amount": 0,
                                "shipping_amount": 10,
                                "shipping_discount_amount": 0,
                                "shipping_discount_tax_compensation_amount": 0,
                                "shipping_incl_tax": 10,
                                "shipping_tax_amount": 0
                            }
                        },
                        "items": [
                            {
                                "amount_refunded": 0,
                                "base_amount_refunded": 0,
                                "base_cost": 8.5,
                                "base_discount_amount": 0,
                                "base_discount_invoiced": 0,
                                "base_discount_tax_compensation_amount": 0,
                                "base_original_price": 15,
                                "base_price": 15,
                                "base_price_incl_tax": 15,
                                "base_row_invoiced": 0,
                                "base_row_total": 30,
                                "base_row_total_incl_tax": 30,
                                "base_tax_amount": 0,
                                "base_tax_invoiced": 0,
                                "created_at": "2019-08-14 11:26:48",
                                "discount_amount": 0,
                                "discount_invoiced": 0,
                                "discount_percent": 0,
                                "free_shipping": 0,
                                "discount_tax_compensation_amount": 0,
                                "is_qty_decimal": 0,
                                "is_virtual": 0,
                                "item_id": 341,
                                "name": "BELLA DIAMOND GLITTER GRAY 0175",
                                "no_discount": 0,
                                "order_id": 125,
                                "original_price": 15,
                                "price": 15,
                                "price_incl_tax": 15,
                                "product_id": 228,
                                "product_type": "simple",
                                "qty_canceled": 0,
                                "qty_invoiced": 0,
                                "qty_ordered": 2,
                                "qty_refunded": 0,
                                "qty_shipped": 0,
                                "quote_item_id": 1281,
                                "row_invoiced": 0,
                                "row_total": 30,
                                "row_total_incl_tax": 30,
                                "row_weight": 0,
                                "sku": "CLBELGLG0175DCMO",
                                "store_id": 1,
                                "tax_amount": 0,
                                "tax_invoiced": 0,
                                "tax_percent": 0,
                                "updated_at": "2019-08-14 11:26:48"
                            }
                        ]
                    }
                ],
                "rewards_discount": 0,
                "rewards_spend": 0,
                "rewards_earn": 950
            }
        },
        {
            "applied_rule_ids": "4,11",
            "base_currency_code": "KWD",
            "base_discount_amount": -15,
            "base_grand_total": 165,
            "base_discount_tax_compensation_amount": 0,
            "base_shipping_amount": 45,
            "base_shipping_discount_amount": 0,
            "base_shipping_discount_tax_compensation_amnt": 0,
            "base_shipping_incl_tax": 45,
            "base_shipping_tax_amount": 0,
            "base_subtotal": 135,
            "base_subtotal_incl_tax": 135,
            "base_tax_amount": 0,
            "base_total_due": 165,
            "base_to_global_rate": 1,
            "base_to_order_rate": 1,
            "billing_address_id": 268,
            "created_at": "2019-09-24 13:16:32",
            "customer_email": "test@gmail.com",
            "customer_firstname": "Test",
            "customer_group_id": 1,
            "customer_id": 51,
            "customer_is_guest": 0,
            "customer_lastname": "Customer",
            "customer_note_notify": 1,
            "discount_amount": -15,
            "email_sent": 1,
            "entity_id": 134,
            "global_currency_code": "KWD",
            "grand_total": 165,
            "discount_tax_compensation_amount": 0,
            "increment_id": "000000128",
            "is_virtual": 0,
            "order_currency_code": "KWD",
            "protect_code": "1deccd785628b2df682d9156db732fc0",
            "quote_id": 408,
            "shipping_amount": 45,
            "shipping_description": "Flat Rate - Fixed",
            "shipping_discount_amount": 0,
            "shipping_discount_tax_compensation_amount": 0,
            "shipping_incl_tax": 45,
            "shipping_tax_amount": 0,
            "state": "new",
            "status": "pending",
            "store_currency_code": "KWD",
            "store_id": 1,
            "store_name": "Default Store",
            "store_to_base_rate": 0,
            "store_to_order_rate": 0,
            "subtotal": 135,
            "subtotal_incl_tax": 135,
            "tax_amount": 0,
            "total_due": 165,
            "total_item_count": 1,
            "total_qty_ordered": 9,
            "updated_at": "2019-09-24 13:16:33",
            "weight": 0,
            "items": [
                {
                    "amount_refunded": 0,
                    "applied_rule_ids": "4,11",
                    "base_amount_refunded": 0,
                    "base_cost": 8.5,
                    "base_discount_amount": 15,
                    "base_discount_invoiced": 0,
                    "base_discount_tax_compensation_amount": 0,
                    "base_original_price": 15,
                    "base_price": 15,
                    "base_price_incl_tax": 15,
                    "base_row_invoiced": 0,
                    "base_row_total": 135,
                    "base_row_total_incl_tax": 135,
                    "base_tax_amount": 0,
                    "base_tax_invoiced": 0,
                    "created_at": "2019-09-24 13:16:32",
                    "discount_amount": 15,
                    "discount_invoiced": 0,
                    "discount_percent": 0,
                    "free_shipping": 0,
                    "discount_tax_compensation_amount": 0,
                    "is_qty_decimal": 0,
                    "is_virtual": 0,
                    "item_id": 351,
                    "name": "BELLA DIAMOND GLITTER GRAY 0175",
                    "no_discount": 0,
                    "order_id": 134,
                    "original_price": 15,
                    "price": 15,
                    "price_incl_tax": 15,
                    "product_id": 228,
                    "product_type": "simple",
                    "qty_canceled": 0,
                    "qty_invoiced": 0,
                    "qty_ordered": 9,
                    "qty_refunded": 0,
                    "qty_shipped": 0,
                    "quote_item_id": 1378,
                    "row_invoiced": 0,
                    "row_total": 135,
                    "row_total_incl_tax": 135,
                    "row_weight": 0,
                    "sku": "CLBELGLG0175DCMO",
                    "store_id": 1,
                    "tax_amount": 0,
                    "tax_invoiced": 0,
                    "tax_percent": 0,
                    "updated_at": "2019-09-24 13:16:32"
                }
            ],
            "billing_address": {
                "address_type": "billing",
                "city": "California",
                "company": "Company",
                "country_id": "US",
                "email": "test@gmail.com",
                "entity_id": 268,
                "firstname": "john",
                "lastname": "harrison",
                "parent_id": 134,
                "postcode": "12345",
                "region": "New York",
                "region_code": "NY",
                "region_id": 43,
                "street": [
                    "Street Address"
                ],
                "telephone": "2313131312"
            },
            "payment": {
                "account_status": null,
                "additional_information": [
                    "Check / Money order"
                ],
                "amount_ordered": 165,
                "base_amount_ordered": 165,
                "base_shipping_amount": 45,
                "cc_exp_year": "0",
                "cc_last4": null,
                "cc_ss_start_month": "0",
                "cc_ss_start_year": "0",
                "entity_id": 134,
                "method": "checkmo",
                "parent_id": 134,
                "shipping_amount": 45
            },
            "status_histories": [],
            "extension_attributes": {
                "shipping_assignments": [
                    {
                        "shipping": {
                            "address": {
                                "address_type": "shipping",
                                "city": "California",
                                "company": "Company",
                                "country_id": "US",
                                "email": "test@gmail.com",
                                "entity_id": 267,
                                "firstname": "john",
                                "lastname": "harrison",
                                "parent_id": 134,
                                "postcode": "12345",
                                "region": "New York",
                                "region_code": "NY",
                                "region_id": 43,
                                "street": [
                                    "Street Address"
                                ],
                                "telephone": "2313131312"
                            },
                            "method": "flatrate_flatrate",
                            "total": {
                                "base_shipping_amount": 45,
                                "base_shipping_discount_amount": 0,
                                "base_shipping_discount_tax_compensation_amnt": 0,
                                "base_shipping_incl_tax": 45,
                                "base_shipping_tax_amount": 0,
                                "shipping_amount": 45,
                                "shipping_discount_amount": 0,
                                "shipping_discount_tax_compensation_amount": 0,
                                "shipping_incl_tax": 45,
                                "shipping_tax_amount": 0
                            }
                        },
                        "items": [
                            {
                                "amount_refunded": 0,
                                "applied_rule_ids": "4,11",
                                "base_amount_refunded": 0,
                                "base_cost": 8.5,
                                "base_discount_amount": 15,
                                "base_discount_invoiced": 0,
                                "base_discount_tax_compensation_amount": 0,
                                "base_original_price": 15,
                                "base_price": 15,
                                "base_price_incl_tax": 15,
                                "base_row_invoiced": 0,
                                "base_row_total": 135,
                                "base_row_total_incl_tax": 135,
                                "base_tax_amount": 0,
                                "base_tax_invoiced": 0,
                                "created_at": "2019-09-24 13:16:32",
                                "discount_amount": 15,
                                "discount_invoiced": 0,
                                "discount_percent": 0,
                                "free_shipping": 0,
                                "discount_tax_compensation_amount": 0,
                                "is_qty_decimal": 0,
                                "is_virtual": 0,
                                "item_id": 351,
                                "name": "BELLA DIAMOND GLITTER GRAY 0175",
                                "no_discount": 0,
                                "order_id": 134,
                                "original_price": 15,
                                "price": 15,
                                "price_incl_tax": 15,
                                "product_id": 228,
                                "product_type": "simple",
                                "qty_canceled": 0,
                                "qty_invoiced": 0,
                                "qty_ordered": 9,
                                "qty_refunded": 0,
                                "qty_shipped": 0,
                                "quote_item_id": 1378,
                                "row_invoiced": 0,
                                "row_total": 135,
                                "row_total_incl_tax": 135,
                                "row_weight": 0,
                                "sku": "CLBELGLG0175DCMO",
                                "store_id": 1,
                                "tax_amount": 0,
                                "tax_invoiced": 0,
                                "tax_percent": 0,
                                "updated_at": "2019-09-24 13:16:32"
                            }
                        ]
                    }
                ],
                "rewards_discount": 0,
                "rewards_spend": 0,
                "rewards_earn": 3770
            }
        }
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "customer_email",
                        "value": "test@gmail.com",
                        "condition_type": "eq"
                    }
                ]
            }
        ]
    },
    "total_count": 2
}

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.