0

How can I add a Notification bubble for Pending/On Hold/Processing order on Admin Top Bar?

Like This

2 Answers 2

1

I will get you some idea on how to do this as you didn't mention from where count data will come. Add following code to your theme/child-theme functions.php file

function custom_admin_bar_menu() {
    $pending_count = wc_orders_count('pending');
    $on_hold_count = wc_orders_count('on-hold');
    $processing_count = wc_orders_count('processing');

    if ( current_user_can( 'manage_options' ) ) {
        global $wp_admin_bar;

        //Add an icon with count
        $wp_admin_bar->add_menu( array(
            'id'    => 'custom-1', //Change this to yours
            'title' => '<span class="ab-icon dashicons dashicons-cart"></span><span class="ab-label">'.$pending_count.'</span>',
            'href'  => get_admin_url( NULL, 'edit.php?post_status=wc-pending&post_type=shop_order' ),//Replace with your resired destination
        ) );

        //Add another icon with count
        $wp_admin_bar->add_menu( array(
            'id'    => 'custom-2', //Change this to yours
            'title' => '<span class="ab-icon dashicons dashicons-chart-line"></span><span class="ab-label">'.$on_hold_count.'</span>',
            'href'  => get_admin_url( NULL, 'edit.php?post_status=wc-on-hold&post_type=shop_order' ),//Replace with your resired destination
        ) );

        //Add another icon with count
        $wp_admin_bar->add_menu( array(
            'id'    => 'custom-3', //Change this to yours
            'title' => '<span class="ab-icon dashicons dashicons-carrot"></span><span class="ab-label">'.$processing_count.'</span>',
            'href'  => get_admin_url( NULL, 'edit.php?post_status=wc-processing&post_type=shop_order' ),//Replace with your resired destination
        ) );

        //You can add more as per your need.
    }
}
add_action( 'admin_bar_menu', 'custom_admin_bar_menu' , 500 );

The 0 inside <span class="ab-label">0</span> represents the count. You can easily replace that by using a variable and some custom query.

3
  • Can please make the full code with the following query? $args = array( 'status' => 'on-hold', ); $orders = wc_get_orders( $args );
    – ashik4u
    Commented Jun 28, 2020 at 16:35
  • 1
    I've updated the code and added pending,on-hold,processing count along with respective links. Commented Jun 29, 2020 at 14:44
  • Awesome. Working great. Thanks man
    – ashik4u
    Commented Jun 29, 2020 at 16:03
0

For someone looking for a working example that they can adapt based on the counts they need OR a snippet that works to SHOW UNREAD ENTRIES FROM GRAVITY FORMS ON CUSTOM MENU ITEMS... check this out.

This snippet gets the count for ALL the form IDs in the array() within the count_entries() call so that the parent menu item shows the count for ALL unread entries

Then as I build the menu, I change the ID in both the count_entries() call AND the URL - see comments in the snippet. This allows the SUBMENU items to each show the count for their specific unread entries

function register_my_custom_menu_page() {
    $search_criteria = array(
        'status'     => 'active', //Active forms 
        'field_filters' => array( //which fields to search
            array(
                'key' => 'is_read', 'value' => false, // let's just get the count for entries that we haven't read yet.
            )
          )
        );

    // Add the form IDs to the array below, the parent menu will show ALL unread entries for these forms
    $notification_count = GFAPI::count_entries( array(1,4,5,6,11,13), $search_criteria );
    
    add_menu_page(
        'Full Quote Form submissions', // Page Title
        // here we're going to use the var $notification_count to get ALL the form IDS and their counts... just for the parent menu item
        $notification_count ? sprintf( 'Quotes <span class="awaiting-mod">%d</span>', $notification_count ) : 'View Quotes',
        'manage_options', // Capabilities 
        'admin.php?page=gf_entries&id=13', // menu slug
        '', // callback function
        'dashicons-format-aside', // icon URL
        6 // position
    );
    add_submenu_page( 
        'admin.php?page=gf_entries&id=13', // this needs to match menu slug of the parent
        'View Full Quotes', // name of the page
        // The ID on the next line, 13 in this example, matches the form ID in the URL on the LAST line. Notice we're NOT using the var $notification_count from above, as that contains the entry count for ALL the forms
        GFAPI::count_entries( 13, $search_criteria ) ? sprintf( 'Full Quotes <span class="awaiting-mod">%d</span>', GFAPI::count_entries( 13, $search_criteria ) ) : 'Full Quotes',
        'manage_options', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        'admin.php?page=gf_entries&id=13'
    );
    add_submenu_page( 
        'admin.php?page=gf_entries&id=13', // this needs to match menu slug of the parent
        'View Short Quotes', // name of the page
        // The ID on the next line, 1 in this example, matches the form ID in the URL on the LAST line.     
        GFAPI::count_entries( 1, $search_criteria ) ? sprintf( 'Short Quotes <span class="awaiting-mod">%d</span>', GFAPI::count_entries( 1, $search_criteria ) ) : 'Short Quotes',
        'manage_options', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        'admin.php?page=gf_entries&id=1'
    );
    add_submenu_page( 
        'admin.php?page=gf_entries&id=13', // this needs to match menu slug of the parent
        'Sell Your Equipment', // name of the page
        // The ID on the next line, 5 in this example, matches the form ID in the URL on the LAST line.             
        GFAPI::count_entries( 5, $search_criteria ) ? sprintf( 'Selling Equip <span class="awaiting-mod">%d</span>', GFAPI::count_entries( 5, $search_criteria ) ) : 'Selling Equip',
        'manage_options', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        'admin.php?page=gf_entries&id=5'
    );
    add_submenu_page( 
        'admin.php?page=gf_entries&id=13', // this needs to match menu slug of the parent
        'Equipment Wanted', // name of the page
        // The ID on the next line, 6 in this example, matches the form ID in the URL on the LAST line.             
        GFAPI::count_entries( 6, $search_criteria ) ? sprintf( 'Equip Wanted <span class="awaiting-mod">%d</span>', GFAPI::count_entries( 6, $search_criteria ) ) : 'Equip Wanted',
        'manage_options', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call  
        'admin.php?page=gf_entries&id=6'
    );
    add_submenu_page( 
        'admin.php?page=gf_entries&id=13', // this needs to match menu slug of the parent
        'Appraisal Requests', // name of the page
        // The ID on the next line, 11 in this example, matches the form ID in the URL on the LAST line.                
        GFAPI::count_entries( 11, $search_criteria ) ? sprintf( 'Appraisal Requests <span class="awaiting-mod">%d</span>', GFAPI::count_entries( 11, $search_criteria ) ) : 'Appraisal Requests',
        'manage_options', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        'admin.php?page=gf_entries&id=11'
    );
    add_submenu_page( 
        'admin.php?page=gf_entries&id=13', // this needs to match menu slug of the parent
        'Contact Form',  // name of the page
        // The ID on the next line, 4 in this example, matches the form ID in the URL on the LAST line.             
        GFAPI::count_entries( 4, $search_criteria ) ? sprintf( 'Contact Form <span class="awaiting-mod">%d</span>', GFAPI::count_entries( 4, $search_criteria ) ) : 'Contact Form',
        'manage_options', 
        // make sure to change the ID on the end of this URL to the form you want to point to, AND make sure it matches the ID two lines above in the count_entries() call
        'admin.php?page=gf_entries&id=4'
    );
    
}
add_action( 'admin_menu', 'register_my_custom_menu_page' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.