Вы можете попробовать следующий код:
// the array to store the data
$cart = array();
foreach($items as $item => $values)
{
$_product = wc_get_product( $values['data']->get_id());
if($_product->post_type == 'product_variation')
{
echo $_product->parent_id; echo '//'; echo $values['variation']['taille'];
echo '//'; echo $values['quantity'];
// create the new temporary array to save the data structure
$tmp = array( $_product->parent_id, array( $values['variation']['taille'], $values['quantity'] ) );
// add the tmp array to the storage array
array_push($cart, $tmp );
}
}
Если вы распечатаете массив "cart", он будет выглядеть так:
Array
(
[0] => Array
(
[0] => 86253
[1] => Array
(
[0] => 35
[1] => 1
)
)
[1] => Array
(
[0] => 86253
[1] => Array
(
[0] => 36
[1] => 1
)
)
[2] => Array
(
[0] => 86253
[1] => Array
(
[0] => 38
[1] => 2
)
)
)
EDIT:
Это не совсем то, что вам нужно, но также следует сгруппировать данные массива по идентификатору продукта:
// the array to store the data
$cart = array();
foreach($items as $item => $values)
{
$_product = wc_get_product( $values['data']->get_id());
if($_product->post_type == 'product_variation')
{
echo $_product->parent_id; echo '//'; echo $values['variation']['taille'];
echo '//'; echo $values['quantity'];
// Check if the parent_id is already set as array key
if( !array_key_exists ( $_product->parent_id, $cart ) )
{
// use the parent_id as key
$cart[$_product->parent_id] = array();
}
// create the new temporary array
$tmp = array( $values['variation']['taille'], $values['quantity'] );
// add the tmp array to the $cart
array_push($cart[$_product->parent_id], $tmp );
}
}
Если вы распечатаете его, оно должно выглядеть так:
Array
(
[86253] => Array
(
[0] => Array
(
[0] => 35
[1] => 1
)
[1] => Array
(
[0] => 36
[1] => 1
)
)
[86245] => Array
(
[0] => Array
(
[0] => 36
[1] => 7
)
[1] => Array
(
[0] => 39
[1] => 4
)
)
)