Home > Software engineering >  Trying to loop through all the objects in my json to get all the amount_refunded in php
Trying to loop through all the objects in my json to get all the amount_refunded in php

Time:10-22

This is my json in postman:

     [
    [
        {
            "amount_refunded": 1,
            "base_amount_refunded": 1,
            "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": 1,
            "base_price": 1,
            "base_price_incl_tax": 1,
            "base_row_invoiced": 1,
            "base_row_total": 1,
            "base_row_total_incl_tax": 1,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "base_tax_refunded": 0,
            "created_at": "2021-10-04 13:02:36",
            "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,
            "is_virtual": 0,
            "item_id": 166,
            "name": "Configurable Product 16 - option 21",
            "no_discount": 0,
            "order_id": 85,
            "original_price": 1,
            "price": 1,
            "price_incl_tax": 1,
            "product_id": 1234,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 164,
            "row_invoiced": 1,
            "row_total": 1,
            "row_total_incl_tax": 1,
            "row_weight": 1,
            "sku": "Configurable Product 16 - option 21",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "tax_refunded": 0,
            "updated_at": "2021-10-15 09:28:44",
            "weee_tax_applied": "[]",
            "weight": 1
        }
    ],
    [
        {
            "amount_refunded": 80,
            "applied_rule_ids": "1",
            "base_amount_refunded": 80,
            "base_discount_amount": 8,
            "base_discount_invoiced": 8,
            "base_discount_refunded": 8,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_original_price": 80,
            "base_price": 80,
            "base_price_incl_tax": 86.6,
            "base_row_invoiced": 80,
            "base_row_total": 72,
            "base_row_total_incl_tax": 77.94,
            "base_tax_amount": 5.94,
            "base_tax_invoiced": 5.94,
            "base_tax_refunded": 5.94,
            "created_at": "2021-10-15 11:23:54",
            "discount_amount": 8,
            "discount_invoiced": 8,
            "discount_percent": 10,
            "discount_refunded": 8,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 169,
            "name": "Ski vest 92",
            "no_discount": 0,
            "order_id": 88,
            "original_price": 80,
            "price": 80,
            "price_incl_tax": 86.6,
            "product_id": 1267,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 171,
            "row_invoiced": 80,
            "row_total": 72,
            "row_total_incl_tax": 77.94,
            "row_weight": 5,
            "sku": "Ski vest 92",
            "store_id": 1,
            "tax_amount": 5.94,
            "tax_invoiced": 5.94,
            "tax_percent": 8.25,
            "tax_refunded": 5.94,
            "updated_at": "2021-10-15 11:25:07",
            "weee_tax_applied": "[]",
            "weight": 5
        },
        {
            "amount_refunded": 0,
            "applied_rule_ids": "1",
            "base_amount_refunded": 0,
            "base_discount_amount": 2,
            "base_discount_invoiced": 2,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_original_price": 20,
            "base_price": 20,
            "base_price_incl_tax": 20,
            "base_row_invoiced": 20,
            "base_row_total": 18,
            "base_row_total_incl_tax": 18,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "created_at": "2021-10-15 11:23:54",
            "discount_amount": 2,
            "discount_invoiced": 2,
            "discount_percent": 10,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 170,
            "name": "testcron",
            "no_discount": 0,
            "order_id": 88,
            "original_price": 20,
            "price": 20,
            "price_incl_tax": 20,
            "product_id": 1260,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 0,
            "qty_shipped": 1,
            "quote_item_id": 173,
            "row_invoiced": 20,
            "row_total": 18,
            "row_total_incl_tax": 18,
            "row_weight": 2,
            "sku": "testcron",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "updated_at": "2021-10-15 11:24:39",
            "weee_tax_applied": "[]",
            "weight": 2
        },
        {
            "amount_refunded": 0,
            "applied_rule_ids": "1",
            "base_amount_refunded": 0,
            "base_discount_amount": 0.5,
            "base_discount_invoiced": 0.5,
            "base_discount_tax_compensation_amount": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_original_price": 5,
            "base_price": 5,
            "base_price_incl_tax": 5.41,
            "base_row_invoiced": 5,
            "base_row_total": 4.5,
            "base_row_total_incl_tax": 4.87,
            "base_tax_amount": 0.37,
            "base_tax_invoiced": 0.37,
            "created_at": "2021-10-15 11:23:54",
            "discount_amount": 0.5,
            "discount_invoiced": 0.5,
            "discount_percent": 10,
            "free_shipping": 0,
            "discount_tax_compensation_amount": 0,
            "discount_tax_compensation_invoiced": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 171,
            "name": "time",
            "no_discount": 0,
            "order_id": 88,
            "original_price": 5,
            "price": 5,
            "price_incl_tax": 5.41,
            "product_id": 1261,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 0,
            "qty_shipped": 1,
            "quote_item_id": 174,
            "row_invoiced": 5,
            "row_total": 4.5,
            "row_total_incl_tax": 4.87,
            "row_weight": 2,
            "sku": "time",
            "store_id": 1,
            "tax_amount": 0.37,
            "tax_invoiced": 0.37,
            "tax_percent": 8.25,
            "updated_at": "2021-10-15 11:24:39",
            "weee_tax_applied": "[]",
            "weight": 2
        }
    ],
    [
        {
            "amount_refunded": 80,
            "base_amount_refunded": 80,
            "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": 80,
            "base_price": 80,
            "base_price_incl_tax": 80,
            "base_row_invoiced": 80,
            "base_row_total": 80,
            "base_row_total_incl_tax": 80,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "base_tax_refunded": 0,
            "created_at": "2021-10-19 08:49:35",
            "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,
            "is_virtual": 0,
            "item_id": 172,
            "name": "Ski vest 92",
            "no_discount": 0,
            "order_id": 89,
            "original_price": 80,
            "price": 80,
            "price_incl_tax": 80,
            "product_id": 1267,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 175,
            "row_invoiced": 80,
            "row_total": 80,
            "row_total_incl_tax": 80,
            "row_weight": 5,
            "sku": "Ski vest 92",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "tax_refunded": 0,
            "updated_at": "2021-10-19 09:06:19",
            "weee_tax_applied": "[]",
            "weight": 5
        },
        {
            "amount_refunded": 5,
            "base_amount_refunded": 5,
            "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": 5,
            "base_price": 5,
            "base_price_incl_tax": 5,
            "base_row_invoiced": 5,
            "base_row_total": 5,
            "base_row_total_incl_tax": 5,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "base_tax_refunded": 0,
            "created_at": "2021-10-19 08:49:35",
            "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,
            "is_virtual": 0,
            "item_id": 173,
            "name": "time",
            "no_discount": 0,
            "order_id": 89,
            "original_price": 5,
            "price": 5,
            "price_incl_tax": 5,
            "product_id": 1261,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 177,
            "row_invoiced": 5,
            "row_total": 5,
            "row_total_incl_tax": 5,
            "row_weight": 2,
            "sku": "time",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "tax_refunded": 0,
            "updated_at": "2021-10-19 09:06:19",
            "weee_tax_applied": "[]",
            "weight": 2
        }
    ],
    [
        {
            "amount_refunded": 5,
            "base_amount_refunded": 5,
            "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": 5,
            "base_price": 5,
            "base_price_incl_tax": 5.41,
            "base_row_invoiced": 5,
            "base_row_total": 5,
            "base_row_total_incl_tax": 5.41,
            "base_tax_amount": 0.41,
            "base_tax_invoiced": 0.41,
            "base_tax_refunded": 0.41,
            "created_at": "2021-10-19 10:27:53",
            "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,
            "is_virtual": 0,
            "item_id": 176,
            "name": "Configurable Product 16",
            "no_discount": 0,
            "order_id": 91,
            "original_price": 5,
            "price": 5,
            "price_incl_tax": 5.41,
            "product_id": 1253,
            "product_type": "configurable",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 1,
            "quote_item_id": 180,
            "row_invoiced": 5,
            "row_total": 5,
            "row_total_incl_tax": 5.41,
            "row_weight": 1,
            "sku": "Configurable Product 16 - option 9",
            "store_id": 1,
            "tax_amount": 0.41,
            "tax_invoiced": 0.41,
            "tax_percent": 8.25,
            "tax_refunded": 0.41,
            "updated_at": "2021-10-19 10:50:02",
            "weee_tax_applied": "[]",
            "weight": 1,
            "product_option": {
                "extension_attributes": {
                    "configurable_item_options": [
                        {
                            "option_id": "212",
                            "option_value": 478
                        },
                        {
                            "option_id": "213",
                            "option_value": 480
                        }
                    ]
                }
            }
        },
        {
            "amount_refunded": 0,
            "base_amount_refunded": 0,
            "base_discount_amount": 0,
            "base_discount_invoiced": 0,
            "base_discount_refunded": 0,
            "base_discount_tax_compensation_invoiced": 0,
            "base_discount_tax_compensation_refunded": 0,
            "base_price": 0,
            "base_row_invoiced": 0,
            "base_row_total": 0,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "base_tax_refunded": 0,
            "created_at": "2021-10-19 10:27:53",
            "discount_amount": 0,
            "discount_invoiced": 0,
            "discount_percent": 0,
            "discount_refunded": 0,
            "free_shipping": 0,
            "discount_tax_compensation_invoiced": 0,
            "discount_tax_compensation_refunded": 0,
            "is_qty_decimal": 0,
            "is_virtual": 0,
            "item_id": 177,
            "name": "Configurable Product 16 - option 9",
            "no_discount": 0,
            "order_id": 91,
            "original_price": 0,
            "parent_item_id": 176,
            "price": 0,
            "product_id": 1222,
            "product_type": "simple",
            "qty_canceled": 0,
            "qty_invoiced": 1,
            "qty_ordered": 1,
            "qty_refunded": 1,
            "qty_shipped": 0,
            "quote_item_id": 181,
            "row_invoiced": 0,
            "row_total": 0,
            "row_weight": 0,
            "sku": "Configurable Product 16 - option 9",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "tax_refunded": 0,
            "updated_at": "2021-10-19 10:50:02",
            "weight": 1,
            "parent_item": {
                "amount_refunded": 5,
                "base_amount_refunded": 5,
                "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": 5,
                "base_price": 5,
                "base_price_incl_tax": 5.41,
                "base_row_invoiced": 5,
                "base_row_total": 5,
                "base_row_total_incl_tax": 5.41,
                "base_tax_amount": 0.41,
                "base_tax_invoiced": 0.41,
                "base_tax_refunded": 0.41,
                "created_at": "2021-10-19 10:27:53",
                "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,
                "is_virtual": 0,
                "item_id": 176,
                "name": "Configurable Product 16",
                "no_discount": 0,
                "order_id": 91,
                "original_price": 5,
                "price": 5,
                "price_incl_tax": 5.41,
                "product_id": 1253,
                "product_type": "configurable",
                "qty_canceled": 0,
                "qty_invoiced": 1,
                "qty_ordered": 1,
                "qty_refunded": 1,
                "qty_shipped": 1,
                "quote_item_id": 180,
                "row_invoiced": 5,
                "row_total": 5,
                "row_total_incl_tax": 5.41,
                "row_weight": 1,
                "sku": "Configurable Product 16 - option 9",
                "store_id": 1,
                "tax_amount": 0.41,
                "tax_invoiced": 0.41,
                "tax_percent": 8.25,
                "tax_refunded": 0.41,
                "updated_at": "2021-10-19 10:50:02",
                "weee_tax_applied": "[]",
                "weight": 1,
                "product_option": {
                    "extension_attributes": {
                        "configurable_item_options": [
                            {
                                "option_id": "212",
                                "option_value": 478
                            },
                            {
                                "option_id": "213",
                                "option_value": 480
                            }
                        ]
                    }
                }
            },
            "row_total_incl_tax": 0,
            "base_row_total_incl_tax": 0
        }
    ]
]

