3 回答
TA贡献1828条经验 获得超3个赞
我找到了一个公平的解决方案。这有点笨拙...但它的工作原理
首先,将子菜单添加到WooCommerce菜单:
add_action( 'admin_menu', 'custom_status_menu', 50 );
function custom_status_menu(){
$status_page = add_submenu_page( 'woocommerce', 'Awaiting Shipment', __( 'Awaiting', 'woocommerce' ), 'manage_woocommerce', 'wc-awaiting', 'awaiting_orders_page' );
}
然后,添加要在单击此菜单项时运行的函数。此功能(这里是黑客部分)会将用户重定向到常规订单页面,仅显示请求的状态。(为了测试我使用了“已取消”状态,您应该更改)
function awaiting_orders_page(){
header('Location: /wp-admin/edit.php?post_status=wc-cancelled&post_type=shop_order');
}
最后,添加计数器。同样在这里,我使用了“等待”状态,将其更改为您创建的状态
add_action( 'admin_head', 'add_custom_status_count');
function add_custom_status_count(){
global $submenu;
$order_count = wc_orders_count( 'awaiting' );
foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
if ( 0 === strpos( $menu_item[0], 'Awaiting' ) ) {
$submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; // WPCS: override ok.
break;
}
}
}
TA贡献1844条经验 获得超8个赞
这是解决方案
function add_order_status_count(){
global $submenu;
$order_count = wc_orders_count( 'awaiting-shippment' );
foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
if ( 0 === strpos( $menu_item[0], 'Orders' ) ) {
$submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; // WPCS: override ok.
break;
}
}
}
add_action( 'admin_head', 'add_order_status_count');
如果您想禁用Woocommerce处理订单状态计数
function remove_processing_order_count() {
return false;
}
add_filter( 'woocommerce_include_processing_order_count_in_menu', 'remove_processing_order_count' );
TA贡献1966条经验 获得超4个赞
如果你想完全替换默认计数器,下面的代码段应该符合你的需求:
<?php
add_action( 'admin_head', 'usrlnd_action_change_order_counter' ), 999 );
function usrlnd_action_change_order_counter() {
global $submenu;
if ( isset( $submenu['woocommerce'] ) ) {
// Add count if user has access.
if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'manage_woocommerce' ) ) {
// my custom status slug is `new`
$order_count = wc_orders_count( 'new' );
$old_order_count = wc_processing_order_count();
if ( $order_count ) {
foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) {
$old = ' <span class="awaiting-mod update-plugins count-' . esc_attr( $old_order_count ) . '"><span class="processing-count">' . number_format_i18n( $old_order_count ) . '</span></span>';
$new = ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>';
$item = $submenu['woocommerce'][ $key ][0];
if ( strpos( $item, $old ) !== false ) {
// replace old counter with new
$item = str_replace( $old, $new, $item );
} else {
// there is no counter, just add the new
$item .= $new;
}
$submenu['woocommerce'][ $key ][0] = $item;
break;
}
}
}
}
}
}
使用Woocommerce 4.2.2进行测试。
- 3 回答
- 0 关注
- 161 浏览
添加回答
举报