Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.6.2]: Calling unknown method: craft\elements\User::getPrimaryBillingAddress() #3546

Closed
lukasNo1 opened this issue Jun 17, 2024 · 7 comments
Assignees
Labels
bug commerce4 Issues related to Commerce v4 Craft Commerce

Comments

@lukasNo1
Copy link

What happened?

Description

I just updated from 4.6.1 to 4.6.2

I have this code in my template which doesn't work anymore now.

{% set primaryBillingAddress = currentUser ? currentUser.getPrimaryBillingAddress() : null %}

Throwing the following error:
Calling unknown method: craft\elements\User::getPrimaryBillingAddress()

Craft CMS version

4.10.0

Craft Commerce version

4.6.2

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@lukasNo1 lukasNo1 added bug commerce4 Issues related to Commerce v4 Craft Commerce labels Jun 17, 2024
@nfourtythree
Copy link
Contributor

Hi @lukasNo1

Can you let us know which other plugins you have installed in your project. We have seen this issue before when not all plugins in a project are using Craft's onInit() method in their plugin's init() class.

Thanks..

@lukasNo1
Copy link
Author

Hey @nfourtythree

Thanks for the help. I just checked the project and the code that seems to be the problem is inside a module:

    Event::on(
        Plugins::class,
        Plugins::EVENT_AFTER_LOAD_PLUGINS,
        function () {
            $user = \Craft::$app->getUser()->getIdentity();
              ....
          }
    );

I need to check the user as early as possible, because we have custom redirects for every user.
If i remove the $user line, the currentUser.getPrimaryBillingAddress() works. Putting it in onInit instead produces the same error.

I tested putting the code in some other Event like CraftVariable::EVENT_INIT which apparently works.

Do you know if this is something you can fix, or is it just "forbidden :)" to query the user onInit or after_load_plugins

@nfourtythree
Copy link
Contributor

Hi @lukasNo1

Thank you for your detailed response, that is appreciated to understand what is happening.

Before I get to a potential solution it is worth noting that the Plugins::EVENT_AFTER_LOAD_PLUGINS is called before all the onInit() calls so this currently wouldn't help you due to the changes in Craft and Craft Commerce.

As for how we can solve what is going on, it might be good to understand what you are trying to achieve in your module's init method. There may be some more specific events or other places available to you which solve this issue.

If it is easier, you can send your explanation along with your project files to support@craftcms.com and we can continue the conversation there, hopefully get to the bottom of what is happening and get your project back on its way.

Thanks!

@lukasNo1
Copy link
Author

Our usecase is this:
Every user has a default site which is saved in his profile. He is only allowed to access the frontend of this site. If he somehow accesses the website over another site (from google f.ex.) we want to redirect him to the correct site.

I can put the redirect code in CraftVariable::EVENT_INIT which works for now. Although its not really the right place for it.

@nfourtythree
Copy link
Contributor

Hi @lukasNo1

Thank you for your response.

After further internal discussion, we have revised the guidelines for when plugins and modules should use Craft::$app->onInit(). In Commerce, we had probably added too many things into that callback method that did not need to be in there.

We recommend that people use that function in their plugin/module when they have:

  • code that would create/execute an element query
  • code that would cause Twig to be initialized

We have just released Commerce 4.6.3.1 with the fix/update. This means you should be able to update your project and put your code inside your module's init() method in an onInit() call (because it looks like you are trying to retrieve data from the system) and therefore you will not need the after load plugins event.

Hope this helps, thank you for your help, feedback and patience.

Thanks!

@lukasNo1
Copy link
Author

Thank you!

I can confirm that my code now works in the onInit() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug commerce4 Issues related to Commerce v4 Craft Commerce
Projects
None yet
Development

No branches or pull requests

2 participants