Reputation: 337
I am processing a json string using $input = json_decode($raw, true);
and feeding values into an array with the objective of outputting that array as a purchase order receipt.
It reads most of the json fine, except the "product_id" within the first (and in this case only) line item array of "line_items" (sample below). "line items" is itself a sub-array of "order".
Whilst it reads most of the array $input["order"]["line_items"][0]
OK, it interprets
json string sample:
"line_items": [{
"id": 124,
"subtotal": "332.50",
"subtotal_tax": "66.50",
"total": "0.00",
"total_tax": "0.00",
"price": "0.00",
"quantity": 1,
"tax_class": null,
"name": "Hydraflex Mattress",
"product_id": 1037,
"sku": "",
"meta": [{
"key": "pa_size",
"label": "Available Sizes",
"value": "UK Standard Single"
}]
Result of var_dump($input["order"]["line_items"][0]);
:
array(12) {
["id"]=> int(124)
["subtotal"]=> string(6) "332.50"
["subtotal_tax"]=> string(5) "66.50"
["total"]=> string(4) "0.00"
["total_tax"]=> string(4) "0.00"
["price"]=> string(4) "0.00"
["quantity"]=> int(1)
["tax_class"]=> NULL
["name"]=> string(18) "Hydraflex Mattress"
["product_id"]=> bool(true)
["sku"]=> string(0) ""
["meta"]=> array(1) {
[0]=> array(3) {
["key"]=> string(7) "pa_size"
["label"]=> string(15) "Available Sizes"
["value"]=> string(18) "UK Standard Single"
}
}
}
Main code:
$prices = array(
5928 => 34.48, //Sleepsac;
5924 => 99.99,
5925 => 99.99,
5926 => 99.99,
5927 => 99.99,
1037 => 105.67
);
$raw = file_get_contents("webhook_sample.json");
$input = json_decode($raw, true);
// create an initial array with required fields set to null
$output = array(
"po_number" => NULL,
"item" => NULL,
"quantity" => NULL,
"invoice_price" => NULL,
"invoice_address" => NULL,
"delivery_address" => NULL,
"instructions" => NULL
);
function mailout($message) {
$to = '[email protected]';
$subject = 'the subject';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
// provides a check to see if the field is set and whether it's blank.
function conditional_return($input, $multi_input = false, $multi_last = false) {
if (isset($input)) {
if (!empty($input)) {
if ($multi_input && !$multi_last) {
// if it's not the last one
return $input.', ';
} else {
// if it is the last one or if it's a solo entry
return $input;
}
} else {
return "";
}
} else {
return "";
}
}
// create and populate array for address
$shipping_address = array();
$shipping_address["address_1"] = $input["order"]["shipping_address"]["address_1"];
$shipping_address["address_2"] = $input["order"]["shipping_address"]["address_2"];
$shipping_address["city"] = $input["order"]["shipping_address"]["city"];
$shipping_address["state"] = $input["order"]["shipping_address"]["state"];
$shipping_address["postcode"] = $input["order"]["shipping_address"]["postcode"];
// strip out any address elements that are not populated
foreach ($shipping_address as $key => $value) {
if ($value == "") {
unset($shipping_address[$key]);
}
}
// use remaining elements to create text address
$shipping_address_string = implode(", ", $shipping_address);
$number_of_items = count($input["order"]["line_items"]);
for ($i=0; $i < $number_of_items; $i++) {
if ($input["order"]["line_items"][$i]["product_id"] = 1037 ||
$input["order"]["line_items"][$i]["product_id"] = 0000 ||
$input["order"]["line_items"][$i]["product_id"] = 0000 ||) {
// Set the output parameters
echo "variable i is ".$i."<br>";
setlocale(LC_MONETARY,"en_EN");
$output["item"] = $input["order"]["line_items"][$i]["name"];
$output["quantity"] = $input["order"]["line_items"][$i]["quantity"];
$output["invoice_price"] = $prices[$input["order"]["line_items"][$i]["product_id"]];
$output["invoice_address"] = "Mammoth Technologies, Office 3, Yarm Road, Stockton-on-Tees, TS18 3NA, UK";
$output["delivery_address"] = $shipping_address_string;
$output["instructions"] = $input["order"]["note"];
// write output string
$output_string = "";
foreach ($output as $key => $value) {
$output_string .= $key.": ".$value."<br>";
}
echo $output_string;
var_dump($prices);
echo "<br><br>";
echo($input["order"]["line_items"][0]["product_id"]);
echo "<br><br>";
var_dump($input["order"]["line_items"][0]);
// mailout($output_string);
// file_put_contents("/outbox/dermatherapy_PO_".$input["order"]["id"]."_".$input["order"]["line_items"][$i]["product_id"].".txt", $output_string);
}
Upvotes: 0
Views: 1034
Reputation: 22158
I locate the problem. In your condition, you are making an assignment, not a comparison. Your code is this:
if ($input["order"]["line_items"][$i]["product_id"] = 1037 ||
$input["order"]["line_items"][$i]["product_id"] = 0000 ||
$input["order"]["line_items"][$i]["product_id"] = 0000 ||)
And the correct code should be
if ($input["order"]["line_items"][$i]["product_id"] === 1037 ||
$input["order"]["line_items"][$i]["product_id"] === 0000 ||
$input["order"]["line_items"][$i]["product_id"] === 0000)
This is because you are assign the 0000
value to the product id, and it is a boolean.
Upvotes: 2