简体   繁体   中英

On WooCommerce My account page, display My Addresses section based on specific user role

On the WooCommerce My Account page I am trying to hide a couple of the sections based on the user role.

At the moment, all people who register directly with the WooCommerce registration form are assigned user role 'Customer'. However, only users with role 'Employer' are actually able to make purchases... so effectively I want to hide the My Addresses section to users who are 'Customers'.

Any ideas if I can do this with a function? Miro

This is possible easily with templates. Add this function in your functions.php file so you can reuse it:

function isEmployer(){
    $currentUser = wp_get_current_user();
    return in_array('employer', $currentUser->roles);
}

Grab the my-account.php template from woocommerce > templates > myaccount and copy over to your theme's WooCommerce directory ( YOURTHEME > woocommerce > myaccount ).

From there go to line 36. THIS is where the address gets loaded in.

Wrap the address with a PHP if statement like so:

<?php if( isEmployer() ){
        wc_get_template( 'myaccount/my-address.php' ) 
    }?>

You would need to override the my-account.php template in your theme and then wrap the call to the address template in some conditional logic. Specificallycurrent_user_can() which checks for WordPress capabilities.

<?php 
if( current_user_can( 'place_order' ) ){
   wc_get_template( 'myaccount/my-address.php' ); 
} ?>

Ideally, you would do this based on a capability that the Employer role has that the Customer role does not, but in the worst case you could use the role name à la current_user_can('employer')

Update 2021-02-16

Given the restructuring of my-account.php that is no longer the ideal template to modify and I believe you could remove sections entirely via hooks/filters without overriding a template.

5.0 is out right now, and I would probably filter woocommerce_account_menu_items to remove items from the account menu navigation. And then for security purposes remove the callback from the endpoint too... As an example this adds the address content to the address endpoint: add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_edit_address' );

So to update my example, if you want to completely remove the Edit Addresses tab for certain users, you could use the following snippet to 1. remove the item from the My Account navigation and 2. completely disable that endpoint.

/**
 * Conditionally remove address menu item from My Account.
 *
 * @param array $items the My Account menu items
 * @return array
 */
function so_31342804_remove_address_from_my_account_menu( $items ) {

    // Remove menu item for users without certain capability.
    if( ! current_user_can( 'place_order' ) ) {
        unset( $items['edit-address'] );
    }

    return $items;
}
add_filter( 'woocommerce_account_menu_items', 'so_31342804_remove_address_from_my_account_menu' );


/**
 * Conditionally remove address endpoint from My Account area.
 *
 * @param array $items the My Account menu items
 * @return array
 */
function so_31342804_remove_address_endpoint( $endpoints ) {

    // Remove endpoint content for users without certain capability.
    if( ! current_user_can( 'place_order' ) ) {
        unset( $endpoints['edit-address'] );
    }

    return $endpoints;
}
add_filter( 'woocommerce_get_query_vars', 'so_31342804_remove_address_endpoint' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM