Как получить историю заказов клиента в API Magento 2?
Получить список всех предыдущих заказов клиента, используя Токен клиента в API.
3 answers
Решение 1
Вы можете создать свой собственный API для того же самого;
С помощью filter Builder
вы можете использовать критерии поиска
$customerId = $this->userContext->getUserId();
$filter = $this->filterBuilder
->setField('customer_id')
->setValue($customerId)
->setConditionType('eq')
->create();
$this->searchCriteriaBuilder->addFilters([$filter]);
$searchCriteria = $this->searchCriteriaBuilder->create();
Решение 2
Magento использует ресурс Magento_Sales::sales
из-за этого мы не можем получить к нему доступ с помощью токена клиента , поэтому для того же нам нужно переопределить Конкретный API с помощью
<resource ref="self" />
И с помощью этого мы можем получить доступ ко всем заказам конкретного клиента, используя Токен клиента в API Magento.
<route url="/V1/orders" method="GET">
<service class="Magento\Sales\Api\OrderRepositoryInterface" method="getList"/>
<resources>
<resource ref="self" />
</resources>
</route>
Собственный API Magento не имеет конечной точки для получения заказов с помощью токена клиента, у него есть только один для интеграции или токен администратора.
Вы можете использовать или посмотреть на Божество, как они подготовили расширение magento, которое предоставляет такую конечную точку: https://github.com/deity-io/falcon-magento2-module/blob/master/src/etc/webapi.xml#L132
Для раскрытия информации, я написал этот фрагмент кода, но я больше не являюсь разработчиком этого модуля.
Есть альтернатива, если вы используя некоторый уровень промежуточного программного обеспечения для общения с API Magento2, который вы контролируете и можете использовать там токен администратора. Первый запрос customers/me
конечной точки с использованием токена клиента для извлечения текущих данных клиента, а затем запрос /orders
конечной точки с токеном администратора, предоставляющим полученный идентификатор клиента в searchCriteria
.
Для получения истории заказов клиентов используйте searchCriteria
и переданный customer_email
идентификатор.
Следуйте приведенному ниже API Magento 2, чтобы получить историю заказов клиентов.
URL-адрес запроса: https://magento.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=customer_email&searchCriteria[filter_groups][0][filters][0][value][email protected]
Метод: ПОЛУЧИТЬ
Ответ:
{
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"condition_type": "eq"
}
]
}
]
},
"total_count": 2
}