4

I want to display some info from the Customer Profile (Customer Number, Contact Number) whenever the shopping cart view displays. I can see these fields in 'People' but obviously they are -extended- fields... when I run this:

$my_user = user_load( $user->uid );

...I get the field names for the data I want, but not the values.

I -thought- I could do this...

$order = commerce_cart_order_load($user->uid);

...and then somehow get to the customer_profile like so:

commerce_customer_profile_load ($profile_id);

...but I have no idea how to connect the $order or the global $user->uid with the $profile_id

How does one do this?

Again, I just need to retrieve the string values of Customer Number and Contact Number.

TIA,

---JC

4 Answers 4

6

There are probably a few different ways to bridge that gap but here are the first 2 that spring to mind:

  1. Use commerce_customer_profile_load_multiple instead, and make use of the $conditions parameter, e.g.

    $profiles = commerce_customer_profile_load_multiple(array(), array('uid' => $order->uid));
    
    foreach ($profiles as $profile) {
      // Make sure this is the correct profile (users can have multiple profiles)
      // and extract the field values as you would with any entity (field_get_items())
    }
    

    But the $conditions array is deprecated for core entities so probably will be for commerce entities too at some point.

  2. Use an EntityFieldQuery to get the profiles, e.g.

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'commerce_customer_profile')
      ->propertyCondition('uid', $order->uid);
    
    $results = $query->execute();
    
    if (!empty($results['commerce_customer_profile'])) {
      $profiles = commerce_customer_profile_load_multiple(array_keys($results['commerce_customer_profile']));
    
      foreach ($profiles as $profile) {
        // ...
      }
    }
    
6
  • Just walked out the door so can't try until later, but wanted to thank you asap. Not trying to be snarky, but... is it just -me-, but Drupal Commerce seems -almost- intentionally convoluted (or perhaps just under-documented)? This seems like a very common task that almost any dev would want to do... so I expected it to be somewhat more clearly 'exposed'. Cheers.
    – jchwebdev
    Commented Oct 31, 2012 at 23:26
  • 1
    It's a tricky one, I do understand your frustration though. The thing is the commerce modules are actually pretty well documented, it's just that you need to know quite a bit about the way Drupal core entities work in general to get the most out of it. The solutions I've outlined above are completely generic and the logic could apply to any type of entity, not just commerce entities, so arguably the documentation for such things would fall outside of the scope of the commerce modules themselves
    – Clive
    Commented Oct 31, 2012 at 23:37
  • I'm starting to get that---basically they assume you know not only PHP, but Drupal's -paradigm- very well and try to adhere to that. My beef is the dearth of examples. It grinds at me because a number of people have clearly spent a lot of effort on doing examples for end-users (non-programmers) and I find myself wishing they'd divery some of that effort to API examples. I've been approached by several companies to work on D/C and -none- of them from admin on down want to mess with complex Rules. They'd much rather have a programmer do -whatever-. Again THANKS. Sorry for ranting.
    – jchwebdev
    Commented Oct 31, 2012 at 23:41
  • 1
    Heh no need to apologise, I've been there :) If I could offer any advice it would be to stick with it though. I didn't 'get' commerce at first but now that I do it really is an awesome tool. And don't be afraid to use this site and ask as many questions as you need to. The beauty of this place is that useful questions are always, always welcome. So if you've got a hundred questions that will be useful to future visitors (which yours surely will by the sounds of things), ask away. Some of the core D/C developers are regular visitors so you shouldn't be waiting too long for a good response
    – Clive
    Commented Oct 31, 2012 at 23:48
  • Hi again... Neither of these seem to work. They both return an empty array. I'm using Drupal 7 if that makes any difference. Just to be clear: $query = new EntityFieldQuery(); $query->entityCondition('entity_type', 'commerce_customer_profile') ->propertyCondition('uid', $order->uid); $order->uid is the Drupal user ID. Any ideas about what I'm doing wrong... or... what more info I can provide to help? Again, the data is 'there' in the Drupal Admin under People. (BTW: Unfortunately I have no direct access to the db so I can't check in via Navicat.) Help! ---JC
    – jchwebdev
    Commented Nov 1, 2012 at 0:57
4

You can also use entity_metadata_wrappers:

$order = commerce_cart_order_load($user->uid);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$billing_profile_wrapper = $order_wrapper->commerce_customer_billing;

So now you have your profile wrapped in an entity_metadata_wrapper in the var $billing_profile_wrapper. Since it's a entity_metadata_wrapper, we can access it's field really easy:

$billing_profile_wrapper->field_name_you_need->value();

More info on entity_metadata_wrapper's:

Feel free to add more links, these are just the ones I like the most

1

If you have the $order object, then...

$profile_id = $order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'];

4
  • You should not rely on LANGUAGE_NONE and 0 here. you should use the entity_metadata_wrapper. field translation and all..
    – Pinoniq
    Commented Jun 22, 2015 at 8:50
  • Thanks @Pinoniq. Is there ever more than one profile per order? If so, then you're right that 0 isn't a good option. Why not use LANGUAGE_NONE? Considering that what one is after is a numeric ID, I don't see the downside. Lemme know if I'm missing something. Seems like the OP already has the data in the $order object, so why not use it. Hmm, I guess if there's some chance the commerce devs will change the structure of the commerce_customer_billing array in a future release, that would present a problem for my approach.
    – arnoldbird
    Commented Jun 22, 2015 at 13:51
  • if you use field_translation, then the LANGUAGE_NONE could possibly not exist. And why write code that might possibly break if there is a perfect alternative to use. I even think using the entity_metadata_wrapper approach looks cleaner, but that is not a real argument :p
    – Pinoniq
    Commented Jun 23, 2015 at 8:50
  • @Pinoniq -- Why not post an answer to the question using entity_metadata_wrapper? I'd like to see your code, and it looks like the answer hasn't been given.
    – arnoldbird
    Commented Jun 23, 2015 at 12:47
0

You may need this information outside the context of a commerce order. I worked this out to get the profile ID associated to a User. You will need to loop through results if you get more than one:

NOTE: field_profile is the name of the Customer reference profile field on my User accounts.

<?php

  global $user;
  $uid = $user->uid;
  $query = db_select("field_data_field_profile","p");
  $query->fields("p",array("field_profile_profile_id"));
  $query->condition("entity_id",$uid);
  $output = $query->execute()->fetchObject();

  $profile_id = $output->field_profile_profile_id;
  if(!empty($profile_id)){
    $profile_info = commerce_customer_profile_load ($profile_id);
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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