Custom ordering products based on user role - woocommerce

My shop is currently set to sort products by ascending price.
Is there a way I can sort products by custom ordering for a specific user role only?
Thank you for your help!

I've found the answer to this myself, for anyone wondering:
add_filter('woocommerce_default_catalog_orderby', 'change_catalog_orderby_specific_user');
function change_catalog_orderby_specific_user( $sort_by ) {
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
return 'menu_order';
}
else {
return 'price';
}
}

Related

Disable password change option for specific user

How can I disable the password change option for specific user in Wordpress?
I have a user where I set a new password each week for multiple users. But some tried to change the password for this user. I don't want that - but only for this specific user profile. All the other users should be able toi change their password.
I have tried different plugins but none of them work.
Thanks a lot if you can help on this!
Add this in your function.php file
class Password_Reset_Removed
{
function __construct()
{
add_filter( 'show_password_fields', array( $this, 'disable' ) );
add_filter( 'allow_password_reset', array( $this, 'disable' ) );
}
function disable()
{
if ( is_admin() ) {
$userdata = wp_get_current_user();
$user = new WP_User($userdata->ID);
if ( !empty( $user->roles ) && is_array( $user->roles ) && $user->roles[0] == 'administrator' )
return true;
}
return false;
}
}
$pass_reset_removed = new Password_Reset_Removed();
We have just removed the fields to change password from the back-end of the WordPress. Now, some users will also try to reset the password using the Lost your password? form from the log-in page.
In order to prevent them from doing that, we will remove lost password link and disable the lost password form by adding below code
function remove_lost_your_password($text)
{
return str_replace( array('Lost your password?', 'Lost your password'), '', trim($text, '?') );
}
add_filter( 'gettext', 'remove_lost_your_password' );
function disable_reset_lost_password()
{
return false;
}
add_filter( 'allow_password_reset', 'disable_reset_lost_password');
Note - you can update userid - as per your requirements

Woocommerce products editable by their author for specific user role

I have different users registered on my WordPress website with different roles. Apart from the rest of users, I want to allow advertisers (users with advertiser role - advertiser is a custom role that I have created) to place their own products on my site and also manage them. But they need to be limited only to manage (create, edit and delete) their own products, not of others.
So far, I have tried the following code but it seems to be not valid. I am sure I can accomplish my goal using pre_get_posts action and the following function can help me but I need some help in resolving the issues with this code. I am not sure about the post type of products.
Here is the code that I am trying to accomplish my goal with:
function show_specific_advertiser_products( $query ) {
$current_user = wp_get_current_user();
if ( is_admin() && in_array ($query->get( 'post_type'), array( 'woocommerce_products' ) ) && !user_can( $current_user, 'administrator' ) ) {
$query->set( 'author__in', $current_user->ID );
}
}
add_action( 'pre_get_posts', 'show_specific_advertiser_products' );
Any help will highly be appreciated.
The error in your code comes from the post_type… for woocommerce products it's simply product. You will have to replace administrator by your custom user role.
So try the following instead:
add_action( 'pre_get_posts', 'show_specific_advertiser_products' );
function show_specific_advertiser_products( $query ) {
$user = wp_get_current_user();
if ( is_admin() && $query->get( 'post_type') === 'product' && in_array('administrator', $user->roles) ) {
$query->set( 'author', $user->ID );
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.

How to Show Different Menus to Different User Roles in Woocommerce

I want to hide Downloads from Woocommerce menu when user type is Artist.
Add this code to your theme's 'functions.php'.
function custom_my_account_menu_items( $items ) {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
if($role[0]=="artist") // change role name if different
unset($items['downloads']);
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );

Custom Redirection of A Page On User Role

I am working with WooCommerce and in the website, there are three specific users. i) Admin ii) Vendor iii) Customer. So what I want is to redirect to a specific page, when the user role is Vendor. So I am hoping there would be a way to do it, something like the below:
function vendor_dashboard_redirect() {
if (condition) {
redirect("To The Default WordPress Dashboard");
}
}
add_action('template_redirect', 'vendor_dashboard_redirect');
I am expecting there would be an appropriate way to do it and stuck with it for a while.
This shoud work. Change the $vendor_role variable to your custom role identifier:
function vendor_dashboard_redirect() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = $user->roles;
$vendor_role = 'vendor';
if ( in_array( $vendor_role, $roles ) === true ) {
wp_redirect( admin_url('/') );
exit;
}
}
}
add_action('template_redirect', 'vendor_dashboard_redirect');

woocommerce conditional products/categories on user role

Looking for a plugin that helps me to restrict woocommerce products or product categories based on role.
Let's say that I only want to sell bulk products to whole sale buyers.
Any help is awesome, thanks!
Here is how I managed to hide products based on role:
First, I have added a checkbox in the product options inventory section to enable admins to hide the products based on their selection:
add_action( 'woocommerce_product_options_stock_status', 'hide_if_available_to_user_role' );
function hide_if_available_to_user_role(){
woocommerce_wp_checkbox( array( 'id' => '_hide_from_users', 'wrapper_class' => 'show_if_simple show_if_variable', 'label' => __( 'Hide this product from specific roles?', 'customhideplugin' ) ) );
}
I then saved this selection in the actual post when a post is updated.
add_action( 'woocommerce_process_product_meta', 'hide_save_product_meta' );
function hide_save_product_meta( $post_id ){
if( isset( $_POST['_hide_from_users'] ) ) {
update_post_meta( $post_id, '_hide_from_users', 'yes' );
} else {
delete_post_meta( $post_id, '_hide_from_users' );
}
}
This is how I got current user's role.
function getCurrentUserRole( $user = null ) {
$user = $user ? new WP_User( $user ) : wp_get_current_user();
return $user->roles ? $user->roles[0] : false;
}
Now query products. If the current user role matches the roles below, show the products as usual.
Otherwise, set the query based on the code above...
add_action( 'woocommerce_product_query', 'hide_product_query' );
function hide_product_query( $q ){
if((getCurrentUserRole() == 'editor' ) || (getCurrentUserRole() == 'administrator' )){
return false;
} else {
$meta_query = $q->get( 'meta_query' );
if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'no' ) {
$meta_query[] = array(
'key' => '_hide_from_users',
'compare' => 'NOT EXISTS'
);
}
$q->set( 'meta_query', $meta_query );
}
}
To achieve this you can use the Free Groups plugin. But for that you must add all the wholesalers to one group say wholesale group 1. Then while editing any product you get an option to access to, add the wholesaler group 1 there. The product will now be only seen by the user who is in wholesalers group 1.
I tried a few different plugins to try to achieve that. I finally chose this one because it's easy to understand and can show/hide depending on procucts, tags, categories and custom taxonomies. WooCommerce Products Visibility

Resources