Drupal Commerce alter checkout form and custom validate

Drupal Commerce: How to alter the checkout form and how to add the custom validation

Commerce has its own API http://api.drupalcommerce.org/, that we have used it to adapt the checkout form to our requirements.

Description of the required functionality

In the billing information of an order we have added the fields “Request invoice” and “NIF/CIF” (ID). By default the field “Request invoice” is not checked and “NIF/CIF” is hidden and not required. When the buyer checks the field “Request invoice” the field “NIF/CIF” becomes visible and required.

Proposed solution

  1. Adding the code that changes the field “NIF/CIF” when the user checks “Request invoice”. By using hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)
  2. Adding a callback to validate the form hook_commerce_checkout_pane_info_alter(&$checkout_panes)
  3. Adding the validation function commerce_billing_pane_validate(&$form, &$form_state, $checkout_pane, $order)

Step 1. Adding the alter form to add the behavior of the field. field_request_invoice

?
1
2
3
4
5
6
7
8
9
10
11
function example_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {
   global $user;
   $form[ 'customer_profile_billing' ][ 'field_cif_nif' ][ '#states' ] = array(
     'visible' => array(
       ':input[name="customer_profile_billing[field_request_invoice][und]"]' => array( 'checked' => TRUE),
     ),
     'required' => array(
       ':input[name="customer_profile_billing[field_request_invoice][und]"]' => array( 'checked' => TRUE),
     ),
   );
}

Step 2. Alter the customer_profile_billing panel to add the callback to the validation function

?
1
2
3
4
5
function example_commerce_checkout_pane_info_alter(&$checkout_panes) {
   if (isset($checkout_panes[ 'customer_profile_billing' ])) {
     $checkout_panes[ 'customer_profile_billing' ][ 'callbacks' ][ 'checkout_form_validate' ] = 'example_commerce_billing_pane_validate' ;
   }
}

Step 3. Adding the custom validation function

?
1
2
3
4
5
6
7
function example_commerce_billing_pane_validate(&$form, &$form_state, $checkout_pane, $order) {
   if ($form_state[ 'values' ][ 'customer_profile_billing' ][ 'field_request_invoice' ][ 'und' ][ 0 ][ 'value' ] == 1 && empty($form_state[ 'values' ][ 'customer_profile_billing' ][ 'field_cif_nif' ][ 'und' ][ 0 ][ 'value' ])) {
     form_set_error($checkout_pane[ 'pane_id' ] . '][field_cif_nif][und][0][value' , t( 'DNI/CIF/NIF field is required.' ));
     return FALSE;
   }
   return TRUE;
}

Now, the billing panel in the checkout form shows this

Commerce checkout validation

 

We hope this is of help. If you know another way to do this or you have another idea you can write a comment. Thanks!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值