Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the "Orders" menu position when using HPOS #1330

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 72 additions & 27 deletions includes/class-wc-calypso-bridge-ecommerce-admin-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Class Ecommerce_Atomic_Admin_Menu.
*
* @since 1.9.8
* @version 2.2.17
* @version x.x.x
*
* The admin menu controller for Ecommerce WoA sites.
*/
Expand Down Expand Up @@ -39,14 +39,15 @@ public function __construct() {
add_filter( 'submenu_file', array( $this, 'modify_woocommerce_menu_highlighting' ), 99999 );
}

// Move Orders.
// TODO: What about the COT menu?
add_filter( 'woocommerce_register_post_type_shop_order', function( $args ) {
$args[ 'labels' ][ 'add_new' ] = __( 'Add New', 'woocommerce' );
$args[ 'show_in_menu' ] = true;
$args[ 'menu_icon' ] = 'dashicons-cart';
return $args;
} );
// Move Orders CPT.
if ( ! \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
add_filter( 'woocommerce_register_post_type_shop_order', function( $args ) {
$args[ 'labels' ][ 'add_new' ] = __( 'Add New', 'woocommerce' );
$args[ 'show_in_menu' ] = true;
$args[ 'menu_icon' ] = 'dashicons-cart';
return $args;
} );
}

// Ensure the $submenu['woocommerce] will be available at prio 10.
add_action( 'admin_menu', function() {
Expand Down Expand Up @@ -93,12 +94,58 @@ protected function handle_free_trial_menu() {
}, 99999 );
}

/**
* Moves the "WooCommerce > Orders" menu item to the top level.
*
* @since x.x.x
*
* @return void
*/
protected function handle_orders_menu() {
global $submenu, $menu;

if ( \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {

// Create the toplevel menu from scratch.
$this->hide_submenu_page( 'woocommerce', 'wc-orders' );
add_menu_page( __( 'Orders', 'woocommerce' ), __( 'Orders', 'woocommerce' ), 'edit_shop_orders', 'admin.php?page=wc-orders', null, 'dashicons-cart', 40 );
add_submenu_page( 'admin.php?page=wc-orders', __( 'Orders', 'woocommerce' ), __( 'Orders', 'woocommerce' ), 'edit_shop_orders', 'admin.php?page=wc-orders', null, 1 );
add_submenu_page( 'admin.php?page=wc-orders', __( 'Add New Order', 'woocommerce' ), __( 'Add New', 'woocommerce' ), 'edit_shop_orders', 'admin.php?page=wc-orders&action=new', null, 2 );
} else {

// Restore the Orders submenu CPT page for backwards compatibility.
add_submenu_page( 'woocommerce', __( 'Orders', 'wc-calypso-bridge' ), __( 'Orders', 'wc-calypso-bridge' ), 'manage_woocommerce', 'edit.php?post_type=shop_order', '', 1 );
$this->hide_submenu_page( 'woocommerce', 'edit.php?post_type=shop_order' );
}

// Add Orders count.
if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'edit_others_shop_orders' ) ) {
$order_count = (int) apply_filters( 'woocommerce_menu_order_count', wc_processing_order_count() );

if ( $order_count ) {
$bubble = '<span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

// Add for Orders (CPT).
foreach ( $menu as $i => $menu_item ) {
if ( 'edit.php?post_type=shop_order' === $menu_item[2] || 'admin.php?page=wc-orders' === $menu_item[2] ) {
$menu[ $i ][0] .= ' ' . $bubble;
break;
}
}

// Add for Orders (HPOS) submenu.
if ( isset( $submenu['admin.php?page=wc-orders'][0] ) && is_string( $submenu['admin.php?page=wc-orders'][0][0] ) ) {
$submenu['admin.php?page=wc-orders'][0][0] .= ' ' . $bubble;
}
}
}
}

/**
* Override the base implementation of add_plugins_menu() to avoid
* adding the Plugins menu for eCommerce trials.
*
* @since 2.0.8
* @version 2.0.8
*
* @return void
*/
Expand Down Expand Up @@ -131,6 +178,7 @@ public static function menu_order( $menu_order ) {

$woocommerce_menu_order[] = 'wc-calypso-bridge-separator-top'; // Separator WC top.
$woocommerce_menu_order[] = 'edit.php?post_type=shop_order'; // Orders.
$woocommerce_menu_order[] = 'admin.php?page=wc-orders'; // Orders (HPOS).
$woocommerce_menu_order[] = 'edit.php?post_type=product'; // Products.
$woocommerce_menu_order[] = 'admin.php?page=wc-admin&path=/customers'; // Customers.
if ( false !== $payments_connect_exists ) {
Expand Down Expand Up @@ -165,6 +213,7 @@ public static function menu_order( $menu_order ) {
'wc-admin&path=/analytics/overview',
'edit.php?post_type=product',
'edit.php?post_type=shop_order',
'admin.php?page=wc-orders',
'admin.php?page=wc-admin&path=/customers'
), true ) ) {
$woocommerce_menu_order[] = $item;
Expand Down Expand Up @@ -247,6 +296,17 @@ public function modify_woocommerce_menu_highlighting( $submenu_file ) {
} );
}

// Move WooCommerce > Orders (HPOS) to Orders.
if ( in_array( $screen_id, array( 'woocommerce_page_wc-orders' ), true ) ) {
$plugin_page = '';
$parent_file = 'admin.php?page=wc-orders';
$submenu_file = 'admin.php?page=wc-orders';

if ( isset( $_GET['action'] ) && 'new' === $_GET['action'] ) {
$submenu_file = 'admin.php?page=wc-orders&action=new';
}
}

return $submenu_file;
}

Expand All @@ -271,9 +331,8 @@ public function add_woocommerce_menu() {
// Hide WooCommerce > Home.
$this->hide_submenu_page( 'woocommerce', 'wc-admin' );

// Restore the Orders submenu page for backwards compatibility.
add_submenu_page( 'woocommerce', __( 'Orders', 'wc-calypso-bridge' ), __( 'Orders', 'wc-calypso-bridge' ), 'manage_woocommerce', 'edit.php?post_type=shop_order', '', 1 );
$this->hide_submenu_page( 'woocommerce', 'edit.php?post_type=shop_order' );
// Handle Orders menu item.
$this->handle_orders_menu();

// Move WooCommerce > Settings under Settings > WooCommerce.
$this->hide_submenu_page( 'woocommerce', 'wc-settings' );
Expand Down Expand Up @@ -321,20 +380,6 @@ public function add_woocommerce_menu() {
$this->hide_submenu_page( 'woocommerce', 'wc-addons' );
}

// Add Orders count.
if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'edit_others_shop_orders' ) ) {
$order_count = (int) apply_filters( 'woocommerce_menu_order_count', wc_processing_order_count() );

if ( $order_count ) {
foreach ( $menu as $i => $menu_item ) {
if ( 'edit.php?post_type=shop_order' === $menu_item[2] ) {
$menu[ $i ][0] .= ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
break;
}
}
}
}

// Re-order submenus.
$this->reorder_woocommerce_menu();
$this->reorder_settings_menu();
Expand Down
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ This section describes how to install the plugin and get it working.

== Changelog ==

= unreleased =
= Unreleashed =
* Fix the "Orders" menu position when using HPOS #1330
* Preconfigure product measurement units #xxx

= 2.2.17 =
Expand Down
Loading