Custom cart notice based on user total purchased amount in Woocommerce
Try the following instead (I have revisited a bit get_customer_total_purchases_sum()
function):
// Utililty function to get customer's total purchases sumfunction get_customer_total_purchases_sum() { $current_user_id = get_current_user_id(); // Current user ID if( $current_user_id == 0 ) return 0; // we return zero if customer is not logged in global $wpdb; // return the SQL query (paid orders sum) return $wpdb->get_var("SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}postmeta as pm INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm.post_id = pm2.post_id INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID WHERE p.post_status LIKE 'wc-completed' AND p.post_type LIKE 'shop_order' AND pm.meta_key LIKE '_order_total' AND pm2.meta_key LIKE '_customer_user' AND pm2.meta_value LIKE '$current_user_id'");}// Display a custom noticeadd_action( 'template_redirect', 'total_purchase_custom_notification' );function total_purchase_custom_notification( ) { if ( is_wc_endpoint_url('order-received') && WC()->session->get( 'purchases_sum' ) ) { // We remove this session variable in thankyou page (if it still exist) WC()->session->__unset( 'purchases_sum' ); } // On cart page we display a custom notice elseif( is_cart() ) { // Get customer's purchases total sum and set it in WC_Session if( ! WC()->session->get( 'purchases_sum' ) ){ WC()->session->set('purchases_sum', get_customer_total_purchases_sum()); } $total_purchases = WC()->session->get( 'purchases_sum' ); if ( $total_purchases == 0 ) return; // We exit (no purchases or non logged users) if ( ( 10000 - $total_purchases ) > 0 ) { wc_add_notice( sprintf( '<a class="button alt wc-forward" style="float:right" href="%s">%s</a> ' . __( "You need an extra %s at all to get a...", "woocommerce" ), get_permalink( wc_get_page_id( 'shop' ) ), __( "Continue shopping", "woocommerce" ), strip_tags( wc_price( 10000 - $total_purchases ) ) ), 'notice'); } else { wc_add_notice( __( "......", "woocommerce"), 'notice' ); } }}
This code goes on function.php file of your active child theme (or theme). Tested and works.