I have part of a JSON feed that I need to segment based on variable existance, but its a nested array and I am not quite sure how to write it down.
Here is the code
$postData += array(
'LineItems' => array(
array(
'ItemName' => $_POST['milestone_1_detail'],
'Description' => 'Minimum Guarantee - Installment 1',
'Quantity' => 1,
'Price' => $_POST['milestone_1_amount'],
'SellComm' => 0,
'BuyComm' => 0,
'Accept' => true
),
array(
'ItemName' => $_POST['milestone_2_detail'],
'Description' => 'Minimum Guarantee - Installment 2',
'Quantity' => 1,
'Price' => $_POST['milestone_2_amount'],
'SellComm' => 0,
'BuyComm' => 0,
'Accept' => true
),
array(
'ItemName' => $_POST['milestone_3_detail'],
'Description' => 'Minimum Guarantee - Installment 3',
'Quantity' => 1,
'Price' => $_POST['milestone_3_amount'],
'SellComm' => 0,
'BuyComm' => 0,
'Accept' => true
)
)
);
As you can see, its three (potentially) items all listed in a parent array
I need to do a basic
if ($_POST['milestone_1_amount']) { LineItem1 }
if ($_POST['milestone_2_amount']) { LineItem2 }
if ($_POST['milestone_3_amount']) { LineItem3 }
type condition for all three items,that will still keep those child arrays in the parent array but ONLY show the lineItems that have a value set, but not quite sure how to correctly write the code and retain the array format
Thank you in advance for helping me to stop tearing my hair out
Related
\Cart::add(array(
'id' => $request->test,
'name' => $price->tests->item_name,
'quantity' => 1,
'price' => $price->price,
'attributes' => array(
'lab_logo' => $price->labs->logo,
'lab_name' => $price->labs->name,
'item_number' => $price->tests->item_number
),
));
When i add the same product it increments the item quantity but i want the quantity of the item to be 1 at max.
According to the documentation:
// NOTE: as you can see by default, the quantity update is relative to its current value
// if you want to just totally replace the quantity instead of incrementing or decrementing its current quantity value
// you can pass an array in quantity value like so:
Cart::update(456, array(
'quantity' => array(
'relative' => false,
'value' => 5
),
));
So, if you pass an array on add and override it to not be relative, it should work as expected:
Cart::add(array(
'id' => $request->test,
'name' => $price->tests->item_name,
'quantity' => array(
'relative' => false,
'value' => 1,
),
'price' => $price->price,
'attributes' => array(
'lab_logo' => $price->labs->logo,
'lab_name' => $price->labs->name,
'item_number' => $price->tests->item_number
),
));
I'm developing a plugin that sends WooCommerce order data to a REST API, using a Basic Authentication Header.
As for the WooCommerce order lines, the API documentation tells me to add every order line as an object to the 'lines' array. I'm struggling to set that up.
See the following code for more clarification:
$items = array();
foreach ( $order->get_items() as $item_id => $item_data ) {
$product = $item_data->get_product();
$product_name = $product->get_name();
$item_quantity = $item_data->get_quantity();
$item_total = $item_data->get_total();
$items[] = array(
'unit' => $product_name,
'quantity' => $item_quantity,
'price' => $item_total
);
}
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 20,
'headers' => array(
'username' => '***',
'password' => '***',
'company' => '***'
),
'body' => array(
'employee' => 1,
'debtor' => 1,
'deliveryMethod' => 1,
'deliveryAddress' => array(
'address' => $order->get_billing_address_1(),
'postcode' => $order->get_billing_postcode(),
'city' => $order->get_billing_city(),
'country' => 'NL'
),
'lines' => array(
'unit' => $items['unit'],
'quantity' => $items['quantity'],
'price' => $items['price']
)
)
) );
The WordPress debug log tells me it can't find the index of 'unit', 'quantity' and 'price'.
So your array variable would look similar to the following (basing from your example code):
// print_r($items)
Array(
[0] => ['unit' => '', 'quantity' => '', 'price' = ''],
[1] => ['unit' => '', 'quantity' => '', 'price' = ''],
[2] => ['unit' => '', 'quantity' => '', 'price' = ''],
// etc...
)
The empty strings would just have your data.
You're adding an array to an array, so if you wanted to parse through the $items array you could use foreach:
foreach($items as $item) {
print_r($item);
}
That output should be similar to:
Array
(
[unit] => '',
[quantity] => '',
[price] => ''
)
But it would loop over each array in $items, so in my case using the above code it would print_r() three times.
Another way to visualize it is:
print $items[0]['unit'];
print $items[1]['unit'];
print $items[2]['unit'];
Each array is indexed (unless you define a key) so [0] would be the first array in the $items array, then you're referencing the ['unit'] key.
Here's a link to something that could help you understand more: https://www.geeksforgeeks.org/multidimensional-arrays-in-php/
Hope this helps!
Your array apparently looks ok except few things that is different than the documents.
First use 'line_items' instead of 'lines'.
Now, each order may have one or many products so you have to associate 'line_items' with products. And you're trying to get attributes which are related to those product. You got error because you've not associated quantity and price with product_id according to the docs.
'line_items' => [
[
'product_id' => 123,
'quantity' => 2,
'price' => 100
]
]
Unit is something different. Unit could be get from meta. I guess you're referring unit of dimensions. To get meta you can use it like
'line_items' => [
[
'product_id' => 123,
'quantity' => 2,
'price' => 100,
'meta' => [
"key" => "dimension_unit",
"label" => "Dimension Unit",
"value" => "in"
]
]
]
Here I've used every sample data. You can assign your desire data your way.
Hope this help you and your API should work correctly now!
Question might be difficult to answer but here it goes:-
See I have an market order book in array like this:-
$str = array(
array(
'amount' => 1.5,
'price' => 10,
'user' => 'test',
'filled' => 0,
'status' => 'active',
),
array(
'amount' => 2.5,
'price' => 10,
'user' => 'test1',
'filled' => 0,
'status' => 'active',
),
array(
'amount' => 5,
'price' =>10,
'user' => 'test2',
'filled' => 0,
'status' => 'active',
),
array(
'amount' => 10,
'price' => 10,
'user' => 'test3',
'filled' => 0,
'status' => 'active',
)
);
Here the above arrays shows the name of user selling the amount of a thing and the price per thing. Status is active as nothing is still sold to anyone(filled is 0). Let's say a user named test4 comes into the picture and wants to buy 6 things at price'10'. How do I make so that the first three orders get dynamically filled and also from 'active' status to 'done' or 'partially filled'.
Please help :)
Hi there i need some help to loop an array of items.
The part that I have repeated needs to be looped in order to get multiple items from the database. At this stage I am only to invoice one item where as users generally purchase a few so i need to run a foreach statement I think. Please excuse my ignorance and lack of understanding, I have tried a series of things but they do not seem to work
$json1 = array(
'Date' => $currentDate->format('Y-m-d\TH:i:s'),
'Customer' => array(
'DisplayID' => $phone
),
'CustomerPurchaseOrderNumber' => $ordID,
'Freight' => '0',
'BalanceDueAmount' => '0.0',
'Status' => 'Closed',
'Lines' => /*$orderDetails,*/ array(
array(
'ShipQuantity' => $ShowDetails['quantity'],
'UnitPrice' => $ShowDetails['unit_price'],
'Total' => $ShowDetails['total_price'],
'Item' => array(
'UID' => $productUID,//itemuid(609400gmkit)
),
'Description' => $productTitle. ' ' .$sizeTitle. ' '
.$colorTitle,
'TaxCode' => array(
'UID' => '7d1f2e99-ffe0-4463-9c29-
61d078b392d3'//taxcodeuid(GST)
)
)
),
'Freight' => $shippingprice,//if dg then $10 otherwise $0 unless below
$100
'ShippingMethod' => $ShippingMethod,// if dg the 'MyFreight' otherwise
'transdirect'
);
The portion that i need to loop is as follows :
array(
'ShipQuantity' => $ShowDetails['quantity'],
'UnitPrice' => $ShowDetails['unit_price'],
'Total' => $ShowDetails['total_price'],
'Item' => array(
'UID' => '27d56af8-d3d6-4f0a-b71b-
4586fffbd63a'//itemuid(609400gmkit)
),
'Description' => $productTitle. ' ' .$sizeTitle. ' '
.$colorTitle,
'TaxCode' => array(
'UID' => '7d1f2e99-ffe0-4463-9c29-
61d078b392d3'//taxcodeuid(GST)
)
)*/
I've been wrapping my head around this for a couple of days...
I have several arrays that need to be sort of merged into a single array. The order in which they merge is of great importance and is simply the order in which they appear in the global array (like the example below):
$input1 = array(
array(
'context' => 'aa', 'id' => 1, 'view' => 1, 'update' => 1,
),
array(
'context' => 'bb', 'id' => 2, 'view' => 0, 'update' => 0,
)
);
$input2 = array(
array(
'context' => 'cc', 'id' => 3, 'view' => 0, 'update' => 1,
),
array(
'context' => 'dd', 'id' => 4, 'view' => 0, 'update' => 0,
),
array(
'context' => 'ee', 'id' => 5, 'view' => 1, 'update' => 0,
)
);
$input3 = array(
array(
'context' => 'ff', 'id' => 6, 'view' => 1, 'update' => 1,
),
array(
'context' => 'gg', 'id' => 7, 'view' => 1, 'update' => 0,
),
);
$global = array($input1, $input2, $input3);
Each input array itself consists of several subarrays that are of equal structure; see http://pastebin.com/fQMUjUpB for an example. This pastebin code also includes the desired output.
The output array should contain:
a single level array
a tree-like passthrough upon "merging the next input array", viz. every possible cross-combination of subarrays should be made during a merge between two input arrays
the key of each a combination should be generated as a concatenated string of the corresponding context and id elements (glued with a plus) joined together with an ampersand (&); e.g: context1+id1&context2+id2
For the next merge the previous resulting array should be used in order for the example from above becomes
context1+id1&context2+id2&context3+id3
The resulting viewand update elements are calculated by simply multiplying their corresponding values during merge.
$output = array(
'aa+1&cc+3&ff+6' => array('view' => 0, 'update' => 1),
'aa+1&cc+3&gg+7' => array('view' => 0, 'update' => 0),
'aa+1&dd+4&ff+6' => array('view' => 0, 'update' => 0),
'aa+1&dd+4&gg+7' => array(...),
'aa+1&ee+5&ff+6' => array(...),
'aa+1&ee+5&gg+7' => array(...),
'bb+2&cc+3&ff+6' => array(...),
'bb+2&cc+3&gg+7' => array(...),
'bb+2&dd+4&ff+6' => array(...),
'bb+2&dd+4&gg+7' => array(...),
'bb+2&ee+5&ff+6' => array(...),
'bb+2&ee+5&gg+7' => array(...)
);
How can this be accomplished when looping over $global?
I may have expressed myself quite vaguely (it's really hard to explain!), but hopefully it becomes more clear when you take a look at the pastebin code...
Any help would be greatly appreciated!
Here's a minimal working code so that you can get the general idea (if you want to improve the code, feel free, there's a lot to do!):
function generate_output($globalArray, $context = array(), $view = 1, $update = 1, &$output = array()) {
if(!count($globalArray)) {
$output[implode('&', $context)] = array('view' => $view, 'update' => $update);
}
else {
foreach(reset($globalArray) as $elt) {
$newContext = $context;
$newContext[] = $elt['context'] . '+' . $elt['id'];
generate_output(array_slice($globalArray, 1), $newContext, $view * $elt['view'], $update * $elt['update'], $output);
}
}
return $output;
}
generate_output($global);