3 回答
TA贡献1784条经验 获得超8个赞
我找到了一个公平的解决方案。这有点hacky......但它有效
首先,将子菜单添加到 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' );
}
然后,添加一个函数以在您单击此菜单项时运行。这个函数(这里是 hacky 部分)会将用户重定向到常规订单页面,只显示请求的状态。(为了测试我使用“取消”状态,你应该改变它)
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贡献1877条经验 获得超6个赞
如果您想完全替换默认计数器,下面的代码段应该满足您的需求:
<?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 测试。
TA贡献1871条经验 获得超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' );
- 3 回答
- 0 关注
- 132 浏览
添加回答
举报