I am trying to loop threough this json so I can get all the "amount_refunded values". This is the code that I have now but I don't understand how I can get the amount_refunded values.

Note that ->items is used and is correct because it needs to get all the items.

public function getRefundedOrders(Request $request)
{
    try{
        $response = Http::withToken($request->post('token'))
        ->get($request->post('URL').'/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=base_subtotal_refunded&searchCriteria[filter_groups][0][filters][0][condition_type]=gt');

    }
    catch(Throwaway $e){
        Log::error($e);
    }


    $refunds = json_decode($response);

    $user = Users::whereUrl($request->post('URL'))->firstOrFail();

    $refundedItems = [];


    foreach($refunds->items as $product){
        array_push($refundedItems, $product->items);
    }

    $refundedProducts = [];


    if (isset($response['message']))
    {
        return response()->json(['type' => 'error', 'message' => $response['message']]);
    }
    
    return $refundedItems;
   
}

I don't understand how I can loop through this so I can get all the amount_refunded from every object inside of this json.

CodePudding user response:

Assuming your json is missing a closing "]", then decode it into an associative array using :

$string = '--yourjsongoeshere--';
$data = json_decode($string, true);

then set your total refunds to 0, loop through the arrays and add the total refunds to it.

$totalrefund = 0;
foreach($data as $datum) { 
    foreach($datum as $entry) { 
        $totalrefund  = $entry['amount_refunded'];
    }
}
echo $totalrefund;

