lundi 22 mars 2021

How to access specific PHP array object

The following code is passing TM EPO custom product fields (woocommerce addon) to order export plugin, so I can export each field in a separate column in CSV table:

var $tm_fields = array( 
        'Day Time Field'=>['6012beca7979e4.51764573', '6012c0481b1b30.37462970', '6012c0581b1b46.84309510', '6012c05a1b1b51.41167065', '6012c05c1b1b65.99820650', '604a3a296c3179.70414672', '604a3ac46c3183.55870947', '604a3ac66c3193.93047952', '604a3ac96c31a4.57982566', '604a3acb6c31b2.86456982'],
        'Side Field'=>['6012b57d151e28.97324618', 'pa_side-dish'],
        'Bundle Field'=>['pa_second-dish', 'pa_main-dish'],
    );
    function __construct() {
        add_filter('woe_get_order_product_fields',array($this,'add_product_fields') );
        add_filter('woe_get_order_product_item_meta', array($this,'fill_tm_fields') );
    }
    function add_product_fields($fields) {
        foreach($this->tm_fields as $name=>$element_ids) {
            $fields['tm_field_'.$name] = array('label'=>$name,'colname'=>$name,'checked'=>1);
        }   
        return $fields;
    }
    function format_line($tm_field) {
          
        $a = $tm_field['name'];
        $b = $tm_field['value'];
        
        if ( ($a != 'Side Dish') && ($a != 'pa_side-dish') && ($a != 'pa_main-dish') && ($a != 'pa_second-dish') ) {
            $tm_field = $a . ' - ' . $b; // now $c contains $a and $b           
        } else {
            $tm_field = $b;
        }
    
        return $tm_field;  // can use $tm_field['name'], $tm_field['price'], $tm_field['quantity'], $tm_field['value']     
    }

    function fill_tm_fields($item_meta) {
        // gather TM values to array
        $product_fields = array();
        $product_fields[] = array();
        
        
        // cart Items
        if( isset($item_meta["_tmcartepo_data"])  AND  is_array($tmfields = maybe_unserialize($item_meta["_tmcartepo_data"][0]))  ) {
            foreach($tmfields as $tm_field) {
                $element_id = $tm_field['section'];
                if( !isset($product_fields[$element_id]) ) 
                    $product_fields[$element_id] = array();
                    $product_fields[$element_id][] = $this->format_line($tm_field); 
            }
        }
        
        // make list 
        foreach($product_fields as $element_id=>$values)
            $product_fields[ $element_id ] = join("\n", $values);
        
        // add to item meta 
        foreach($this->tm_fields as $name=>$element_ids) {
            // gather all keys
            $vals = array( );
            foreach($element_ids as $element_id) {
                if( isset($product_fields[$element_id])) 
                    $vals[] = trim($product_fields[$element_id]);
            }
            // it must be array !
            $item_meta['tm_field_'.$name] = array( join("\n", $vals) );
        }   
        return $item_meta;
    }

How I can access $tm_fields array field somewhere (Day Time Field, Side Field, Bundle Field) so I can create an if statement to tell which field meta I want to pass, depending on label for example:

if ($tm_fields = "Day Time Field"){
return $tm_field[name];
} else if ($tm_fields = "Side Field"){
return $tm_field[value];
}

I want to have separate columns for $tm_field[name] and $tm_field[value] for Day Time Field, in function format_line($tm_field) - if satetment is fast fix and it prints both in one column

How output looks now: Order export sample

Aucun commentaire:

Enregistrer un commentaire