0

How i can modify order response only needed fields

Request url

http://localhost/magento2/index.php/rest/V1/orders?searchCriteria[currentPage]=1&searchCriteria[pageSize]=1

Reponse:

{
    "items": [
        {
            "adjustment_negative": 0,
            "adjustment_positive": 0,
            "base_adjustment_negative": 0,
            "base_adjustment_positive": 0,
            "base_currency_code": "USD",
            "base_discount_amount": 0,
            "base_discount_invoiced": 0,
            "base_discount_refunded": 0,
            "base_grand_total": 78.61,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_shipping_amount": 5,
            "base_shipping_discount_amount": 0,
            "base_shipping_discount_tax_compensation_amnt": 0,
            "base_shipping_incl_tax": 5,
            "base_shipping_invoiced": 5,
            "base_shipping_refunded": 5,
            "base_shipping_tax_amount": 0,
            "base_shipping_tax_refunded": 0,
            "base_subtotal": 68,
            "base_subtotal_incl_tax": 73.61,
            "base_subtotal_invoiced": 68,
            "base_subtotal_refunded": 68,
            "base_tax_amount": 5.61,
            "base_tax_invoiced": 5.61,
            "base_tax_refunded": 5.61,
            "base_total_due": 0,
            "base_total_invoiced": 78.61,
            "base_total_invoiced_cost": 0,
            "base_total_offline_refunded": 78.61,
            "base_total_paid": 78.61,
            "base_total_refunded": 78.61,
            "base_to_global_rate": 1,
            "base_to_order_rate": 1,
            "billing_address_id": 2,
            "created_at": "2020-02-04 07:26:46",
            "customer_dob": "1973-12-15 00:00:00",
            "customer_email": "roni_cost@example.com",
            "customer_firstname": "Veronica",
            "customer_gender": 2,
            "customer_group_id": 1,
            "customer_id": 1,
            "customer_is_guest": 0,
            "customer_lastname": "Costello",
            "customer_note_notify": 1,
            "discount_amount": 0,
            "discount_invoiced": 0,
            "discount_refunded": 0,
            "entity_id": 1,
            "global_currency_code": "USD",
            "grand_total": 78.61,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "increment_id": "000000001",
            "is_virtual": 0,
            "order_currency_code": "USD",
            "protect_code": "c5830155c66b416658dada71ad825ede",
            "quote_id": 1,
            "shipping_amount": 5,
            "shipping_description": "Flat Rate - Fixed",
            "shipping_discount_amount": 0,
            "shipping_discount_tax_compensation_amount": 0,
            "shipping_incl_tax": 5,
            "shipping_invoiced": 5,
            "shipping_refunded": 5,
            "shipping_tax_amount": 0,
            "shipping_tax_refunded": 0,
            "state": "closed",
            "status": "closed",
            "store_currency_code": "USD",
            "store_id": 1,
            "store_name": "Main Website\nMain Website Store\nDefault Store View",
            "store_to_base_rate": 0,
            "store_to_order_rate": 0,
            "subtotal": 68,
            "subtotal_incl_tax": 73.61,
            "subtotal_invoiced": 68,
            "subtotal_refunded": 68,
            "tax_amount": 5.61,
            "tax_invoiced": 5.61,
            "tax_refunded": 5.61,
            "total_due": 0,
            "total_invoiced": 78.61,
            "total_item_count": 1,
            "total_offline_refunded": 78.61,
            "total_paid": 78.61,
            "total_qty_ordered": 1,
            "total_refunded": 78.61,
            "updated_at": "2020-02-04 07:26:52",
            "weight": 1,
            "items": [
                {
                    "amount_refunded": 68,
                    "base_amount_refunded": 68,
                    "base_discount_amount": 0,
                    "base_discount_invoiced": 0,
                    "base_discount_refunded": 0,
                    "base_discount_tax_compensation_amount": 0,
                    "base_discount_tax_compensation_invoiced": 0,
                    "base_discount_tax_compensation_refunded": 0,
                    "base_original_price": 68,
                    "base_price": 68,
                    "base_price_incl_tax": 73.61,
                    "base_row_invoiced": 68,
                    "base_row_total": 68,
                    "base_row_total_incl_tax": 73.61,
                    "base_tax_amount": 5.61,
                    "base_tax_invoiced": 5.61,
                    "base_tax_refunded": 5.61,
                    "created_at": "2020-02-04 07:26:46",
                    "discount_amount": 0,
                    "discount_invoiced": 0,
                    "discount_percent": 0,
                    "discount_refunded": 0,
                    "free_shipping": 0,
                    "discount_tax_compensation_amount": 0,
                    "discount_tax_compensation_invoiced": 0,
                    "discount_tax_compensation_refunded": 0,
                    "is_qty_decimal": 0,
                    "item_id": 1,
                    "name": "Valeria Two-Layer Tank",
                    "no_discount": 0,

etc\webapi_rest\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="order_rest_api_response_change" type="Custom\ExtendedSales\Plugin\Api\OrderRepository" />
    </type>
</config>

i wrote plugin but i how i can modify Response data to only needed fields for me?

0

API do returns fixed response. If your worry is about optimisation or readability, you can add a custom API point that queries the same repository and then you can parse the response so that only what you need is sent.

Also, you may be interested in GraphQl which by design can return any subset of the possible returned data.

| improve this answer | |
  • Thank you for your response...... – prabhakaran7 Jul 2 at 12:43

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.