WooCommerce TM Extra Product Options #3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
// add all options to SINGLE product row class WOE_add_TM_cols{ var $tm_cols = array( 'pos','name','value','price','quantity', 'name_value'); function __construct() { add_filter('woe_get_order_product_fields',array($this,'add_product_fields') ); add_filter('woe_fetch_order_products', array($this,'fill_tm_cols') ,10, 5); } function add_product_fields($fields) { foreach($this->tm_cols as $tm_col) { $fields['tm_field_'.$tm_col] = array('label'=>'TM '.$tm_col,'colname'=>'TM '.$tm_col,'checked'=>1); } return $fields; } function fill_tm_cols($products, $order, $labels, $format,$static_vals) { $new_products = array(); foreach( $products as $item_id=>$product) { $item_meta = get_metadata( 'order_item', $item_id ); $pos = 1; if( isset($item_meta["_tmcartepo_data"]) AND is_array($tmfields = maybe_unserialize($item_meta["_tmcartepo_data"][0])) ) { $new_product = $product; foreach($tmfields as $tm_field) { $tm_field['pos']= $pos++;//set fake field $tm_field['name_value'] = $tm_field['name'] . ':' .$tm_field['value']; // fill TM columns foreach($this->tm_cols as $tm_col) { if( !isset($new_product['tm_field_'.$tm_col])) continue; if( !empty($new_product['tm_field_'.$tm_col])) $new_product['tm_field_'.$tm_col] .= ", " .$tm_field[$tm_col]; else $new_product['tm_field_'.$tm_col] = $tm_field[$tm_col]; } } //don't add each option as new product! $new_products[] = $new_product; $pos++; } if( isset($item_meta["_tmcartfee_data"]) AND is_array($tmfields = maybe_unserialize($item_meta["_tmcartfee_data"][0])) ) { $new_product = $product; foreach($tmfields[0] as $tm_field) { $tm_field['pos']= $pos++;//set fake field $tm_field['name_value'] = $tm_field['name'] . ':' .$tm_field['value']; // fill TM columns foreach($this->tm_cols as $tm_col) { if( !isset($new_product['tm_field_'.$tm_col])) continue; if( !empty($new_product['tm_field_'.$tm_col])) $new_product['tm_field_'.$tm_col] .= ", " .$tm_field[$tm_col]; else $new_product['tm_field_'.$tm_col] = $tm_field[$tm_col]; } } //don't add each option as new product! $new_products[] = $new_product; $pos++; } if($pos==1) //nothing added - just copy product as is $new_products[] = $product; } return $new_products; } } new WOE_add_TM_cols(); |