0

I have installed Drupal Commerce in an existing site and I am currently trying to figure out how to prepopulate fields during the checkout process. I use the Commerce Extra Adress Popuplate module to fetch the address information from the user account, but how do I do this exact same thing with other custom fields? For instance I have a custom Client number field in the user account that I really would need to prepopulate to the checkout form.

I have read a bunch of other post on this subject (for instance Populating fields from the User entity during a Commerce checkout, but I haven't found a solution that seems to do exactly what I am looing for.

EDITED: to be more specific, I have created a new Customer Profile using the Customer Profile Type UI module. It contains one field so far, the client number field. How can I fetch the current user's value from the same field in the User account, during checkout?

EDITED AGAIN: With rules I have tried using this setup:

{ "rules_kopiera_kundnummer_fran_anvandaren" : {
    "LABEL" : "Kopiera kundnummer fr\u00e5n anv\u00e4ndaren",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : { "commerce_order_insert" : [] },
    "DO" : [
      { "data_set" : {
          "data" : [ "commerce-order:owner:field-kundnummer" ],
          "value" : [ "site:current-user:field-kundnummer" ]
        }
      }
    ]   } }

But the current client number is not copied into the field in question. Any ideas?

1 Answer 1

1

If I am not mistaken, you can do this with rules. Use the 'after saving a new commerce order' event and choose to 'set a property' on the commerce order. For the value you can navigate to the customer profile and select your field.

Optionally you can also add the 'after saving an existing commerce order' to update the field at every step in the process. (commerce saves the order after each checkout step).

Update

This requires some knowledge about how Drupal commerce works. An order is created as soon as you place a product in the cart. You now have a product in the cart state. As you proceed to checkout, the order state updates with every step. If you leave the checkout, the state is frozen and you could continue it at every time.

Since your rule only acts on commerce_order_insert this means the number will only be copied if you create a new cart. (proceed to checkout your current order or delete the entire order by going to the order overview.) This is why I proposed to also add the commerce_order_update event as a trigger. That should update the number at every step of the checkout process.

Also there appears to be a misconfiguration in the rule (or I misunderstood your question). If the number is on a customer profile type, you should get it from there. [ "site:current-user:field-kundnummer" ] assumes this is a field configured on the user profile. And [ "commerce-order:owner:field-kundnummer" ] is really the user if of the order owner. (Changing that might mess up everything).

I guess you should be setting [ "commerce-order:<commerce-customer-profile-reference>:field-kundnummer" ] instead. That would be the field on the customer profile.

8
  • Thanks! I tried to create that rule, but I didn't get it ti work. If it isn't too much trouble, could you take a look on my edited question above?
    – TBJ
    Jan 2, 2017 at 12:55
  • The answer was written on my phone, so perhaps not as detailed as it should. I'll update the answer with some more details now. Jan 2, 2017 at 12:57
  • Thanks a lot for this detailed answer. I am trying to understand why it does not work. Yes I do want to fetch a value from a custom field in the User profile and set this value into a field in the checkout form.
    – TBJ
    Jan 2, 2017 at 13:43
  • Can you export the rule again and update the question? { "data_set" : { "data" : [ "commerce-order:owner:field-kundnummer" ], "value" : [ "site:current-user:field-kundnummer" ] } } is setting the field on the user to the value of the field of the user (it is a self-reference). You should be setting your customer profile setting of which only you know the name. It should be available as a property of commerce-order. Jan 2, 2017 at 13:46
  • For some reason my 'commerce customer profile type' (named "kundnummer") is not available as a property... I keep looking.
    – TBJ
    Jan 2, 2017 at 14:23

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.