// Aggiungi filtro per la gestione delle colonne
add_filter( 'manage_edit-shop_order_columns', 'dmm_add_order_count_column', 20 );
function dmm_add_order_count_column( $columns ) {
// Aggiungi la colonna 'Totale ordini cliente'
$columns['email_order_count'] = __( 'Totale ordini cliente', 'woocommerce' );
return $columns;
}
// Popola la colonna con il conteggio degli ordini
add_action( 'manage_shop_order_posts_custom_column', 'dmm_populate_new_column' );
function dmm_populate_new_column( $column ) {
global $post;
if ( 'email_order_count' === $column ) {
$billing_email = get_post_meta( $post->ID, '_billing_email', true );
if ( $billing_email ) {
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_billing_email',
'meta_value' => $billing_email,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
) );
$order_count = count( $customer_orders );
// Se è il primo ordine del cliente, visualizza "Primo ordine!"
if ( $order_count === 1 ) {
echo 'Primo ordine!';
} else {
echo $order_count;
}
}
}
}
// Rendi la colonna ordinabile
add_filter( 'manage_edit-shop_order_sortable_columns', 'dmm_make_order_count_column_sortable' );
function dmm_make_order_count_column_sortable( $sortable_columns ) {
$sortable_columns['email_order_count'] = 'email_order_count';
return $sortable_columns;
}
// Ordina in base al conteggio degli ordini del cliente
add_action( 'pre_get_posts', 'dmm_custom_order_count_column_sort' );
function dmm_custom_order_count_column_sort( $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return;
}
$orderby = $query->get( 'orderby' );
if ( 'email_order_count' === $orderby ) {
$query->set( 'meta_key', '_billing_email' );
$query->set( 'orderby', 'meta_value' );
}
}
Se hai attivato l’opzione di WooCommerce “Archiviazione degli ordini ad alte prestazioni” dovrai utilizzare lo snippet qui sotto.
/**
* Visualizza totale ordini cliente HPOS
*
* Visualizza il numero totale di ordini effettuati dal cliente con l'opzione HPOS attiva.
* © Matteo De Maria – v. 1.0.0
* Testato su WordPress 6.4.3 | WooCommerce 8.6.2
*/
// Aggiungi filtro per la gestione delle colonne
add_filter( 'manage_woocommerce_page_wc-orders_columns', 'dmm_count_order' );
function dmm_count_order( $columns ){
$columns[ 'dmm_count_order' ] = 'Totale ordini cliente';
return $columns;
}
// Popola la colonna con il conteggio degli ordini
add_action( 'manage_woocommerce_page_wc-orders_custom_column', 'dmm_populate_orders_column', 25, 2 );
function dmm_populate_orders_column( $column_name, $order ){
if ( 'dmm_count_order' === $column_name ) {
$billing_email = $order->get_billing_email();
if ( $billing_email ) {
$customer_orders = wc_get_orders( array(
'numberposts' => -1,
'billing_email' => $billing_email,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
) );
$order_count = count( $customer_orders );
if ( $order_count === 1 ) {
echo 'Primo ordine!';
} else {
echo $order_count . '';
}
}
}
}
Copia il codice nel file functions.php del tuo tema child attivo o utilizza il plugin Code Snippets