Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #530393

    Hi everyone,
    I need to hide the cart dropdown for non logged users and show it only for specific user roles?
    is there a workaround or snippet for this?

    Kind Regards,
    Chris

    #530626

    Hi chris1986!

    Thank you for using Enfold.

    Add this in the functions.php file:

    add_action( 'init', 'avia_woocommerce_cart_placement', 10);
    function avia_woocommerce_cart_placement()
    {
    	$position 	= avia_get_option('header_position',  'header_top') == "header_top" ? "ava_main_header" : "ava_inside_main_menu";
    	if(avia_get_option('cart_icon') == "always_display_menu")
    	{
    		$position = "ava_inside_main_menu";
    		if( strpos( avia_get_option('header_layout'), 'bottom_nav_header') !== false && avia_get_option('header_position') == 'header_top')
    		{
    			$position = "ava_before_bottom_main_menu";
    		}
    	}
    
    	if ( is_user_logged_in() ) {
    		add_action( $position, 'avia_woocommerce_cart_dropdown', 10);
    	}
    }

    If you want to display the cart for specific user role change the conditional function is_user_logged_in() to current_user_can(): https://codex.wordpress.org/Function_Reference/current_user_can

    Cheers!
    Ismael

    #530656

    Made my day!
    Thanks a lot Ismael =) it works .

    For everyone who needs to setup the cart with more than one role:

    add_action( ‘init’, ‘avia_woocommerce_cart_placement’, 10);
    function avia_woocommerce_cart_placement()
    {
    $position = avia_get_option(‘header_position’, ‘header_top’) == “header_top” ? “ava_main_header” : “ava_inside_main_menu”;
    if(avia_get_option(‘cart_icon’) == “always_display_menu”)
    {
    $position = “ava_inside_main_menu”;
    if( strpos( avia_get_option(‘header_layout’), ‘bottom_nav_header’) !== false && avia_get_option(‘header_position’) == ‘header_top’)
    {
    $position = “ava_before_bottom_main_menu”;
    }
    }

    $user = wp_get_current_user();
    $allowed_roles = array(‘your_custom_role1’, ‘your_custom_role2’, ‘your_custom_role3’);
    if( array_intersect($allowed_roles, $user->roles ) ) {add_action( $position, ‘avia_woocommerce_cart_dropdown’, 10);}
    }

    Have a nice day.

    Kind Regards,
    Chris

    #531326

    Hi,

    Glad we could help and thanks for sharing the solution :-)

    Regards,
    Rikard

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.