Reputation: 11
I am trying to create a new array with suppliers as main ID.
Every supplier can supply multiple products. I want to assign all the products in an a new array.
This code generates a list of products with the following information.
Supplier id Supplier name Product id Product name Quantity 187 Supplier A 37 Happy Ninja 1 195 Supplier C 70 Flying Ninja 1 187 Supplier A 53 Happy Ninja 1 187 Supplier A 31 Ninja Silhouette 1 193 Supplier B 56 Ninja Silhouette 1 193 Supplier B 50 Patient Ninja 1 187 Supplier A 19 Premium Quality 1
What I need is an array like this
Array
(
[187] => Array
[0]
(
[product_id] => 37
[product_name] => Happy Ninja
[quantity] => 1
)
[1]
(
[product_id] => 53
[product_name] => Flying Ninja
[quantity] => 1
)
[2]
(
[product_id] => 31
[product_name] => Ninja Silhouette
[quantity] => 1
)
[3]
(
[product_id] => 19
[product_name] => Premium Quality
[quantity] => 1
)
[195] => Array
(
[product_id] => 70
[product_name] => Flying Ninja
[quantity] => 1
)
[193] => Array
[0]
(
[product_id] => 56
[product_name] => Ninja Silhouette
[quantity] => 1
)
[1]
(
[product_id] => 50
[product_name] => Patient Ninja
[quantity] => 1
)
)
global $woocommerce;
$order_id = 123;
$suppliers = array();
/**
* Get the order
*/
$order = new WC_order($order_id);
$items = $order->get_items();
?>
<table >
<thead>
<th>
<td>Supplier id</td>
<td>Supplier name</td>
<td>Product id</td>
<td>Product name</td>
<td>Quantity</td>
</th>
</thead>
<tbody>
<?php
foreach($items as $item):
$supplier_id = get_post_meta($item['item_meta']['_product_id'][0], 'jm_ots-product-supplier', true);
$product_id = $item['item_meta']['_product_id'][0];
$product_name = $item['name'];
$quantity = $item['item_meta']['_qty'][0];
$suppliers[$supplier_id] = array('product_id' => $product_id, 'product_name' => $product_name, 'quantity' => $quantity);
echo '<tr>';
echo '<td >' . $supplier_id . '<td>';
echo '<td>';
if(!empty($supplier_id)): echo get_the_title($supplier_id); endif;
echo '</td>';
echo '<td>' . $product_id . '<td>';
echo '<td>' . $product_name . '<td>';
echo '<td>' . $quantity . '<td>';
echo '</tr>';
endforeach;
?>
</tbody>
</table>
<br />
<br />
<?php get_footer();?>
Can anyone point out a direction I need to go? Thanks.
Upvotes: 1
Views: 57
Reputation: 908
Try the following:
1- Populate $suppliers like this:
//This
$suppliers[] = array(
'supplier_id' => $supplier_id,
'product_id' => $product_id,
'product_name' => $product_name,
'quantity' => $quantity
);
//Instead of:
$suppliers[$supplier_id] = array(
'product_id' => $product_id,
'product_name' => $product_name,
'quantity' => $quantity
);
2- After populating $suppliers, sort it by supplier ID:
//Get array with $supplier_id values
foreach ($suppliers as $key => $row) {
$colSuppId[$key] = $row['supplier_id'];
}
//Sort $suppliers by $supplier_id
array_multisort($colSuppId, SORT_ASC, $suppliers);
3- Create your array from here
$newArray = array();
$bufArray = array();
$bufVal = null;
foreach ($suppliers as $row) {
$supp = $row['supplier_id'];
if (!empty($bufArray) and $bufVal != $supp) {
$newArray[$bufVal] = $bufArray;
$bufArray = array();
}
$bufArray[] = array(
'product_id' => $row['product_id'],
'product_name' => $row['product_name'],
'quantity' => $row['quantity']
);
$bufVal = $supp;
}
if (!empty($bufArray)) {
$newArray[$bufVal] = $bufArray;
}
$suppliers = $newArray;
For the next time you make a question, here is some advice:
Avoid posting code that is irrelevant for your question. It makes harder to understand your problem, and the question less useful for others with similar problems.
Make sure that indentations are formatted correctly. While you type the question, a preview is made onChange() ;-)
Add tags to the question that are related with the problem you have. That way, it will be easier to find your question for the people in this site that are able to help you. In this case, for example, at least you should add the PHP tag.
The better you explain your problem, the easier it is to understand it, the easier it is to help you, the more useful the question is for other users. Making a simplified example of your problem is a good way to go.
Upvotes: 1