woocommerce offtopic – Customize or change error message from capability

Question

I have a woocommerce set up that allows certain people to edit an order, unless the status of the order changes to make sure they don’t revert status of an order that’s in processing to something that’s not since actual processing happens in another system and that can only handle if an order is being processed. It has no status (= order only gets sent when it’s in processing in WC).

I have managed this through map_meta_cap. Currently I return a ‘do_not_allow’. But this shows a very basic answer. Is there a way to create a custom message that I can assign to when this is needed instead of changing the message for ‘do_not_allow’?
Do I need to create an entirely new capability for this?

What I currently have and all I want to do is change the error message displayed to something custom:

add_filter('map_meta_cap', 'restrict_edit_orders', 10, 4);

function restrict_edit_orders($caps, $cap, $user_id, $args) {
    $user = get_user_by('id', $user_id);
  if (!$user) {
    return $caps;
  }
    if (!in_array('administrator', (array) $user->roles)) {
        $to_filter = ['edit_post', 'delete_post', 'edit_page', 'delete_page'];

        // If the capability being filtered isn't of our interest, just return current value
        if (!in_array($cap, $to_filter, true)) {
            return $caps;
        }

        // First item in $args array should be page ID, wpse_user_can_edit determines status of order
        if (!$args || empty($args[0]) || !wpse_user_can_edit($user_id, $args[0])) {
            // User is not allowed, let's tell that to WP - This is what I want to change!
             return ['do_not_allow'];
        }
        // Otherwise just return current value
    }
    return $caps;
}

0
Nahbyr 1 month 2021-06-21T05:06:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse