Hi All,
I have json that contains multiple products from an order. I convert this json into an array, then i am trying to run a foreach over each product to:
- extract the key and array (quantity and weight)
- Multiply the key and array together
- Add all the sums together to get a total (across multiple arrays)
But i am having a lot of trouble doing so.
Here is my code, any idea how i can achieve this?
// my json
$products = '[
{
"id": 61,
"order_id": 40,
"product_id": 10,
"order_address_id": 43,
"name": "Large Bag ",
"sku": "111222",
"type": "physical",
"base_price": "0.0000",
"price_ex_tax": "0.0000",
"price_inc_tax": "0.0000",
"price_tax": "0.0000",
"base_total": "0.0000",
"total_ex_tax": "0.0000",
"total_inc_tax": "0.0000",
"total_tax": "0.0000",
"weight": "15.0000",
"quantity": 1,
"base_cost_price": "0.0000",
"cost_price_inc_tax": "0.0000",
"cost_price_ex_tax": "0.0000",
"cost_price_tax": "0.0000",
"is_refunded": false,
"quantity_refunded": 0,
"refund_amount": "0.0000",
"return_id": 0,
"wrapping_name": "",
"base_wrapping_cost": "0.0000",
"wrapping_cost_ex_tax": "0.0000",
"wrapping_cost_inc_tax": "0.0000",
"wrapping_cost_tax": "0.0000",
"wrapping_message": "",
"quantity_shipped": 1,
"event_name": null,
"event_date": "",
"fixed_shipping_cost": "0.0000",
"ebay_item_id": "",
"ebay_transaction_id": "",
"option_set_id": null,
"parent_order_product_id": null,
"is_bundled_product": false,
"bin_picking_number": "",
"external_id": null,
"fulfillment_source": "",
"applied_discounts": [],
"product_options": [],
"configurable_fields": []
},
{
"id": 62,
"order_id": 40,
"product_id": 17,
"order_address_id": 41,
"name": "Rod",
"sku": "11112223,
"type": "physical",
"base_price": "0.0000",
"price_ex_tax": "0.0000",
"price_inc_tax": "0.0000",
"price_tax": "0.0000",
"base_total": "0.0000",
"total_ex_tax": "0.0000",
"total_inc_tax": "0.0000",
"total_tax": "0.0000",
"weight": "1.0000",
"quantity": 2,
"base_cost_price": "0.0000",
"cost_price_inc_tax": "0.0000",
"cost_price_ex_tax": "0.0000",
"cost_price_tax": "0.0000",
"is_refunded": false,
"quantity_refunded": 0,
"refund_amount": "0.0000",
"return_id": 0,
"wrapping_name": "",
"base_wrapping_cost": "0.0000",
"wrapping_cost_ex_tax": "0.0000",
"wrapping_cost_inc_tax": "0.0000",
"wrapping_cost_tax": "0.0000",
"wrapping_message": "",
"quantity_shipped": 2,
"event_name": null,
"event_date": "",
"fixed_shipping_cost": "0.0000",
"ebay_item_id": "",
"ebay_transaction_id": "",
"option_set_id": null,
"parent_order_product_id": null,
"is_bundled_product": false,
"bin_picking_number": "",
"external_id": null,
"fulfillment_source": "",
"applied_discounts": [],
"product_options": [],
"configurable_fields": []
}
]';
//convert json to array
$products = json_decode($products, true);
foreach($products as $product){
$weight = $product["weight"] ;
$quantity = $product["quantity"];
//this doesnt work, how cani perform the operation and add the value to an array?
$totalWeight = array($product["weight"] * $product["quantity"]);
$total = array_sum($totalWeight);
}