1

In PHP, is it possible to grab the customer model from a user model?

EDIT: I just noticed that it is possible to attach a customer info field to the user. How do we achieve this?

2

You shouldn't really need to do that, you're just adding custom fields to the user when those can be fetched easilly. A customer will always be a user, but a user might not be a customer.

If you intend to get the customer details from a plugin (with PHP) you can just get the customer details (if exist) from the user, by using the Commerce_CustomersService

code example would be

$customer = craft()->commerce_customers->getCustomerByUserId($user->id);

this will either return the customer model or null (if a customer is not created against the user).

| improve this answer | |
  • That is definitely the correct answer! Thanks! Have you had success programmatically creating orders in Commerce 1 in the past? – biko Feb 6 '19 at 0:09
0

Figured it out. For anyone else looking for this info, the answer is:

Create a field of type Customer Customer Info. Add it as a field in the User Field Settings.

Access it using the handle you assigned it.

E.g.

$customer = $user->customerInfo;
| improve this answer | |
  • Marked RoussKS as the right answer. – biko Feb 6 '19 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.