繁体   English   中英

有条件地在woocommerce我的帐户页面上显示一个标签

[英]Display a Tab on the woocommerce my account page conditionally

您好,我能够在woocommerce我的帐户页面上创建自定义端点:

    function my_custom_my_account_menu_items( $items ) {
    $items = array(
        'dashboard'         => __( 'Dashboard', 'woocommerce' ),
        'orders'            => __( 'Orders', 'woocommerce' ),
        //'downloads'       => __( 'Downloads', 'woocommerce' ),
        //'edit-address'    => __( 'Addresses', 'woocommerce' ),
        //'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
        'edit-account'      => __( 'Edit Account', 'woocommerce' ),
        'special-page'      => 'Special Page',
        'customer-logout'   => __( 'Logout', 'woocommerce' ),
    );

    return $items;
}

add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );

我访问了以下URL: https : //github.com/woocommerce/woocommerce/wiki/2.6-Tabbed-My-Account-page WooCommerce:将端点分配给我的帐户页面中的自定义模板

现在,我想知道如何根据用户角色有条件地显示选项卡? 我只想为供应商显示“特殊页面”选项卡。

if ( ! current_user_can( 'vendor' ) ) {}

任何想法如何实现这一目标?

尼古拉斯

您可以尝试以下代码,但是要工作,需要在定义current_user时调用此部分。

add_action('plugin_loaded', 'vendor_menu_items');

function vendor_menu_items(){
    if ( ! current_user_can( 'vendor' ) ) {
    add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
    }
} 

您可以在add_filter语句周围或函数主体内部添加条件用户角色。 在这种情况下,这并不重要,因为这是一种快速检查。 这是用于将端点添加到woocommerce我的帐户页面的完整代码:

// either use this line if you're writing a plugin
register_activation_hook(__FILE__, 'flush_rewrite_rules');
// or use this line if you're writing a theme
add_action('switch_theme', 'flush_rewrite_rules');

add_action('init', 'my_add_rewrite_endpoints');
function my_add_rewrite_endpoints() {
    add_rewrite_endpoint('vendor', EP_ROOT | EP_PAGES);
}

add_filter('woocommerce_account_menu_items', 'my_my_account_menu_items');
function my_my_account_menu_items($menu_items) {
    // remove items this way
    unset($menu_items['downloads']);
    unset($menu_items['edit-address']);
    unset($menu_items['payment-methods']);

    // add items this way
    if(current_user_can('vendor')) {
        $menu_items['vendor'] = __('Your vendor page', 'your-plugin-domain-name');
    }

    return $menu_items;
}

// note the key you used for $menu_items in the previous function must be in the name of this filter hook.
add_filter('woocommerce_account_vendor_endpoint', 'my_vendor_endpoint');
function my_vendor_endpoint() {
    echo '<p>Your page content here</p>';
}

暂无
暂无

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

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