Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. <?php // Only copy if required!
  2.  
  3. /**
  4. * Custom code to display only the "next status" status for `orders_manager` role in the WooCommerce
  5. * edit order screen - status selection, in order to keep to a specific order workflow.
  6. */
  7. function hide_specific_order_statuses_for_user_role() {
  8.  
  9. global $post;
  10.  
  11. // check if user has user role "orders_manager"
  12. if ( ! current_user_can( 'orders_manager' ) ) {
  13. return;
  14. }
  15.  
  16. // check if we are on a post
  17. if ( strpos( $_SERVER['REQUEST_URI'], 'post.php?post=' ) === false ) {
  18. return;
  19. }
  20.  
  21. // check if the post we are on is with post type "shop_order"
  22. if ( empty( $post ) || 'shop_order' !== $post->post_type ) {
  23. return;
  24. }
  25. ?>
  26. <script>
  27. jQuery(function () {
  28. <?php
  29.  
  30. global $woocommerce, $post;
  31.  
  32. // current order
  33. $order = new WC_Order( $post->ID );
  34.  
  35. // slug of the current order status of the order
  36. $status = new WC_Order_Status_Manager_Order_Status( $order->get_status() );
  37.  
  38. // "Next statuses" of the current order status
  39. $next_statuses = $status->get_next_statuses();
  40. $total_statuses = count( $next_statuses );
  41. $option_selector = '';
  42.  
  43. // for each "Next statuses"...
  44. for ( $i = 0; $i < $total_statuses; ++$i ) {
  45. // ...get the current "Next statuses"-status in the loop...
  46. $current_next_statuses = new WC_Order_Status_Manager_Order_Status( $next_statuses[$i] );
  47. // ...and make a part of a jQuery selector string that target elements that does not have that current "Next statuses"-status as a value
  48. $option_selector .= '[value!="wc-' . $current_next_statuses->get_slug() . '"]';
  49. }
  50. // 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()".
  51. echo 'jQuery(\'#order_status option' . $option_selector . '\').not(\'#order_status option[value="wc-' . $order->get_status() . '"]\').remove();';
  52. ?>
  53. });
  54. </script>
  55. <?php
  56. }
  57. add_action( 'admin_head', 'hide_specific_order_statuses_for_user_role' );
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement