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

所以我添加了一个名为“awaiting-shippment”的自定义订单状态,我想将这些订单包含在管理左侧 WooCommerce 下拉菜单下显示的订单计数中。此处显示的唯一订单是处于“处理中”状态的订单。

任何帮助,伙计们?提前非常感谢。


海绵宝宝撒
浏览 112回答 3
3回答

青春有我

我找到了一个公平的解决方案。这有点hacky......但它有效首先,将子菜单添加到 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' );}然后,添加一个函数以在您单击此菜单项时运行。这个函数(这里是 hacky 部分)会将用户重定向到常规订单页面,只显示请求的状态。(为了测试我使用“取消”状态,你应该改变它)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; }}

慕哥9229398

如果您想完全替换默认计数器,下面的代码段应该满足您的需求:<?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 测试。

ITMISS

这是解决方案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' );
打开App,查看更多内容
随时随地看视频慕课网APP