【问题标题】:On WooCommerce My account page, display My Addresses section based on specific user role在 WooCommerce 我的帐户页面上,根据特定用户角色显示我的地址部分
【发布时间】:2015-07-10 14:03:31
【问题描述】:

在 WooCommerce 我的帐户页面上,我试图根据用户角色隐藏几个部分。

目前,所有直接使用 WooCommerce 注册表单注册的人都被分配了用户角色“客户”。但是,实际上只有具有“雇主”角色的用户才能进行购买……所以我想将“我的地址”部分隐藏给“客户”用户。

如果我可以用函数来做到这一点,有什么想法吗? 米罗

【问题讨论】:

    标签: php function wordpress woocommerce


    【解决方案1】:

    这可以通过模板轻松实现。 在您的functions.php 文件中添加此函数,以便您可以重复使用它:

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

    woocommerce > templates > myaccount 获取my-account.php 模板并复制到您主题的 WooCommerce 目录 (YOURTHEME > woocommerce > myaccount)。

    从那里转到第 36 行。这是加载地址的地方。

    使用 PHP if 语句包装地址,如下所示:

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

    【讨论】:

    • 谢谢。是否可以仅通过函数文件执行此操作?我问是因为我需要将相同的场景应用于我的帐户页面上的几个部分......并且不想丢失任何主题更新......如果我复制文件,我会这样做。
    • 如果您使用的是子主题(您应该使用),那么如果在您的主题模板文件夹中完成 Woo 模板覆盖,您将不会丢失更新。我不确定函数文件是否可行,因为它实际上是需要隐藏的模板部分,它们不是钩子。
    • 好的,我明白了。我正在使用子主题,但我也在使用 woo commerce 订阅插件,这意味着我也需要复制该模板文件......这意味着对该模板文件的任何更新都将保持“未更新”。
    • 这仅在用户完全是雇主时才有效。如果他们更高呢?喜欢管理员?
    • &lt;?php if( isEmployer() || current_user_can('administrator') ) { wc_get_template( 'myaccount/my-address.php' ) }?&gt;
    【解决方案2】:

    您需要覆盖主题中的my-account.php 模板,然后在某些条件逻辑中包装对地址模板的调用。特别是 current_user_can() 检查 WordPress 功能。

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

    理想情况下,您可以根据 Employer 角色具有 Customer 角色所没有的能力来执行此操作,但在最坏的情况下,您可以使用角色名称 la current_user_can('employer')

    2021 年 2 月 16 日更新

    鉴于my-account.php 的重组不再是修改的理想模板,我相信您可以通过钩子/过滤器完全删除部分,而无需覆盖模板。

    5.0 现已推出,我可能会过滤 woocommerce_account_menu_items 以从帐户菜单导航中删除项目。然后出于安全目的,也从端点中删除回调...作为示例,这会将地址内容添加到地址端点:add_action( 'woocommerce_account_edit-address_endpoint', 'woocommerce_account_edit_address' );

    所以要更新我的示例,如果您想完全删除某些用户的“编辑地址”选项卡,您可以使用以下 sn-p 来 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' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多