I'm trying to exclude/ignore a specific item from a 'count' in an array which generates a CSV file.
I generate a daily CSV file of orders from Woocommerce; Each Order is made up of a quantity of varying single products; I define the CSV Headers manually -eg:
$csv_header = array('Order ID','Company Name','Contact Name','Address 1','Address 2','Suburb','Product 1', 'Product 2', 'Product 3','#Items')
I pull the metadata based on Order IDs:
$result = $wpdb->get_results('SELECT * FROM `'.$wpdb->prefix.'postmeta` WHERE post_id = '. (int)$order_id->ID);
I create an instance of each order and get the items:
$order = new WC_Order((int)$order_id->ID);
$items = $order->get_items();
After getting the details and setting the column for those - eg:
if( $res->meta_key == '_shipping_company'){
$order_line[2] = $res->meta_value;
I then loop to create the invidual item product count and the #items total
foreach ($items as $item ) {
$_count = array_search( $item['name'], $csv_header );
$order_line[$_count] = $item['item_meta']['_qty'][0];
$row_items += $order_line[$_count];
}
$order_line[10] = $row_items;
and add blanks to products not in the order:
for ($i = 0; $i < 37; $i++) {
if( !isset($order_line[$i]) && empty($order_line[$i]) ) {
$order_line[$i] = '';
}
This all works well, but now I need to EXCLUDE a product NOT defined in the $csv_header from the count - bearing in mind that the Order will contain defined products. eg: count products 1-3 but not Product 4.
At the moment if a Product is not defined in $csv-header, it will put a number in my first column in place of the order-id instead of ignoring it.
Any suggestions appreciated.