I have the following a function in my functions.php:
function admin_style()
{ global $user_ID;
if ( current_user_can( 'shop_manager') )
{ wp_enqueue_style('admin-styles', '/wp-content/themes/electa-child/admin.css');
}}
add_action('admin_enqueue_scripts', 'admin_style');
I would like to add another user role but when I do if ( current_user_can( 'shop_manager', 'shop_assistant') ) I am getting an error.
How should I add another user role to be checked upon?
Thanks in advance,
Please try
if( current_user_can('shop_manager') || current_user_can('shop_assistant') )
You can get you current user role (translated) like this.
then you can play around with it.
/**
* Returns the translated role of the current user.
* No role, get false.
*
* #return string The translated name of the current role.
**/
function get_current_user_role() {
global $wp_roles;
$current_user = wp_get_current_user();
$roles = $current_user->roles;
$role = array_shift( $roles );
return isset( $wp_roles->role_names[ $role ] ) ? translate_user_role( $wp_roles->role_names[ $role ] ) : FALSE;
}
Related
Having failed at adding a suffix based on User Role because it wont display the Woo price display shortcode, I'm now approach the problem from the other direction - I have added the suffix to Woos tax tab, and now instead want to remove the suffix (from all user roles except one).
I found this code on github to remove the suffix from products:
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 );
function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) {
if ( ! $product->is_taxable() ) {
return '';
}
return $price_display_suffix;
}
and I have modified it to hide the suffix from certain user types
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 );
function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) {
// check current user role
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
if ( in_array( 'administrator', $roles ) ) {
$price = $your_suffix;
} elseif ( in_array( 'default_wholesaler', $roles ) ) {
$price = '$your_suffix';
return '';
}
return $price_display_suffix;
}
This worked, however I had to switch the users (I want Admin and Wholesalers to see the suffix) and put in Customers, etc.
The problem is that guest customers still see the suffix.
Could someone suggest a way of hiding the suffix from anyone except the logged in 'default-wholesaler' user and 'Administrator'?
Thanks!
You could use the following for this.
This works for the administrator role! you can add the other role yourself as an 'exercise'
function custom_woocommerce_get_price_suffix( $html, $product, $price, $qty ) {
// check current user role
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
// if NOT in array user roles
if ( !in_array( 'administrator', $roles ) ) {
$html = '';
}
return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 4 );
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' );
I'm building a wordpress ecommerce site with the woocommerce plugin, it turns that when an user gets logged in and add products to his cart, but the user don't want to proceed to the checkout process, the user prefers to logout and continue with the checkout process later... when the user comes back and gets logged in again the cart is empty.
What is going on here?.
Is this a normal behavior of woocommerce?.
Do I have to do something else? maybe a plugin?
Thanks.
I thought that the cart is emptied when a user logs out, and I finally tracked it down.
On wp_logout() WordPress runs wp_clear_auth_cookie() function. wp_clear_auth_cookie() triggers the do_action( 'clear_auth_cookie' ); action hook.
WooCommerce's Session handler class then runs it's destroy method on this hook.
add_action( 'clear_auth_cookie', array( $this, 'destroy_session' ) );
The destroy_session() method then calls the wc_empty_cart() function, which is a wrapper for the cart class's empty_cart() method.
WC()->cart->empty_cart( false );
But the key thing here is that the parameter is false. Because when we finally track down the empty_cart() method we see that the default is true.
/**
* Empties the cart and optionally the persistent cart too.
*
* #access public
* #param bool $clear_persistent_cart (default: true)
* #return void
*/
public function empty_cart( $clear_persistent_cart = true ) {
$this->cart_contents = array();
$this->reset();
unset( WC()->session->order_awaiting_payment, WC()->session->applied_coupons, WC()->session->coupon_discount_amounts, WC()->session->cart );
if ( $clear_persistent_cart && get_current_user_id() ) {
$this->persistent_cart_destroy();
}
do_action( 'woocommerce_cart_emptied' );
}
When passing true the persistant_cart_destroy() method is called and it is this method that deletes the meta data where the user's cart is kept.
/**
* Delete the persistent cart permanently.
*
* #access public
* #return void
*/
public function persistent_cart_destroy() {
delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart' );
}
SO, all of that is to say that I do not think the cart should be emptied when a user logs out and then back in. A little more evidence is that WooCommerce attempts to load the persistent cart as soon as a user logs back in.
/**
* Load the cart upon login
*
* #param mixed $user_login
* #param integer $user
* #return void
*/
function wc_load_persistent_cart( $user_login, $user = 0 ) {
if ( ! $user )
return;
$saved_cart = get_user_meta( $user->ID, '_woocommerce_persistent_cart', true );
if ( $saved_cart )
if ( empty( WC()->session->cart ) || ! is_array( WC()->session->cart ) || sizeof( WC()->session->cart ) == 0 )
WC()->session->cart = $saved_cart['cart'];
}
add_action( 'wp_login', 'wc_load_persistent_cart', 1, 2 );
I would try disabling all other plugins to see if the behavior reverts back to what I think is the normal behavior. From there, you can re-enable them one at a time to isolate the culprit.
I faced same problem and solved it by placing following function to my functions.php file:
/**
* Load the persistent cart if exists
*
* #return void
*/
function adian_load_persistent_cart(){
global $current_user;
if( ! $current_user )
return false;
$saved_cart = get_user_meta( $current_user->ID, '_woocommerce_persistent_cart', true );
if ( $saved_cart ){
if ( empty( WC()->session->cart ) || ! is_array( WC()->session->cart ) || sizeof( WC()->session->cart ) == 0 ){
WC()->session->set('cart', $saved_cart['cart'] );
}
}
}
add_action( 'init', 'adian_load_persistent_cart', 10, 1 );
Function checks if the user has persistent cart saved and replaces session cart with it.
Is there a way to display an extra text next to the Username if the person has the administrator role?
I just want to display the extra text for this role only, what hook could I use for that?
Yes, you can use the filter the_author, like so:
add_filter( 'the_author', 'author_admin_so_14367149' );
function author_admin_so_14367149( $display_name )
{
// Block the filter in the dashboard (admin area)
if( is_admin() )
return $display_name;
global $authordata;
// User has administrator role, add string to display name
if( in_array( 'administrator', $authordata->roles ) )
$display_name = $display_name . ' (admin)';
return $display_name;
}
For the comments section, there's another filter and the checks are different:
add_filter( 'get_comment_author', 'comment_author_admin_so_14367149' );
function comment_author_admin_so_14367149( $author )
{
// Block the filter in the dashboard (admin area)
if( is_admin() )
return $author;
$user = get_user_by( 'login', $author );
// User has administrator role, add string to display name
if( in_array( 'administrator', $user->roles ) )
$author = $author . ' (admin)';
return $author;
}
I would like to know how can I modify the following code to check if the current user has a site/blog already set up on my wordpress network??
<?php
global $wp_roles;
foreach ( $wp_roles->role_names as $role => $name ) :
if ( current_user_can( $role ) && $role == 'administrator') {
if ( class_exists( 'MarketpressFadminWidget' ) ) {
$mp_frontend_settings = get_option( 'mp_frontend_settings' );
$mp_frontend_settings = unserialize($mp_frontend_settings);
$adminpage = $mp_frontend_settings['page_id'];
$permalink = get_permalink($adminpage);
?>
// My Links / Html Content
<?php
}
Use the function get_blogs_of_user.
Description
Returns an array of objects containing the details of each blog the specified user has access to.