I use Woocommerce latest version 3.4.2. In this case, we collect the order data: the product and its additives ( I take in meta data).
How to assign the index of variable $skus[] = $product->get_sku();
as the value of variable $product_mod[] = '';
?
$product_mod[1] = "0";
// The product ( ingredient Sugar) with key 1 is the product modifier with key 0.
// Get product details
$skus = $item_quantities = $line_item_totals = $product_mod = array();
// Loop though order items
foreach( $order->get_items() as $item_id => $item){
$product_id = $item->get_product_id();
$product = $item->get_product();
$item_quantities[] = $item->get_quantity();
$line_item_totals[] = $item->get_total();
$skus[] = $product->get_sku();
$product_mod[] = NULL;
$ai = $item->get_meta('Optionally select');
if( strpos( $ai, 'Cinnamon' ) !== false ) {
$skus[] = '10001';
$item_quantities[] ='1';
$line_item_totals[] = '50';
$product_mod[] = '';
}
if( strpos( $ai, 'Sugar' ) !== false ) {
$skus[] = '10002';
$item_quantities[] ='1';
$line_item_totals[] = '50';
$product_mod[] = '';
}
if( strpos( $ai, 'Mint' ) !== false ) {
$skus[] = '10003';
$item_quantities[] ='1';
$line_item_totals[] = '50';
$product_mod[] = '';
}
}
// Product details
foreach ($skus as $key => $value){
$data .= "&product_sku[".$key."]=".$value."";
$data .= "&product_quantity[".$key."]=".$item_quantities[$key]."";
$data .= "&product_price[".$key."]=".$line_item_totals[$key]."";
if( isset($product_mod[$key]) ) {
$data .= "&product_mod[".$key."]=".$key."";
}
}
print_r( $data );
now show:
// For the convenience of reading, I wrote in a column, but this is a string.
&product_sku[0]=10030
&product_quantity[0]=1
&product_price[0]=499
&product_sku[1]=10002
&product_quantity[1]=1
&product_price[1]=50
&product_mod[1]=1
Need:
&product_sku[0]=10030 // Coffe sku
&product_quantity[0]=1 // Coffe quantity
&product_price[0]=499 // Coffe price
&product_sku[1]=10002 // Sugar sku
&product_quantity[1]=1 // Sugar quantity
&product_price[1]=50 // Sugar price
&product_mod[1]=0 // Ingredient Sugar with number 1, is a product modifier with number 0.
You have been complicating a bit the thing… You need to set the main order item index in a variable to get it for your product modifier in the selected additional options. No need of any complications…
I have revisited, simplified and compacted your code:
// Array of defined options ( sku => option name )
$options = array(
'10001' => 'Cinnamon',
'10002' => 'Sugar',
'10003' => 'Mint',
);
$count = 0;
// Loop though order items
foreach( $order->get_items() as $item_id => $item){
$product_id = $item->get_product_id();
$product = $item->get_product();
$data .= '&product_sku['.$count.']='.$product->get_sku();
$data .= '&product_quantity['.$count.']='.$item->get_quantity();
$data .= '&product_price['.$count.']='.$item->get_total();
$ind = $count; // Here we set the main order item index in a variable
$count++;
// Get order item selected options
$options_selected = $item->get_meta('Optionally select');
// Loop though order items selected options
foreach( $options as $sku_key => $label_value ){
if( strpos( $options_selected, $label_value ) !== false ) {
$data .= '&product_sku['.$count.']='.$sku_key;
$data .= '&product_quantity['.$count.']=1';
$data .= '&product_price['.$count.']=50';
$data .= '&product_mod['.$count.']='.$ind;
$count++;
}
}
}
// Testing output
print_r( $data );
Untested, but it should work as expected.