Jump to content
Marco Ro

Get user in a module give me this error: isLoggedin() on a non-object

Recommended Posts

Hi everybody,
I have to change the price of some products based on the type of role the user has, if he is logged in.

For do this I use a module, I can change the price based of any fields in the products page but when I try to access to $user data give me an error, like this: isLoggedin() on a non-object or if I use $user->hasRole I give the same error hasRole on a non-object. I think the problem is that the module can't know which user is logged in, but I can't find a solution.

Can someone help me to understand how I can access to this data to change the price?

Thank you

Share this post


Link to post
Share on other sites

Thank you @Robin S, now work well. Thanks 🙂

I copy here some part of the documentation, if someone need:

Quote

If you’ve seen other API access methods like $this->pages or $this->wire('pages'); those are access methods that you would use from within a PHP class. Meaning, if you see $this as part of the access method, you can ignore it for now because template files are not PHP classes. Only when you get into module development are you likely to use access methods that start with $this.

Accessing API variables from within modules or classes

You cannot access API variables directly in a class (i.e. $pages), because they are out of scope. You can use the API functions like pages() or wire(‘pages’), as they are in scope, but we recommend a different strategy…

When you are in a Wire-derived class (such as a ProcessWire module), you should preferably access API variables from $this->var (like $this->pages). That’s because ProcessWire injects dependencies into every Wire-derived object, which ties it to the current instance. Modules are more likely to be shared and thus more likely to end up in a multi-instance environment. Accessing your API variables from $this->var ensures no ambiguity about what instance they getting pulled from.

While $this->var works, in most cases we recommend going a step further and instead using $this->wire(‘var’); because it’s slightly more efficient, and sightly safer. That’s because it prevents the possibility of name collisions between API variables and your own class variables. Though if you are being reasonably careful, it doesn’t really matter whether you use $this->var or $this->wire(‘var’); But I like using $this->wire(‘var’) because it’s a little more efficient.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...