I am trying to display a custom notice in my custom account menu element based on user total purchased amount in Woocommerce, based on this answer code:
Custom cart notice based on user total purchased amount in Woocommerce
It does not work as I would like. What I am doing wrong?
This is the code that I use:
add_filter ( 'woocommerce_account_menu_items', 'xu', 40 );
function xu( $menu_links ){
$menu_links = array_slice( $menu_links, 0,3 , true )
+ array( 'xu' => 'Xu của bạn' )
+ array_slice( $menu_links, 3, NULL, true );
return $menu_links;
}
add_action( 'init', 'add_endpoint' );
function add_endpoint() {
add_rewrite_endpoint( 'xu', EP_PAGES );
}
add_action( 'woocommerce_account_xu_endpoint', 'xuxu' );
function xuxu() {
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 )
{
echo 'You need an extra ' . wc_price( 10000 - $total_purchases ) . ' at all to get a... ';
}
else
{
echo '... ';
}
}
Any help is appreciated.
To make the content appear for your custom menu item, you need to refresh rewrite rules.
For that go to WordPress Settings > Permalinks… And click on "Save changes". Now your content will appear.
Here is your revisited code (clean formatted) with some additions to remove the session value on thankyou page:
This code goes on function.php file of your active child theme (or theme). Tested and works.