Discussions

Checkout form validation: alter the value of a commerce customer profile

In my checkout form, I have 2 customer profiles: billing (default) and shipping (from commerce shipping module).

Basically, I need to emulate the commerce_shipping 7.x-1 option "copy shipping information from billing", but i need to do that manually in a validation function (the values depends from few factors).

so: how to change the values of a customer_profile address field in the validation function?

My code:

function MYMODULE_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id)
{
    // [...cut...]
    array_unshift($form['buttons']['continue']['#validate'], '_MYMODULE_validate_checkout');
}

function _MYMODULE_validate_checkout($form, &$form_state)
{
    // a custom checkbox i
    if($form_state['values']['b2s'] == 1)
    {
        // here, for example, i have to copy all the values from billing address to shipping address
    }
}

I tryed many ways, but nothing works

strae
Posted: Nov 6, 2012

Comments

strae strae on November 6, 2012

An error i get:

function _MYMODULE_validate_checkout($form, &$form_state)
{
       form_set_value(
            $form['customer_profile_shipping']['customer_profile']['#value']->commerce_customer_address,
            $form['customer_profile_billing']['customer_profile']['#value']->commerce_customer_address['und'][0],
            $form_state
        );
}

Notice: Undefined index: #parents in form_set_value() (line 2514 di /httpdocs/includes/form.inc).
Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, null given, called in /httpdocs/includes/form.inc on line 2514 and defined in drupal_array_set_nested_value() (linea 6440 di /httpdocs/includes/common.inc).

Joel Wallis Jucá on November 6, 2012

I've worked in a similar issue recently. I created the customer profile programmatically and posted the code here:
http://www.drupalcommerce.org/comment/5505#comment-5505

I think you need to add your custom validation function to the billing information form, and build the shipping information programmatically. But why in validation? Why not in submition? Or better, why not in billing information creation event? I'm not sure but I believe that Customer Profiles are entities, so you may use Entity API to do that.

strae strae on November 7, 2012

Hi joe, thanks for your reply!

Your totally right, things like that should be done in submit hook and not in validation, but I was testing a thing in a validation hook so i put in there my test.

I'll take a look to your code, becose this problem really look wired to me!

By the way, i think my next e-commerce I wont use billing ad shipping profiles, personally I dont like how they get handled, im thinking about a very simply things (both for me and for site's users) associating those data directly to the user instead of keeping somehow separed site users from commerce users.

StryKaizer Jimmy Henderickx on January 31, 2014

@strae

The reason commerce is storing this for each order is because this data should be tied to an order.
If a user orders something on your site you want to know which address he entered for that order.
If he later on decides to change his address in his profile, we dont want the order data to change along as tracing where an order was sent is no option anymore.