craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
215 stars 169 forks source link

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

Closed lukasNo1 closed 2 weeks ago

lukasNo1 commented 2 weeks ago

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

-

linear[bot] commented 2 weeks ago

PT-1840 [4.6.2]: Calling unknown method: craft\elements\User::getPrimaryBillingAddress()

nfourtythree commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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:

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 commented 2 weeks ago

Thank you!

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