简体   繁体   English

在 WooCommerce 我的帐户页面上,根据特定用户角色显示我的地址部分

[英]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.在 WooCommerce 我的帐户页面上,我试图根据用户角色隐藏几个部分。

At the moment, all people who register directly with the WooCommerce registration form are assigned user role 'Customer'.目前,所有直接使用 WooCommerce 注册表注册的人都被分配了用户角色“客户”。 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:在您的functions.php文件中添加这个函数,以便您可以重用它:

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 ).woocommerce > templates > myaccount my-account.php模板,然后复制到您主题的 WooCommerce 目录( YOURTHEME > woocommerce > myaccount )。

From there go to line 36. THIS is where the address gets loaded in.从那里转到第 36 行。这是地址被加载的地方。

Wrap the address with a PHP if statement like so:用 PHP if 语句包裹地址,如下所示:

<?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.您需要覆盖主题中的my-account.php模板,然后在某些条件逻辑中包装对地址模板的调用。 Specificallycurrent_user_can() which checks for WordPress capabilities.特别是current_user_can()用于检查 WordPress 功能。

<?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')理想情况下,您将根据 Employer 角色具有而 Customer 角色没有的功能来执行此操作,但在最坏的情况下,您可以使用角色名称 à la current_user_can('employer')

Update 2021-02-16更新 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.鉴于my-account.php的重组不再是理想的修改模板,我相信您可以通过钩子/过滤器完全删除部分而无需覆盖模板。

5.0 is out right now, and I would probably filter woocommerce_account_menu_items to remove items from the account menu navigation. 5.0 现已发布,我可能会过滤woocommerce_account_menu_items以从帐户菜单导航中删除项目。 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' );然后出于安全目的也从端点删除回调......作为一个例子,这将地址内容添加到地址端点: 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.因此,要更新我的示例,如果您想完全删除某些用户的“编辑地址”选项卡,您可以使用以下代码段来 1. 从“我的帐户”导航中删除该项目,以及 2. 完全禁用该端点。

/**
 * 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' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce:仅针对特定用户角色显示我的帐户链接 - WooCommerce: Display My Account link only for specific user role 根据用户角色显示自定义主题“my-account.php”woocommerce 页面 - Display a custom themed "my-account.php" woocommerce page based on user role 在WooCommerce的“我的帐户”仪表板上显示用户角色名称 - Display User Role Name on My Account Dashboard in WooCommerce 仅针对特定用户角色在 Woocommerce 中自定义我的帐户端点 - Custom my account endpoint in Woocommerce just for a specific user role 在 WooCommerce 中添加基于用户角色的自定义我的帐户菜单项 3+ - Add custom my account menu item based on user role in WooCommerce 3+ 有条件地在woocommerce我的帐户页面上显示一个标签 - Display a Tab on the woocommerce my account page conditionally 在 Woocommerce 我的帐户下载部分显示产品图像 - Display product image on Woocommerce My Account Downloads section 根据用户角色更改 Woocommerce 我的帐户选项卡名称 - Change Woocommerce My Account Tab name depending on User Role WooCommerce - 我的帐户页面添加锚链接到注册部分 - WooCommerce - My account Page add anchor link to the REGISTER section 将内容添加到 WooCommerce 我的帐户仪表板主页部分 - Add content to WooCommerce My account dashboard main page section
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM