Foxy Forum Status

We're no longer responding to questions via our forum, but we will keep it up for historical reasons. If you can't find the answer you're looking for, please visit our knowledge base or contact us. If there's enough interest in the future, we may bring the forum back.

FoxyShop and User Roles

dhindmandhindman Member
in Help edited February 2014
Hello all,

I'm looking for some guidance on updating user roles in WP using the FoxyShop plugin. By default, new users are given the role of 'subscriber' by this plugin. Existing user roles are not touched. I want to change that so that the default is 'total_access', a custom role I've added. I also want to do this in my functions.php file so I can update FoxyShop without overwriting.

I see in datafeedfuntions.php some hooks(?) for this:

//Run Your Custom Actions Here with add_action()
do_action("foxyshop_datafeed_add_wp_user", $xml, $user_id);

and
//Run Your Custom Actions Here with add_action()
do_action("foxyshop_datafeed_update_wp_user", $xml, $current_user->ID);

So I've been playing in my sandbox account and have this in my functions.php file:

function foxycart_add_role() {
update_user_meta($current_user->ID, 'role', 'total_access');
}
add_action ( 'foxyshop_datafeed_add_wp_user', 'foxycart_add_role' );


function foxycart_update_role() {
update_user_meta($current_user->ID, 'role', 'total_access');
}
add_action ( 'foxyshop_datafeed_update_wp_user', 'foxycart_update_role' );

Of course, this is not working. I did a test transaction and am refeeding it with every change to see if the role will update. No luck so far.

Bonus points: I'd REALLY like to find a way to change the role based on the product category, as I plan on having a 'free_access' role later on. But that may be beyond this initial question.

Thanks so much in advance!
Tagged:
Comments
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    I think you are wanting to do wp_update_user() and set the the role that way: http://codex.wordpress.org/Function_Reference/wp_update_user

    As far as line 195 of datafeedfunctions.php, if you wrap this "subscriber" text with an apply filter like:

    'role' => apply_filters('foxyshop_default_user_role', 'subscriber'),

    I will add this to the core so it will come out with the next version.
  • Thanks. I think either wp_update_user() or update_user_meta() will work for me as I am only changing one thing: role. However, I can't get this to fire either way.

    Here are the two functions I created. Can you spot what I am doing wrong?


    // Change the Role of New Users to Total Access
    function foxycart_add_role( $xml, $user_id ) {
    update_user_meta ( $user_id, 'role', 'total_access' );
    }
    add_action ( 'foxyshop_datafeed_add_wp_user', 'foxycart_add_role', 10, 2 );


    // Change the Role of Existing Users to Total Access
    function foxycart_update_role( $xml, $current_user->ID ) {
    update_user_meta ( $current_user->ID, 'role', 'total_access' );
    }
    add_action ( 'foxyshop_datafeed_update_wp_user', 'foxycart_update_role', 10, 2 );
  • I haven't been able to make any progress here. Is there something wrong in my php code? When I refeed an order that should apply to this, nothing happens.
  • sparkwebsparkweb Member, Integration Developer, FoxyShop, Order Desk
    Just to be clear:
    update_user_meta ( $current_user->ID, 'role', 'total_access' );
    
    will not update the role. To update the role, you have to update the user as notated here: http://codex.wordpress.org/Function_Reference/wp_update_user

    I'd recommend doing the dev outside of the datafeed for now to make sure you can get it working by passing in a constant ID (like 1) and getting the role updated. Once you have a prototype working, then get it inserted into the datafeed.
Sign In or Register to comment.