raunak-gupta
10/11/2016 - 6:57 AM

WordPress backend for WooCommerce customer user role. This will allow customer user role to access to wordpress backend and to publish/edit

WordPress backend for WooCommerce customer user role. This will allow customer user role to access to wordpress backend and to publish/edit posts (with this last feature you should take care as customer user role will have the ability to add/edit/publish posts, so backup your database before).

<?php
add_filter('woocommerce_disable_admin_bar', '_wc_disable_admin_bar', 10, 1);
add_filter('woocommerce_prevent_admin_access', '_wc_prevent_admin_access', 10, 1);
function _wc_prevent_admin_access($prevent_admin_access) {
    $user_data = get_userdata( get_current_user_id() );
    $user_roles = $user_data->roles;
    $customer_role = get_role( 'customer' );

    // For "customer" WooCommerce user role only
    if (in_array('customer', $user_roles)) {

        // Warning! with this (This will be definitive, so make a database backup)
        // Adding 'add_post', 'publish_posts' and 'edit_post' capabilities to customer user role
        if ( !user_can( get_current_user_id(), 'publish_posts' ) ){
            $customer_role->add_cap( 'create_posts' );
            $customer_role->add_cap( 'publish_posts' );
            $customer_role->add_cap( 'edit_posts' );
        }

        // Giving access to wordpress backend
        return false;
    }
}