Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // Only copy if required!
- /**
- * Custom code to display only the "next status" status for `orders_manager` role in the WooCommerce
- * edit order screen - status selection, in order to keep to a specific order workflow.
- */
- function hide_specific_order_statuses_for_user_role() {
- global $post;
- // check if user has user role "orders_manager"
- if ( ! current_user_can( 'orders_manager' ) ) {
- return;
- }
- // check if we are on a post
- if ( strpos( $_SERVER['REQUEST_URI'], 'post.php?post=' ) === false ) {
- return;
- }
- // check if the post we are on is with post type "shop_order"
- if ( empty( $post ) || 'shop_order' !== $post->post_type ) {
- return;
- }
- ?>
- <script>
- jQuery(function () {
- <?php
- global $woocommerce, $post;
- // current order
- $order = new WC_Order( $post->ID );
- // slug of the current order status of the order
- $status = new WC_Order_Status_Manager_Order_Status( $order->get_status() );
- // "Next statuses" of the current order status
- $next_statuses = $status->get_next_statuses();
- $total_statuses = count( $next_statuses );
- $option_selector = '';
- // for each "Next statuses"...
- for ( $i = 0; $i < $total_statuses; ++$i ) {
- // ...get the current "Next statuses"-status in the loop...
- $current_next_statuses = new WC_Order_Status_Manager_Order_Status( $next_statuses[$i] );
- // ...and make a part of a jQuery selector string that target elements that does not have that current "Next statuses"-status as a value
- $option_selector .= '[value!="wc-' . $current_next_statuses->get_slug() . '"]';
- }
- // make a jQuery selector string that target the "Status:" select field (#order_status), and inside that target all the options that does not have a value from the "Next statuses"-loop above. remove those options. Notice that the current order status is not removed becouse it is excluded from the jQuery selector with the "not()".
- echo 'jQuery(\'#order_status option' . $option_selector . '\').not(\'#order_status option[value="wc-' . $order->get_status() . '"]\').remove();';
- ?>
- });
- </script>
- <?php
- }
- add_action( 'admin_head', 'hide_specific_order_statuses_for_user_role' );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement