这是我的json在邮差:
[
[
{
"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
}
]
]
我试图通过这个json循环,所以我可以得到所有的amount_returned值。这是我现在拥有的代码,但我不明白如何获得amount_returned值。
注意->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;
}
我不明白我怎么才能循环通过这个,这样我就可以从这个json中的每个对象中获得所有的amount_refund。
假设您的json缺少结束的"]",然后使用:
将其解码为关联数组:$string = '--yourjsongoeshere--';
$data = json_decode($string, true);
然后将总退款设置为0,循环遍历数组并将总退款添加到其中。
$totalrefund = 0;
foreach($data as $datum) {
foreach($datum as $entry) {
$totalrefund += $entry['amount_refunded'];
}
}
echo $totalrefund;
您可以使用Laravel collection来获取amount_refunded
的数组。
$refunds = collect(json_decode($response))
->flatten()
->pluck('amount_refunded')
->toArray();
获取amount_refunded > 0
过滤的所有数据
$refunds = collect(json_decode($response))->flatten()->filter(function ($item) {
return $item->amount_refunded > 0;
})->toArray();
文档:https://laravel.com/docs/8.x/collections可用方法
假设您显示的JSON是$response
的内容,则没有"item "在JSON中提到的任何地方,所以不清楚为什么要包含它,或者为什么您认为它可以工作。如果您试图使用与该数据一起显示的代码,则肯定会导致错误/警告。
JSON结构是一个外部数组,里面有多个较小的数组,每个较小的数组可以有一个或多个条目。
这段简单的代码将输出每个"amount_refunded"的值字段,通过循环遍历外部数组,然后循环遍历每个内部数组:
$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.
}
工作演示:http://sandbox.onlinephpfunctions.com/code/728b81aa408b811b9d98a0c01d3dc782c4f05951