如何在Woocommerce的自定义订单状态的管理菜单中添加计数?

因此,我添加了一个名为“等待发货”的自定义订单状态,并且我想将这些订单包含在管理左侧WooCommerce下拉菜单下显示的订单计数中。此处显示的唯一订单是具有“正在处理”状态的订单。

有什么帮助吗,伙计们?提前非常感谢。


慕盖茨4494581
浏览 135回答 3
3回答

子衿沉夜

我找到了一个公平的解决方案。这有点笨拙...但它的工作原理首先,将子菜单添加到WooCommerce菜单:add_action( 'admin_menu', 'custom_status_menu', 50 );function custom_status_menu(){&nbsp; &nbsp; $status_page = add_submenu_page( 'woocommerce', 'Awaiting Shipment', __( 'Awaiting', 'woocommerce' ), 'manage_woocommerce', 'wc-awaiting', 'awaiting_orders_page' );}然后,添加要在单击此菜单项时运行的函数。此功能(这里是黑客部分)会将用户重定向到常规订单页面,仅显示请求的状态。(为了测试我使用了“已取消”状态,您应该更改)function awaiting_orders_page(){&nbsp; &nbsp; 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(){&nbsp; &nbsp; global $submenu;&nbsp; &nbsp; $order_count = wc_orders_count( 'awaiting' );&nbsp; &nbsp; foreach ( $submenu['woocommerce'] as $key => $menu_item ) {&nbsp; &nbsp; &nbsp; &nbsp; if ( 0 === strpos( $menu_item[0], 'Awaiting' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $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.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

婷婷同学_

这是解决方案function add_order_status_count(){&nbsp; &nbsp; global $submenu;&nbsp; &nbsp; $order_count = wc_orders_count( 'awaiting-shippment' );&nbsp; &nbsp; foreach ( $submenu['woocommerce'] as $key => $menu_item ) {&nbsp; &nbsp; &nbsp; &nbsp; if ( 0 === strpos( $menu_item[0], 'Orders' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $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.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}add_action( 'admin_head', 'add_order_status_count');如果您想禁用Woocommerce处理订单状态计数function remove_processing_order_count() {&nbsp; &nbsp; return false;}add_filter( 'woocommerce_include_processing_order_count_in_menu', 'remove_processing_order_count' );

慕标5832272

如果你想完全替换默认计数器,下面的代码段应该符合你的需求:<?phpadd_action( 'admin_head', 'usrlnd_action_change_order_counter' ), 999 );function usrlnd_action_change_order_counter() {&nbsp; &nbsp; global $submenu;&nbsp; &nbsp; if ( isset( $submenu['woocommerce'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Add count if user has access.&nbsp; &nbsp; &nbsp; &nbsp; if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'manage_woocommerce' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // my custom status slug is `new`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $order_count&nbsp; &nbsp; &nbsp;= wc_orders_count( 'new' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $old_order_count = wc_processing_order_count();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $order_count ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $submenu['woocommerce'] as $key => $menu_item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $old&nbsp; = ' <span class="awaiting-mod update-plugins count-' . esc_attr( $old_order_count ) . '"><span class="processing-count">' . number_format_i18n( $old_order_count ) . '</span></span>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new&nbsp; = ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item = $submenu['woocommerce'][ $key ][0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( strpos( $item, $old ) !== false ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // replace old counter with new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item = str_replace( $old, $new, $item );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // there is no counter, just add the new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $item .= $new;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $submenu['woocommerce'][ $key ][0] = $item;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}使用Woocommerce 4.2.2进行测试。
打开App,查看更多内容
随时随地看视频慕课网APP