這是我在郵遞員中的 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_refunded 值”。這是我現在擁有的代碼,但我不明白如何獲得 amount_refunded 值。
請注意 ->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_refunded。
uj5u.com熱心網友回復:
假設您的 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;
uj5u.com熱心網友回復:
您可以使用 Laravel 集合來獲取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#available-methods
uj5u.com熱心網友回復:
假設您顯示的 JSON 是 的內容$response,那么 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329484.html