CodePudding user response:

You can use Laravel collection to get an array of amount_refunded.

$refunds = collect(json_decode($response))
    ->flatten()
    ->pluck('amount_refunded')
    ->toArray();

To get all data filtered by amount_refunded > 0

$refunds = collect(json_decode($response))->flatten()->filter(function ($item) {
    return $item->amount_refunded > 0;
})->toArray();

Docs: https://laravel.com/docs/8.x/collections#available-methods

CodePudding user response:

Assuming the JSON you've shown is the contents of $response, there is no "items" mentioned anywhere in the JSON, so it's not clear why you included that or why you think it's working. It would definitely cause errors / warnings if you tried to use the code you've shown with that data.

Your JSON structure is an outer array, with multiple smaller arrays inside it - and each of those smaller arrays can have one or more entries.

This simple code will just output the value of each "amount_refunded" field, by looping through the outer array, and then looping through each of the inner ones:

$refunds = json_decode($response);

foreach($refunds as $refund) { 
  foreach ($refund as $item)
    echo $item->amount_refunded.PHP_EOL; //PHP_EOL just adds a newline character. Use <br/> instead for HTML output.
}

Working demo: http://sandbox.onlinephpfunctions.com/code/728b81aa408b811b9d98a0c01d3dc782c4f05951

  • Related