php 使用客户令牌获取订单列表- Magento 2 Rest API

h4cxqtbf  于 2023-04-10  发布在  PHP
关注(0)|答案(3)|浏览(134)

我正在尝试使用下面的URL获取订单列表

GET METHOD - http://magento.local/index.php/rest/V1/orders?searchCriteria
Header: Authorization: Bearer TOKEN(Customer Token)

但它给出了如下的回应,

{
    "message": "Consumer is not authorized to access %resources",
    "parameters": {
        "resources": "Magento_Sales::sales"
    }
}

使用客户令牌获取订单列表需要做什么?

nx7onnlm

nx7onnlm1#

不幸的是,它还没有在Magento_Sales::sales中实现,但是你可以扩展你自己的API。https://github.com/magento/magento2/issues/3552

tcomlyy6

tcomlyy62#

对于获取客户订单历史记录,使用searchCriteria并传递customer_email id。
按照下面的Magento 2 API获取客户订单历史记录。
请求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
方法:GET

回复:

{
    "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
}
kmynzznz

kmynzznz3#

Magento默认顺序API与管理员进行身份验证。如果您想与客户进行身份验证。
请创建一个新的自定义模块,然后创建自己的新API端点。
Vender\Module\etc\webapi.xml文件中添加新的API端点。

<!--Get Order by ID-->
<route url="/V1/ca-orders/:id" method="GET">
    <service class="Magento\Sales\Api\OrderRepositoryInterface" method="get"/>
    <resources>
        <resource ref="self" />
    </resources>
</route>
<!--Get Order list-->
<route url="/V1/ca-orders" method="GET">
    <service class="Magento\Sales\Api\OrderRepositoryInterface" method="getList"/>
    <resources>
        <resource ref="self" />
    </resources>
</route>

现在你可以使用url http://magento.local/index.php/rest/V1/ca-orders?searchCriteria和客户认证令牌。
谢谢你!

相关问题