Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/3b668d9503ede959c6a5b400ebce3625 to your computer and use it in GitHub Desktop.
Save xlplugins/3b668d9503ede959c6a5b400ebce3625 to your computer and use it in GitHub Desktop.
Add a new column for UTM Source in WooCommerce order listing page
// ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'wffn_custom_shop_order_column', 22 );
add_filter( 'woocommerce_shop_order_list_table_columns', 'wffn_custom_shop_order_column', 22 );
function wffn_custom_shop_order_column( $columns ) {
$reordered_columns = array();
// Inserting columns to a specific location
foreach ( $columns as $key => $column ) {
$reordered_columns[ $key ] = $column;
if ( $key == 'billing_address' ) {
$reordered_columns['wc_utm_source'] = __( 'UTM Source', 'theme_domain' );
$reordered_columns['wc_utm_campaign'] = __( 'UTM Campaign', 'theme_domain' );
}
}
return $reordered_columns;
}
// Adding custom fields meta data for each new column (example)
add_action( 'manage_shop_order_posts_custom_column', 'wffn_custom_orders_list_column_content', 22, 2 );
add_action( 'manage_woocommerce_page_wc-orders_custom_column', 'wffn_custom_orders_list_column_content', 22, 2 );
function wffn_custom_orders_list_column_content( $column, $order_or_id ) {
if ( $order_or_id instanceof WC_Order ) {
$post_id = $order_or_id->get_id();
} else {
$post_id = $order_or_id;
}
if ( empty( $post_id ) || 0 === absint( $post_id ) ) {
return '';
}
global $wpdb;
$query = $wpdb->prepare( "SELECT * from " . $wpdb->prefix . "bwf_conversion_tracking WHERE source = %d", $post_id );
$get_data = $wpdb->get_row( $query, ARRAY_A );//phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$data = '<small>(<em>no value</em>)</small>';
switch ( $column ) {
case 'wc_utm_source':
echo ( is_array( $get_data ) && ! empty( $get_data['utm_source'] ) ) ? $get_data['utm_source'] : $data;
break;
case 'wc_utm_campaign' :
echo ( is_array( $get_data ) && ! empty( $get_data['utm_campaign'] ) ) ? $get_data['utm_campaign'] : $data;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment