Created
September 4, 2019 15:38
-
-
Save simonporter007/5e087bf134ce67c8130dc01d86a112b2 to your computer and use it in GitHub Desktop.
Merchant submitted code snippet to display only the next statuses configured with Order Status Manager for certain user roles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Daniel!