WooCommerce admin bar快捷菜单

优质
小牛编辑
129浏览
2023-12-01

WooCommerce网站的插件一般较多,这样后台菜单会很长,想找到自己想用的功能会有困难。因此,本文介绍一种在wp admin bar增加WooCommerce admin bar快捷菜单的方法。

本文用到了自定义WordPress admin bar菜单中提到的方法。

WooCommerce admin bar快捷菜单

WooCommerce admin bar快捷菜单代码

代码放在子主题的functions.php中。

if( is_user_logged_in() && current_user_can('manage_options') ){

	add_action( 'wp_footer', 'sola_admin_bar_custom_styles' );
	add_action( 'admin_footer', 'sola_admin_bar_custom_styles' );

	function sola_admin_bar_custom_styles(){
		?>
<style>
	@media screen and (max-width: 782px){
		#wpadminbar li#wp-admin-bar-sola_quick_menu{
			display: block;
			position: static;
		}
		#wpadminbar li#wp-admin-bar-sola_quick_menu svg{
			width: 28px;height: 28px;
		}
	}

</style>
		<?php
	}
	/**
	 * Add custom menu items to admin bar
	 * 向admin bar里添加自定义链接
	 * 
	 */
	add_action( 'wp_before_admin_bar_render', 'sola_toolbar_quick_menu');

	function sola_toolbar_quick_menu() {

		global $wp_admin_bar;

		$svg_icon = '<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="hsl(132deg 90% 63%)"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M7 10c-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4-1.79-4-4-4zm0 6c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm8.01-1c-1.65 0-3 1.35-3 3s1.35 3 3 3 3-1.35 3-3-1.35-3-3-3zm0 4c-.55 0-1-.45-1-1s.45-1 1-1 1 .45 1 1-.45 1-1 1zM16.5 3C13.47 3 11 5.47 11 8.5s2.47 5.5 5.5 5.5S22 11.53 22 8.5 19.53 3 16.5 3zm0 9c-1.93 0-3.5-1.57-3.5-3.5S14.57 5 16.5 5 20 6.57 20 8.5 18.43 12 16.5 12z"/></svg>';
		
		// Parent menu item ID
		$menu_id = 'sola_quick_menu';

		$menu_items = array(
			array(
				'id'    => 'sola_quick_menu',
				'title' => sprintf('<span>%s</span><span>%s</span>',$svg_icon,'我的快捷菜单'),
				'href'  => '#',
				'meta' => array(
					'class' => 'menupop'
				),
			),

			// Group Status
			array(
				'id' => 'group-status',
				'parent' => $menu_id,
				'group' => true,
			),
			array(
				'id'     => 'manage_orders',
				'title'  => 'Orders',
				'href'   => admin_url() . 'edit.php?post_type=shop_order',
				'parent' => 'group-status',
			),
			array(
				'id'     => 'manage_customers',
				'title'  => 'Customers',
				'href'   => admin_url() . 'admin.php?page=wc-admin&path=%2Fcustomers',
				'parent' => 'group-status',
			),
			array(
				'id'     => 'wc_analytics',
				'title'  => 'Store Analytics',
				'href'   =>  admin_url() . 'admin.php?page=wc-admin&path=%2Fanalytics%2Foverview',
				'parent' => 'group-status',
			),


			// group-store-manager
			array(
				'id' => 'group-store-manager',
				'parent' => $menu_id,
				'group' => true,
			),
			array(
				'id'     => 'manage_products',
				'title'  => 'Products',
				'href'   => admin_url() . 'edit.php?post_type=product',
				'parent' => 'group-store-manager',
			),
			
			array(
				'id'     => 'manage_store_notice',
				'title'  => 'Store Notice',
				'href'   => get_admin_url().'customize.php?url=&autofocus%5Bsection%5D=woocommerce_store_notice',
				'parent' => 'group-store-manager',
			),
			
			array(
				'id'     => 'manage_shippings',
				'title'  => 'Shipping Rules',
				'href'   => admin_url() . 'admin.php?page=wc-settings&tab=shipping&section=wbs',
				'parent' => 'group-store-manager',
			),
			array(
				'id'     => 'manage_coupons',
				'title'  => 'Coupons',
				'href'   => admin_url() . 'edit.php?post_type=shop_coupon',
				'parent' => 'group-store-manager',
			),
			
			// group-wp-manager
			array(
				'id' => 'group-wp-manager',
				'parent' => $menu_id,
				'group' => true,
			),
			array(
				'id'     => 'manage_plugins',
				'title'  => 'Manage Plugins',
				'href'   => admin_url() . 'plugins.php',
				'parent' => 'group-wp-manager',
			),
			array(
				'id'     => 'manage_users',
				'title'  => 'Manage WP Users',
				'href'   => admin_url() . 'users.php',
				'parent' => 'group-wp-manager',
			),
			
		);
		
		foreach( $menu_items as $menu_item_option ){
			$wp_admin_bar->add_menu( $menu_item_option );
		}	
	}
}