WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce Menu Cart
[resolved] Use in admin bar? (11 posts)

  1. thisisbbc
    Member
    Posted 7 months ago #

    Is it possible to use the menu in the admin bar that always stays on top?

    Regards,
    Bastien

    http://wordpress.org/plugins/woocommerce-menu-bar-cart/

  2. Jeremiah
    Member
    Plugin Author

    Posted 7 months ago #

    That is not a feature, but you can customize it and wrap the output in an if admin statement

  3. thisisbbc
    Member
    Posted 7 months ago #

    Thank you for your answer Jeremiah. Would you mind guiding me in the process? Im kind of a novice in plugin coding and I wouldnt know where to start :/

    All best,
    Bastien

  4. Ewout
    Member
    Plugin Author

    Posted 7 months ago #

    Not out of the box, but you could try adding the following code to your functions.php file:

    add_action('admin_bar_menu', 'add_toolbar_wcmenucart', 100);
    function add_toolbar_wcmenucart($admin_bar){
    	if (!class_exists( 'WcMenuCart' ))
    		return;
    
    	$wcMenuCart = new WcMenuCart();
    	$menucart_html = $wcMenuCart->wcmenucart_menu_item();
    	if (!is_admin() && $menucart_html != '') {
    		$dom = new DOMDocument();
    		$dom->loadHTML($menucart_html);
    
    		foreach ($dom->getElementsByTagName('a') as $a) {
    		    $menutitle = $a->nodeValue;
    			$menuhref = $a->getAttribute('href');
    		}
    
    		$admin_bar->add_menu( array(
    			'id'    => 'wcmenucart',
    			'title' => '<i class="wcmenucart-icon-shopping-cart-0"></i>'.$menutitle,
    			'href'  => $menuhref,
    			'meta'  => array(
    				'title' => __('View your shopping cart', 'wcmenucart'),
    			),
    		));
    	}
    }

    Works on my test setup. Note that this doesn't work with AJAX, so whenever you add something to the cart it doesn't appear until you navigate to the next page. I'm assuming that you have some sort of plugin to enable the admin bar for everybody browsing the site?

  5. Jeremiah
    Member
    Plugin Author

    Posted 7 months ago #

    Thanks Ewout!

  6. thisisbbc
    Member
    Posted 7 months ago #

    Hi Ewout!

    Thank you very much for your answer. Well we use buddypress so the admin bar stays on top all the time, the notifications, messages and friend requests goes there. About your solution... It is indeed working... on the front end! But there's a problem with the back-end. The admin-bar is gone and most of the tabs display an empty white page (I can't even go to the theme editor anymore).

    Also, the menu doesn't seem to display a drop-down of the items in the cart on the front-end.

    A quick fix would be tremendously appreciated :)

    Thank you again.

    All best,
    Bastien

  7. Ewout
    Member
    Plugin Author

    Posted 7 months ago #

    Hi Bastien,
    First of all, the fact that it doesn't work on the back-end is on purpose, because it's a bit unstable, depending on all sorts of factors. Front-end only is more of a fool proof method. I think your error actually demonstrates that, because it's most likely this sort of bug that causes the behavior you're seeing.

    The following code detects the backend one step earlier, and doesn't do anything at all in the backend. Can you try if this works for you?

    add_action('admin_bar_menu', 'add_toolbar_wcmenucart', 100);
    function add_toolbar_wcmenucart($admin_bar){
    	if (!class_exists( 'WcMenuCart' ) || is_admin() )
    		return;
    
    	$wcMenuCart = new WcMenuCart();
    	$menucart_html = $wcMenuCart->wcmenucart_menu_item();
    	if ( $menucart_html != '') {
    		$dom = new DOMDocument();
    		$dom->loadHTML($menucart_html);
    
    		foreach ($dom->getElementsByTagName('a') as $a) {
    		    $menutitle = $a->nodeValue;
    			$menuhref = $a->getAttribute('href');
    		}
    
    		$admin_bar->add_menu( array(
    			'id'    => 'wcmenucart',
    			'title' => '<i class="wcmenucart-icon-shopping-cart-0"></i>'.$menutitle,
    			'href'  => $menuhref,
    			'meta'  => array(
    				'title' => __('View your shopping cart', 'wcmenucart'),
    			),
    		));
    	}
    }

    The fly-out is a pro feature, so that won't work. Even if you have the pro version it would take quite a bit of customizing to make it work, because the admin bar works very different from regular menus.

  8. thisisbbc
    Member
    Posted 7 months ago #

    Hey Ewout!

    Thanks for your quick answer that's really appreciated :)
    Working like a charm!

    I effectively seen this feature was reserved to the pro version between the time I posted my answer and I've read yours, so no problems with this.

    However since you mention it wouldn't work in the admin bar, it makes me a bit reluctant to buy the pro version. If you'd be able to add admin bar support you can add me to the satisfied customers list ;)

    Meanwhile I'll stick with the free version since it's pretty much what I needed.

    Regards,
    Bastien

  9. hyperV
    Member
    Posted 5 months ago #

    Hi all,

    nice stuff!!
    I#m looking the opposit wa to schow the my-account menue from the adminbar in the etop-menu beside the chat menu.

    Have someone a solution?

    Would be great!!!

    Regards

    Josef

  10. hyperV
    Member
    Posted 5 months ago #

    In addition:

    I try to integrate somthing like thi:

    add_filter( 'wp_nav_menu_items', 'add_myaccount_item', 10, 2 );
    function add_myaccount_item( $wp_admin_bar ) {
    	$user_id      = get_current_user_id();
    	$current_user = wp_get_current_user();
    	$profile_url  = get_edit_profile_url( $user_id );
    
    	if ( ! $user_id )
    		return;
    
    	$avatar = get_avatar( $user_id, 26 );
    	$howdy  = sprintf( __('Howdy, %1$s'), $current_user->display_name );
    	$class  = empty( $avatar ) ? '' : 'with-avatar';
    
    	$wp_admin_bar->add_menu( array(
    		'id'        => 'my-account',
    		'parent'    => 'top-secondary',
    		'title'     => $howdy . $avatar,
    		'href'      => $profile_url,
    		'meta'      => array(
    			'class'     => $class,
    			'title'     => __('My Account'),
    		),
    	) );
    }

    How can I show this in the Top-Menu of my mystile wootemplate.

    Thanks in advanced

    Josef

  11. Ewout
    Member
    Plugin Author

    Posted 5 months ago #

    Hi Josef,
    This has nothing to do with the Menu Cart plugin, so I'm afraid I can't help you.

    The code below should get you on the way though: it changes the default 'Howdy' link to the my-account page. The main thing that I changed was the action hook (admin_bar_menu) and the my_account_url

    add_action('admin_bar_menu', 'admin_bar_my_account', 100);
    function admin_bar_my_account( $admin_bar ){
    	$user_id		= get_current_user_id();
    	$current_user	= wp_get_current_user();
    	$my_account_url	= get_permalink( woocommerce_get_page_id( 'myaccount' ));
    
    	if ( empty($user_id) ) {
    		return;
    	}
    
    	$avatar	= get_avatar( $user_id, 26 );
    	$howdy	= sprintf( __('Howdy, %1$s'), $current_user->display_name );
    	$class	= empty( $avatar ) ? '' : 'with-avatar';
    
    	$admin_bar->add_menu( array(
    		'id'		=> 'my-account',
    		'parent'	=> 'top-secondary',
    		'title'		=> $howdy . $avatar,
    		'href'		=> $my_account_url,
    		'meta'		=> array(
    			'class'	=> $class,
    			'title'	=> __('My Account'),
    		),
    	) );
    }

Reply

You must log in to post.

About this Plugin

About this Topic