简体   繁体   中英

Display a custom themed "my-account.php" woocommerce page based on user role

We've been building out a new digital dashboard for my client's upcoming launch of a new digital product, however we wish to only provide access to this newly skinned and structured WooCommerce dashboard (my-account template pages) IF the user has been manually provided a user-role, ie: beta_user, org_manager

Any users who do not have this custom role applied would be served the default WooCommerce plugin my-account pages.

Here is what we have tried...

Added this snippet to the Child Theme functions.php:

// allow access to new dashboard if user role is org manager 
function isOrgManager(){
    $currentUser = wp_get_current_user();
    return in_array('org_manager', $currentUser->roles);
}

Then applied this to the /child-theme/woocommerce/my-account.php file:

<?php if( isOrgManager() ){
        wc_get_template( '/wp-content/themes/blankchildhub/woocommerce/my-account.php' ); 
    } else {        
    wc_get_template( 'myaccount/my-account.php' );
}?>

Any insight would be greatly appreciated as I know this is a fairly common challenge for custom WP and WooCommerce managers.

First remove the old file /child-theme/woocommerce/my-account.php

and then copy /plugins/woocommerce/templates/my-account/my-account.php

to /child-theme/woocommerce/my-account.php

then add a switch/ if statement something like below:

<?php

/*** filename /child-theme/woocommerce/my-account.php ***/

/**
 * My Account page
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/my-account.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 3.5.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * My Account navigation.
 *
 * @since 2.6.0
 */


// allow access to new dashboard if user role is org manager 
function isOrgManager(){
    $currentUser = wp_get_current_user();
    return in_array('org_manager', $currentUser->roles);
}

<?php

if( isOrgManager() ):

// your custom code....

else:      

do_action( 'woocommerce_account_navigation' ); ?>

<div class="woocommerce-MyAccount-content">
    <?php
        /**
         * My Account content.
         *
         * @since 2.6.0
         */
        do_action( 'woocommerce_account_content' );
    ?>
</div>

<?php

  endif;

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