Marco Ro Posted June 24, 2020 Share Posted June 24, 2020 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 Link to comment Share on other sites More sharing options...
Robin S Posted June 24, 2020 Share Posted June 24, 2020 2 hours ago, Marco Ro said: when I try to access to $user data give me an error $user is not in scope inside a module method. You would use $this->wire('user') in a Wire-derived class (i.e. in a PW module). Info: https://processwire.com/docs/start/api-access/ 1 1 Link to comment Share on other sites More sharing options...
Marco Ro Posted June 25, 2020 Author Share Posted June 25, 2020 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now