Remove or hide add new button on woocommerce on bulk order panel
A good alternative is to add some custom CSS to hide "Add Order" buttons targeting conditionally user roles capabilities, in a custom function hooked in admin_head
action hook:
add_action( 'admin_head', 'my_custom_admin_styles' );
function my_custom_admin_styles() {
// HIDE "New Order" button when current user don't have 'manage_options' admin user role capability
if( ! current_user_can( 'manage_options' ) ):
?>
<style>
.post-type-shop_order #wpbody-content > div.wrap > a.page-title-action{
display: none !important;
}
</style>
<?php
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and perfectly works.
Remove submenu for 'Add Order' in WooCommerce on WordPress admin dashboard
I'm using WC 4.4.1
& WC 4.6.0
and in both versions there is no possibility to create a new order from the menu.
UPDATE: Due to the output you posted, this should suffice to remove "Order: add new"
function action_admin_menu() {
global $menu, $submenu;
// Unset 'Order: add new'
unset( $submenu['edit.php?post_type=shop_order'][10] );
}
add_action( 'admin_menu', 'action_admin_menu' );
Optional: For "Products: add new" and DEBUGGING you could use
// DEBUG: This displays the complete wordpress admin menu on your dashboard for admin only. (Remove afterwards)
function debug_admin_menus() {
global $menu, $submenu, $pagenow;
if ( current_user_can('manage_options') ) {
if( $pagenow == 'index.php' ) { // print on dashboard
echo '<pre>', print_r( $menu, 1 ), '</pre>'; // top level menus
echo '<pre>', print_r( $submenu, 1 ), '</pre>'; // submenus
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
function action_admin_menu() {
global $menu, $submenu;
// Unset 'Products: add new'
unset( $submenu['edit.php?post_type=product'][10] );
}
add_action( 'admin_menu', 'action_admin_menu' );
Related:
Change WooCommerce products menu title in WordPress admin dashboard
How to rename a menu tab under WooCommerce tab on WordPress admin dashboard
Remove 'Move to Trash' under woocommerce order action for shop manager
To prevent deleting post/order/custom post is capability per user role. To prevent shop_manager role from deleting orders you must remove that capability. All other solutions will visualy work but someone who knows his stuff still can delete order if he wants. So place the following function in your functions.php
function wp23958_remove_shop_manager_capabilities() {
$shop_manager = get_role( 'shop_manager' ); // Target user role
//List of capabilities which we want to edit
$caps = array(
'delete_shop_orders',
'delete_private_shop_orders',
'delete_published_shop_orders',
'delete_others_shop_orders',
);
// Remove capabilities from our list
foreach ( $caps as $cap ) {
$shop_manager->remove_cap( $cap );
}
}
add_action( 'init', 'wp23958_remove_shop_manager_capabilities' );
Bonus How to know what capabilities current user have
function wp32985_check_user_capabilities() {
$data = get_userdata( get_current_user_id() );
if ( is_object( $data) ) {
$current_user_caps = $data->allcaps;
// print it to the screen
echo '<pre>' . print_r( $current_user_caps, true ) . '</pre>';
}
}
add_action( 'init', 'wp32985_check_user_capabilities' );
Remove WooCommerce admin order action from order preview
To remove the "complete" update order status button from admin order preview for "Shop manager" user role, use the following:
add_filter( 'woocommerce_admin_order_preview_actions', 'filter_admin_order_preview_actions', 10, 2 );
function filter_admin_order_preview_actions( $actions, $order ) {
if( current_user_can('shop-manager') && isset($actions['status']['actions']['complete']) ) {
unset($actions['status']['actions']['complete']);
}
return $actions;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Hide a custom action button on click displaying a text in WooCommerce admin orders list
Javascript is not the way to achieve this. You will use the following instead to hide the link and display "Message sent" once an external link has been clicked:
add_filter( 'manage_edit-shop_order_columns', 'dvs_whatsapp_msg_list_column' );
function dvs_whatsapp_msg_list_column( $columns ) {
$columns['whatsapp'] = __('WhatsApp', 'woocommerce');
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'dvs_whatsapp_msg_list_column_content' );
function dvs_whatsapp_msg_list_column_content( $column ) {
if ( 'whatsapp' === $column ) {
global $the_order;
if( ! $the_order->get_meta('_wapp_sent') ) {
echo '<a href="?post_type=shop_order&send=dvs_whatsapp&order_id=' . $the_order->get_id() .' target="blank" class="dvs-whatsapp button">' . __("Send WhatsApp") . '</a>';
}
else {
echo __("Message sent", "woocommerce");
}
}
}
add_action( 'admin_init', 'dvs_redirect_whatsapp_send' );
function dvs_redirect_whatsapp_send() {
global $pagenow;
# Check current admin page.
if ( $pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'shop_order'
&& isset($_GET['send']) && $_GET['send'] == 'dvs_whatsapp' && isset($_GET['order_id']) && $_GET['order_id'] > 0 ) {
$order = wc_get_order( $_GET['order_id'] );
$msg = sprintf( __("Hello %s %s, your order #%s has been received. The order amount is %s. Your payment method is %s. %s", "woocommerce"),
$order->get_billing_first_name(),
$order->get_billing_last_name(),
$order->get_order_number(),
$order->get_total(),
$order->get_payment_method_title(),
__("Please contact us if you have any question regarding your order. Thank you.", "woocommerce")
);
$whatsapp_num = WC()->countries->get_country_calling_code( $order->get_billing_country() ) . $order->get_billing_phone();
update_post_meta( $_GET['order_id'], '_wapp_sent', 'true' ); // Mark order as WhatsApp message sent
wp_redirect( 'https://wa.me/' . $whatsappnum . '?text=' . urlencode($msg) ); // Redirect to WhatsApp sending service
exit;
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Enable and disable 'edit' on orders for shop manager (woocommerce)
Use get_current_user()
, this way you can get user object then check user's role from the object and if it is shop manager then apply your code as you mentioned.
add_filter( 'woocommerce_register_post_type_shop_order','your_function_name' );
function your_function_name($fields) {
$user = wp_get_current_user();
if ( in_array( 'shop_manager', (array) $user->roles ) ) {
$fields['capabilities'] = array(
'create_posts' => false,
);
return $fields;
} else {
return $fields;
}
}
Add a custom action button in WooCommerce admin order list
To resume, you have created a custom order status 'wc-parcial' (with the instructions code provided in your question) and you need to add a related action button to orders admin list.
For WooCommerce version 3.3+ check the update in this answer below
You need to use a custom function hooked in woocommerce_admin_order_actions
filter hook
// Add your custom order status action button (for orders with "processing" status)
add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );
function add_custom_order_status_actions_button( $actions, $order ) {
// Display the button for all orders that have a 'processing' status
if ( $order->has_status( array( 'processing' ) ) ) {
// Get Order ID (compatibility all WC versions)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
// Set the action button
$actions['parcial'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=parcial&order_id=' . $order_id ), 'woocommerce-mark-order-status' ),
'name' => __( 'Envio parcial', 'woocommerce' ),
'action' => "view parcial", // keep "view" class for a clean button CSS
);
}
return $actions;
}
// Set Here the WooCommerce icon for your action button
add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
echo '<style>.view.parcial::after { font-family: woocommerce; content: "\e005" !important; }</style>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works. You will get that:
Related Topics
Run Cron Job on PHP Script, on Localhost in Windows
PHP Function Use Variable from Outside
Save PHP Variables to a Text File
In PHP, How to Detect the Execution Is from Cli Mode or Through Browser
Passing PHP Objects to JavaScript
Xmlparseentityref: No Name' Warnings While Loading Xml into a PHP File
How to Preview an Image Before and After Upload
How to Add Some Textfield in Form Register (Laravel Generator Infyom)
Understanding MVC: Whats the Concept of "Fat" on Models, "Skinny" on Controllers
How to Convert a String to JSON Object in PHP
A Better Approach Than Storing MySQL Password in Plain Text in Config File
Get Woocommerce Product Categories from Wordpress
PHP - Get Numeric Index of Associative Array
How to Read Gzip-Ed Response from Stackoverflow API in PHP
Getting the First Character of a String with $Str[0]
How to Get All Captures of Subgroup Matches with Preg_Match_All()