• Resolved ryanga1

    (@ryanga1)


    Hello –

    Thank you for the very helpful plugin. I’m looking for some rather specific functionality and was hoping you could help with a quick code modification or help point me in the right direction. I’m trying to implement custom pricing for a simple affiliate system.

    It seems like the current plug in allows you to set prices for:
    Role A, Role B, … and “Not Logged In”

    I am adding users via code to a custom Role based on an entry link. For example, if someone visits domain.com/role1, they are added to role1. This user will not be logged in, but will be in a certain (custom) role. The plug in does not currently set the pricing for this role (since the user is not logged in).

    Can you help with the code modification I need to make to check for a role and assign that role pricing, even if a user is not logged in?

    Thanks again!

    https://www.ads-software.com/plugins/woocommerce-role-based-price/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter ryanga1

    (@ryanga1)

    Thanks for the quick reply. I think I may have solved the problem (and probably created some unwanted side effects that I haven’t discovered yet)… I discovered that:

    global $current_user;
    $current_user->roles; // only contains roles of logged in users

    But…

    $user = new WP_User(get_current_user_id());  // $user->roles returns roles of the current user, regardless of logged in status (such as roles assigned with $user->add_role( 'customrole' ))

    So, I changed your code a bit to:

    public function current_role(){
    
       $user_ID= get_current_user_id();   // get the current user_id
       $user = new WP_User( $user_ID );
       $user_role = array_pop($user->roles);
    
       //global $current_user;
       //$user_roles = $current_user->roles;
       //$user_role = array_shift($user_roles);
       if($user_role == null){
         return 'logedout';
       }
       return $user_role;
    }

    And now my custom roles are reflected with the custom role based pricing, regardless of the logged in status of the user. Please let me know if there is something I’ve overlooked (just learning WP coding), and thanks again!

    Plugin Author Varun Sridharan

    (@varunms)

    Hi,

    Nice to here that you found the solution . but note that you never modify the core of wordpress or the core of any plugin INC of my plugins too. because when you updated the modified plugin. the modified code will be removed and you website may not work. after updated and also make sure that even if you modify. the plugin function’s correctly ??

    Thread Starter ryanga1

    (@ryanga1)

    Thanks for the help – am I correct that to do this properly, I should request a hook in the current_role function, and then write a custom filter for that function? If so, is that something I can request in the next version? ??

    I appreciate your feedback.

    Plugin Author Varun Sridharan

    (@varunms)

    Yes you are right.. need to modify the plugin via hook only not via. direct edit. sure let me provide a hook in next version ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Role Based Pricing even if not logged in’ is closed to new replies.