I would like specific users to only see the WooCommerce -> Settings -> Shipping menu. I've managed to remove other tabs e.g. Products, Payments, etc, but stuck on the following 2 things that I want to accomplish:
Remove the "GENERAL" tab in WooCommerce Settings.
Remove the "ORDER STATUSES" Tab from a plugin. (Note: this isn't a tab exactly, the slug is
'edit.php?post_type=wc_order_status')
When I try to remove the GENERAL tab, it eliminates the entire Settings menu. As for the 'Order Statuses', my code just doesn't work.
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {
global $current_user;
//Declare the tabs we want to hide
$tabs_to_hide = array(
'general' => 'General', //this one removes the entire Settings menu
'wc_order_status' => 'Order Statuses'// this doesn't work, maybe bc it's a post_type
);
// Remove tab if user role is shipping_manager
if ( in_array("shipping_manager", $$current_user->roles) ) {
$array = array_diff_key($array, $tabs_to_hide);
}
}
I had also tried the below code to remove the ORDER STATUSES tab, but still no luck:
add_action( 'admin_menu', 'remove_order_statuses_tab', 999);
function remove_order_statuses_tab()
{
global $current_user;
if ( in_array("shipping_manager", $current_user->roles) ) {
remove_menu_page( 'edit.php?post_type=wc_order_status' ); //not working either
}
}
The correct hook to be used is woocommerce_settings_tabs_array.
First you need to find which are the array keys that you need to remove from the tabs array in your code.
For that you will use first the following function that will display all array data in Admin WooCommerce settings (only for testing, to be removed):
add_filter( 'woocommerce_settings_tabs_array', 'filter_wc_settings_tabs_array', 990, 1 );
function filter_wc_settings_tabs_array( $tabs_array ) {
// Display raw array data
echo '<pre>'; print_r( $tabs_array ); echo '</pre>';
return $tabs_array;
}
It will display something like (with all required array keys):
Now you can get the array keys slugs that you need, to remove the corresponding tab settings. So now you will be able to find the correct array key slug for you Third party plugin WooCommerce setting tab, that you will use in the function code below.
To target a specific user role, you can either use global $current_user; $current_user->roles; or the Wordpress dedicated function current_user_can()…
So the working code that will remove specific setting tabs for a user role is:
add_filter( 'woocommerce_settings_tabs_array', 'filter_wc_settings_tabs_array', 200, 1 );
function filter_wc_settings_tabs_array( $tabs_array ) {
// Only for "shipping_manager" user role
if( current_user_can( 'shipping_manager' ) ) {
// Remove some specific tabs
unset( $tabs_array['general'], $tabs_array['order_status'] ); // <== replace 'order_status' by
}
return $tabs_array;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
add_filter( 'woocommerce_settings_tabs_array', 'remove_woocommerce_setting_tabs', 200, 1 );
function remove_woocommerce_setting_tabs( $array ) {
global $current_user;
// Remove tab if user role is shipping_manager
if ( in_array( "shipping_manager", $current_user->roles ) ) {
unset( $array[ 'general' ] );
?>
<script>
document.querySelector("[href='<?php echo esc_url( admin_url( 'edit.php?post_type=wc_order_status' ) ); ?>']").style.display = 'none';
</script>
<?php
}
return $array;
}
Try this code snippet
Related
I am trying to customise the woocommerce cart widget and make it more "visual" like the below example. I have researched and it seems I may be able to override the woocommerce cart widget with my own behaviour through the hook woocommerce_mini_cart().
End result I would like to get to:
Is it possible to modify the core functionality of the cart widget via this approach to achieve something like this, through the functions.php file or would I need CSS aswell?
if ( ! function_exists( ‘woocommerce_mini_cart’ ) ) {
function woocommerce_mini_cart( $args = array() ) {
$defaults = array( ‘list_class’ => ” );
$args = wp_parse_args( $args, $defaults );
woocommerce_get_template( ‘cart/mini-cart.php’, $args );
}
//*MODIFY HERE?*
}
Alternatively does anyone know of a woocommerce plugin which could solve this?
Despite of the question is not actual for its` author already,
I wanna to share some code to modify the default woocommerce_mini_cart() output:
1) The most difficult step - disable ECHO for woocommerce_mini_cart() function
2) change output HTML as you wish;
3) echo formatted HTML.
1)
function disable_echo_for_woocommerce_mini_cart() {
$mini_cart_html = woocommerce_mini_cart();
return $mini_cart_html;
}
2)
$mini_cart_html = disable_echo_for_woocommerce_mini_cart();
$mini_cart_html = str_replace('some_code', 'some_code_2', $mini_cart_html); // change output HTML.
$mini_cart_html = ob_get_clean();
3)
echo $mini_cart_html;
I am running a wholesale shop on Woocommerce. Login is required to see the prices. This is set up and working properly. Now I wish to add a logon form on every product page to only show to visitors (not logged on users).
I am using the WooCommerce Catalog Visibility plugin. This plugin offers the functionality I described above, but my theme is somehow messing it up. The plugin author says to talk to the theme developer and the theme developer says to talk to the plugin author. So now I am trying to find a workaround.
First issue: The plugin comes with a shortcode [woocommerce_logon_form] that will display a logon form. I don't want to manually add this to every existing product since I have thousands of products on my site. I am looking for a way to get it in through the code for the product page layout.
I found this code (to be added to the functions.php) to work well:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
However, it will only show text. The short code won't work when added instead of the sample text.
Second issue: The short code shows the form even when the customer is already logged in.
I am currently using this nice code that shows or hides content depending on whether the user is logged in or not:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
That shortcode works perfectly for text, but not with other shortcodes.
So the combination of these short codes: [visitor][woocommerce_logon_form][/visitor] will not show the logon form to visitors. Instead it will only show them this as text [woocommerce_logon_form].
Please help! I am sure this is probably easily fixed by someone with coding skills.
I appreciate your effort to answer to this question. Keep in mind that my understanding of code is very limited and it would be great if you can also point out in which file to add or modify code.
To make your shortcode working in php code or in php/html code you need to use a native WordPress function do_shortcode() … You can use it with your shortcode for example in your 1st function this way:
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo do_shortcode('[woocommerce_logon_form]');
}
And this will work…
To see all the different hooks you can use instead of woocommerce_single_product_summary, please see this 2 templates code to chose in case a more convenient hook:
WooCommerce single-product.php template
WooCommerce content-single-product.php template
You can also add it the same way in one of your existing short codes, this way:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode('[woocommerce_logon_form]');
return '';
}
And this will work too.
See as reference this answer: Change markup in WooCommerce shortcode output
So as you can see your problem is solved on both issues
I'm using a WordPress plugin https://github.com/lesterchan/wp-postratings.
It also showing ratings on admin, when i visit http://domain.com/wp-admin/edit.php.
How do i remove those ratings from admin side.
You can use the below function in your functions.php file with the manage_posts_columns filter. I'm assuming your custom post type id 'tools' and the column is referenced by 'ratings'. If they are different you can just change that in the code.
add_filter( 'manage_posts_columns', 'custom_post_columns', 10, 2 );
function custom_post_columns( $columns, $post_type ) {
switch ( $post_type ) {
//assuming the id for the custom post type is 'tools'
case 'tools':
unset(
//I'm using 'ratings' but you'll have to check and see what the name for the column really is.
$columns['ratings']
);
break;
}
return $columns;
}
How can i disable the comments form for a specific category in Wordpress.
And i mean with that every post the user will publish it in this category will not has a comment form content.
Rather than doing this in functions.php, you could create two template files. The first template file is your standard theme template, the second would be identical, except it wouldn't contain the comment code section.
Simply name the template file that doesn't have the comment code block category_{id}.php and upload to your theme folder. The ID is the ID of the category you want to disable comments on.
More information on category specific templates here https://developer.wordpress.org/themes/basics/template-hierarchy/#category
More information about the comment template here https://codex.wordpress.org/Function_Reference/comments_template
If you still want to do this via functions.php, see this blog post http://spicemailer.com/wordpress/disable-hide-comments-posts-specific-categories/ which uses the following code snippet
add_action( 'the_post', 'st_check_for_closed' );
function st_check_for_closed()
{
global $post;
$my_post_cat = wp_get_post_categories($post->ID);
$disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs.
$my_result = array_intersect($my_post_cat,$disabled_cat);
if (empty ( $my_result ) )
{
return;
}
else {
add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
}
}
function st_deregister_reply_js()
{
wp_deregister_script( 'comment-reply' );
}
function st_close_comments_on_category ($open, $post_id)
{
$open = false;
}
I am developing a plugin for that I have to black list users, so I need to be display one more dropdown item called Black List inside the Bulk Actions dropdown in the Users page, after the Delete option. But I'm unable to see from where these two actions are coming from and also how to black list a particular user.
My idea is to add one more field is_blacklisted in user table as Boolean with default value false and when apply Black List action it changes to true. Any other thoughts?
There's a filter, but it's only useful to remove bulk actions.
From this WPSE question, answer and comments, there's the following workaround: add a custom option to the dropdown with jQuery and hook into admin_action_$your-action to catch the submission.
The hook admin_footer-$current_page is used to print our JavaScript on a specific admin page (adjust to use in other screens).
add_action( 'admin_footer-users.php', 'bulk_footer_so_23541269' );
add_action( 'admin_action_black_list', 'bulk_request_so_23541269' );
function bulk_footer_so_23541269()
{
# global $typenow; if( $typenow != 'page' ) return; // if used on edit.php screen
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('<option>').val('black_list').text('Black list')
.appendTo("select[name='action'], select[name='action2']");
});
</script>
<?php
}
function bulk_request_so_23541269()
{
# Array with the selected User IDs
wp_die( '<pre>' . print_r( $_REQUEST['users'], true ) . '</pre>' );
// $_REQUEST['post'] if used on edit.php screen
}
Your doubt about blocking a user deserves another question, but I'd start a research here first.
Proper support with add_filter( 'bulk_actions-screenid', 'register_my_bulk_actions' ) is arriving in Wordpress 4.7 .
Quoting the announcement post:
To add an option in the Bulk Actions dropdown HTML element, register a callback on the bulk_actions-{screen_id} filter that adds the new option onto the array. Replace {screen_id} with the ID of the admin screen to offer the bulk action on.
To add a bulk action “Email to Eric,” we could use the following code:
add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' );
function register_my_bulk_actions($bulk_actions)
{
$bulk_actions['email_to_eric'] = __( 'Email to Eric', 'email_to_eric');
return $bulk_actions;
}
To handle a bulk action form submission, register a callback on the handle_bulk_actions-{screen_id} filter for the corresponding screen. The filter expects the redirect URL to be modified, so be sure to modify the passed $redirect_url. This allows us to carry success or failure state into the next request to display a notice to the user. The other callback arguments will differ depending on the screen here to include contextually relevant data.
To add a bulk action handler for emailing the selected posts, we could use the following code:
add_filter( 'handle_bulk_actions-edit-post', 'my_bulk_action_handler', 10, 3 );
function my_bulk_action_handler( $redirect_to, $doaction, $post_ids )
{
if ( $doaction !== 'email_to_eric' ) {
return $redirect_to;
}
foreach ( $post_ids as $post_id ) {
// Perform action for each post.
}
$redirect_to = add_query_arg( 'bulk_emailed_posts', count( $post_ids ), $redirect_to );
return $redirect_to;
}
Showing notices: We could use the existing notice hooks to let the user know what happened, depending on the state we set in the URL:
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );
function my_bulk_action_admin_notice()
{
if ( ! empty( $_REQUEST['bulk_emailed_posts'] ) ) {
$emailed_count = intval( $_REQUEST['bulk_emailed_posts'] );
printf( '<div id="message" class="updated fade">' .
_n( 'Emailed %s post to Eric.',
'Emailed %s posts to Eric.',
$emailed_count,
'email_to_eric'
) . '</div>', $emailed_count );
}
}