WordPress 首选的电商插件,自然是官方制作的 WooCommerce,而如果将其作为 WooCommerce 商店,最高频使用的页面当数订单页(Orders)。无论正在后台做什么操作,一旦顾客呼叫响起,都需要先查找到相应订单,才好做出准确的回答。
因此很有必要增加一个订单搜索栏,方便随时随地查找跳转到对应订单页面。而这个搜索栏的位置,显然是放在后台的管理员工具条 Admin Bar 上最为合适 —— 毕竟无论在什么页面,这个工具条都始终置顶。
实现方法:
将以下代码添加到正在使用的主题 functions.php 文件中:
function admin_bar_shop_order_form() {
global $wp_admin_bar;
$search_query = '';
if (!empty($_GET['post_type']) && $_GET['post_type'] == 'shop_order' ) {
$search_query = !empty($_GET['s']) ? $_GET['s'] : '';
if($search_query) {
$order = get_post($search_query);
if($order && $order->post_type == 'shop_order'){
wp_redirect(get_edit_post_link($order->ID, ''));
exit;
}
}
}
$wp_admin_bar->add_menu(array(
'id' => 'admin_bar_shop_order_form',
'parent' => 'top-secondary',
'title' => '<form target="_blank" method="get" action="'.get_site_url().'/wp-admin/edit.php?post_type=shop_order" style="display: flex; padding: 1px 0px; height: 100%; box-sizing: border-box;"><input name="s" type="text" placeholder="搜订单" value="' . $search_query . '" style="border:0; width:100px; height:100%; padding:0 10px; margin: 0;line-height:1em; text-overflow: ellipsis; white-space: nowrap; overflow: hidden;"/><button class="button button-primary" style="cursor: pointer; height: auto; line-height: 1; padding: 0 10px; border: 0;background: #0085ba;color: #fff; font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif; ">搜</button><input name="post_type" value="shop_order" type="hidden"></form>'
));
}
add_action('admin_bar_menu', 'admin_bar_shop_order_form');
效果演示:
无论在后台任何页面,只需要在管理工具条搜索框中输入关键词,敲击回车或者点击 “搜” 即可在新页面打开订单搜索结果。
也可以配合其他代码,实现搜索订单号、或是搜索结果唯一时,直接进入这唯一的订单页面。