Woocommerce shop manager role, hide woocommerce menu - wordpress

I am using a wordpress theme which supports woocommerce,
when adding a user with shop manager role i don't want to show the woocommerce menu.
Just need the products menu only.
please help.

You can use WordPress's 'remove_menus()' function to do this.
Store Managers have a capability: 'manage_woocommerce'
You can see that they are allowed to see the WooCommerce admin menu here:
'/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-menus.php'
Look for: $main_page = add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'manage_woocommerce', 'woocommerce' , array( $this, 'settings_page' ), null, '55.5' );
So much for the theory. To stop this admin menu item from displaying for anyone but an Administrator, add this to your functions.php file or plugin:
add_action( 'admin_menu', 'remove_menus' );
function remove_menus(){
// If the current user is not an admin
if ( !current_user_can('manage_options') ) {
remove_menu_page( 'woocommerce' ); // WooCommerce admin menu slug
}
}

Don't have the rep points to add a comment, but needs to change the hooked action from:
add_action( 'admin_menu', 'remove_menus' );
to:
add_action( 'admin_init', 'remove_menus' );
and then you can do something like:
function remove_menus(){
// If the current user is not an admin
if ( !current_user_can('manage_options') ) {
remove_submenu_page('woocommerce', 'wc-status');
}
}
if you are trying to remove core woocommerce submenu items.
(responding to Do Xuan Nguyen's comment)

Related

Wordpress change metabox title

Actually, I'm working on woocommerce customization...
I've read this question about the title change in metabox:
Customizing WooCommerce Short Description Metabox title
It help me a lot. But, in the product, how can I find the "metabox tag" callback name?
function epptm_rename_meta_boxes(){
remove_meta_box( 'tagsdiv-product_tag', 'product', 'side' );
add_meta_box( 'tagsdiv-product_tag', __( 'This metabox is awesome', 'your-plugin' ), 'CALLBACK?', 'product', 'side' );
}
add_action( 'add_meta_boxes', 'epptm_rename_meta_boxes', 40 );
here is another solution found:
/**
* hook to the 'add_meta_boxes' action to modify title
*/
function change_meta_box_titles() {
global $wp_meta_boxes; // array of defined meta boxes
// cycle through the array, change the titles you want
$wp_meta_boxes['product']['side']['core']['tagsdiv-product_tag']['title']= 'Here is my new title';
}
add_action('add_meta_boxes', 'change_meta_box_titles');

Add a new block to Woocommerce admin order page

I want to create a custom table block in Woocommerce admin order page.
As you can see in a screenshot, I have used :
add_action( 'woocommerce_admin_order_data_after_order_details', 'vp_admin_order_table' );
What I want is to create a separate block, with this table inside it.
Is there any action to trigger gap between Order details and Products list?
Try this code:
function op_register_menu_meta_box() {
add_meta_box(
'Some identifier of your custom box',
esc_html__( 'Box Title', 'text-domain' ),
'render_meta_box',
'shop_order', // shop_order is the post type of the admin order page
'normal', // change to 'side' to move box to side column
'low' // priority (where on page to put the box)
);
}
add_action( 'add_meta_boxes', 'op_register_menu_meta_box' );
function render_meta_box() {
// Metabox content
echo '<strong>Your awesome content goes here</strong>';
}
Remember to set to box as visible under "Screen Options" on the order page.
Further reading:
https://developer.wordpress.org/reference/functions/add_meta_box/
https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes

How to remove get_subscription_totals_template function from the hook woocommerce_subscription_totals_table

I want to replace the Subscription Totals given by the WooCommerce Subscriptions with my plugins version of Subscription Totals table.
In the WooCommerce Subsciptions plugin's class-wcs-template-loader.php there is a line that loads the subscription total template
add_action( 'woocommerce_subscription_totals_table', array( __CLASS__, 'get_subscription_totals_template' ) );
I'm trying to remove the function from the hook and replace it my own version of template. But it is not working!
I've written the following in my plugin file
remove_action( 'woocommerce_subscription_totals_table', array( __CLASS__, 'get_subscription_totals_template' ) );
but it is not working.
Put this instead:
remove_action( 'woocommerce_subscription_totals_table', array( 'WCS_Template_Loader', 'get_subscription_totals_template' ) );

Hide WooThemes menu from WordPress Admin

I'm using a WooThemes child theme and want to hide the menu from the WordPress admin bar. I use the following code so that only the users held within the array get to see all the options.
function remove_items_from_menu() {
$admins = array(
'Bill', 'Steve', 'Rob'
);
$current_user = wp_get_current_user();
if( !in_array( $current_user->user_login, $admins ) ) {
// exit;
add_filter('acf/settings/show_admin', '__return_false');
remove_action('load-update-core.php','wp_update_plugins');
remove_action( 'admin_notices', 'update_nag', 3 );
remove_menu_page('edit.php?post_type=acf-field-group');
remove_menu_page('edit-comments.php');
remove_menu_page('tools.php');
remove_submenu_page( 'index.php', 'update-core.php' );
remove_menu_page('themes.php');
remove_menu_page('plugins.php');
remove_submenu_page( 'themes.php', 'themes.php' );
remove_submenu_page( 'themes.php', 'widgets.php' );
remove_submenu_page( 'themes.php', 'customize.php' );
remove_submenu_page( 'themes.php', 'theme-editor.php' );
remove_submenu_page('options-general.php', 'options-permalink.php');
remove_submenu_page('options-general.php', 'options-media.php');
remove_submenu_page('options-general.php', 'options-discussion.php');
remove_submenu_page('options-general.php', 'options-reading.php');
remove_submenu_page('options-general.php', 'options-writing.php');
remove_submenu_page( 'options-general.php', 'social-sharing-admin' );
}
}
add_action( 'admin_menu', 'remove_items_from_menu', 999 );
?>
Finding the page to hide for the theme displays itself as admin.php?page=woothemes, but adding the following line to the above code still doesn't hide it from view.
remove_menu_page('admin.php?page=woothemes');
Does anybody know how I can find out the correct page ID to hide this specific menu option? I've searched online and cannot find how to hide a WooThemes menu from the admin bar.
Thanks.
It depends on the id passed to add_menu_page when it was called by the WooTheme. You can search over the code, but I think that
remove_menu_page('woothemes');
should do it.
Hope it helps.

How to add custom fields for WooCommerce order page (admin)?

I would like to add few input fields for the orders in WooCommerce/Wordpress. The fields will be only visible on the admin page. How to do that? Is it same like the pages/posts?
For the post types above I used add_post_meta. Is it the best way for that, or is there any hook for order fields?
Yes, just add_meta_box() to the post type shop_order and proceed as normal.
add_action( 'add_meta_boxes', 'add_meta_boxes' );
function add_meta_boxes()
{
add_meta_box(
'woocommerce-order-my-custom',
__( 'Order Custom' ),
'order_my_custom',
'shop_order',
'side',
'default'
);
}
function order_my_custom()
{
echo '<h1>Sample meta box</h1>';
}
Because you're asking specifically about WooCommerce, I'd like to expand on brasofilo's accepted answer by showing how you'd get the relevant WooCommerce order object (WC_Order) for the page the meta box is on.
Here's how you'd display the billing email from the order:
add_action('add_meta_boxes', function() {
add_meta_box('woocommerce-order-meta-24179529',
__( 'My Meta Box Title' ),
function() {
global $post;
// wc_get_order() returns one of bool|WC_Order|WC_Order_Refund
$order = wc_get_order($post->ID);
if($order instanceof WC_Order) {
echo "Billing email: ";
echo $order->get_billing_email();
}
},
'shop_order',
'side',
'default'
);
});

Resources