• Resolved flavcao

    (@flavcao)


    Hi there,

    I’m wanting to add columns to the CSV exported file with the names of the customers.

    On the website I’m working a customer can buy X amount of tickets, and whenever the user buys more than 1 ticket it generates a loop to add the names to each person the tickets are for.
    So for example: John buys 2 tickets, and on the checkout he can put down that the tickets are for John (himself) and for Mary. The names are saved to the database.

    However, I can’t picture exactly how to add these “extra names” to the CSV file, as these would be in a loop and they can be any number of extra names (user can buy whatever number of tickets they want/need).

    I can’t add one(1) key for each extra name to the “Order Export Settings”, because I’m never going to know what’s the maximum number of tickets any of the users bought. So the way I see this would have to be only one checkbox, say “Extra Names”, and if that’s checked than the csv_write would bring (loop) all the extra names for each order (Extra Name 1, Extra Name 2, etc etc).

    Has anyone tried to add columns using a for loop? Is it doable? Could anyone enlighten me on this idea?

    Thanks anyone for any help, all very appreciated.

    https://www.ads-software.com/plugins/woocommerce-simply-order-export/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi flavcao,

    Let’s assume a situation where tickets are stored in _postmeta table as an array (Of course, it can be stored in some other table too) with meta_key name “_my_tickets”

    Array would something be like.

    Array(
    0 => 'John L.',
    1 => 'Kathy M.',
    2 => 'Christian N.'
    );

    Now, you want to export one more field “tickets” which contain value as

    John L. | Kathy M. | Christian N.

    To accomplish this task, you can use following approach.

    1. Install WooCommerce Simply Order Export Add-on plugin. You can get your copy of this plugin at: https://sharethingz.com/downloads/woocommerce-simply-order-export-add-on/

    2. Go to “Woocommerce > Settings > Order Export > Advanced Settings > Custom Fields” and add following field.

    Field key = _my_tickets
    Field Name = Tickets
    Not in meta field would be “checked”

    For more information, please refer: https://github.com/ankitrox/WooCommerce-Simply-Order-Export-Add-on-Doc/blob/master/custom-fields.md

    3. Add following code to theme’s functions.php file.

    function wsoe_add_tickets_to_export( &$csv_values, $order_details, $key, $fields, $item_id, $current_item ) {
    
        switch ( $key ) {
    
            case '_my_tickets':
    
                $tickets = (array)get_post_meta( $order_details->id, '_my_tickets', true );
    	    $tickets = implode(" | ", $tickets);
                array_push( $csv_values, $tickets );
            break;
    
            default:
    	    array_push( $csv_values, '' );
            break;
    
        }
    }
    add_action('wsoe_addon_add_to_csv', 'wsoe_add_tickets_to_export', 10, 6 );

    That’s it!

    It should work for you.

    Note that based in which table data is getting stored, code in Step 3 would change. I can help you with this, for add-on related questions, you can use the premium forum at: https://sharethingz.com/support/forum/woocommerce-simply-order-export-addon/

    Hope this helps.

    Regards,
    Ankit

    Thread Starter flavcao

    (@flavcao)

    Hi Ankit,

    Thank you very much for your response.

    Unfortunately I think the solution with array won’t work on my case, as each new ‘extra name’ is being stored as a new column on the table (not one column with an array of names – multiple columns).

    My idea is that somehow I’d need to be able to test is the column has content and if yes print it to the .csv file, if not than keep moving through each until it’s completed. The problem I’m facing is never knowing how many ‘extra names’ (therefore extra columns).

    It sounds a lot like a while loop may be a solution (one solution), but then again, I’m not that experienced to put this in code.

    I’ll keep trying.

    Thanks again, appreciated.

    Thread Starter flavcao

    (@flavcao)

    Hi Ankit,

    Thank you for your help. With your tip I managed to get the extra fields in the table using the implode and array_push method to store the information as an array.

    The code works for the purposes I need now.

    I was just wondering if you could give an insight for the table columns headers.

    I can add one header “Extra Fields” using the function:

    function wpg_add_columns($cols) {
    
    	$cols['wc_settings_tab_extra_fields'] = __('Extra Fields', 'my_theme');
    	return $cols;
    
    }
    add_filter('wpg_order_columns', 'wpg_add_columns');

    This way I have only one header and all the extra fields are being printed to the csv file in separate columns.

    I’m trying to somehow loop the writing of the header, something like:

    function wpg_add_columns($cols) {
    	for($i = 1; $i <= 20; $i++){
    		$cols['wc_settings_tab_extra_names'] = __('Extra Name'.$i , 'face3_theme');
    	}
    		return $cols;
    }
    add_filter('wpg_order_columns', 'wpg_add_columns');

    Does it make sense?

    I tried something like the adding to the csv file:

    function wpg_add_columns($cols) {
    
    	$extra_columns = array($names, $emails);
    	for ($i=1; $i <= 20; $i++) {
    		$names = (array)__( 'Participant '.$i.' Name', true );
    		$names = implode(" , ", $names);
    		$emails = (array)__( 'Participant '.$i.' Email', true );
    		$emails = implode(" , ", $emails);
    		array_push( $extra_columns, $names, $emails );
    	}
    
    	$cols['wc_settings_tab_extra_fields'] = __($extra_columns, 'my_theme');
    	return $cols;
    
    }
    add_filter('wpg_order_columns', 'wpg_add_columns');

    But that prints “Array” to the csv file. Everything works normally.

    Could you give a little light on how to loop the writing of table headers for the same field?

    Thanks for any insight, if you can.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Loop for custom fields’ is closed to new replies.