Drupal Commerce alter checkout form and custom validate
来源:互联网 发布:nginx 启动不了windows 编辑:程序博客网 时间:2024/05/22 00:46
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
- 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)
- Adding a callback to validate the form hook_commerce_checkout_pane_info_alter(&$checkout_panes)
- 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
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
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
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
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!
- Drupal Commerce alter checkout form and custom validate
- Drupal commerce checkout流程学习笔记
- Drupal Commerce
- Drupal commerce 性能优化 (Drupal commerce performance optimize)
- form.validate
- validate rules custom
- Spring MVC Display, Validate and Submit Form Example
- drupal Execute custom PHP code
- The State Of E-Commerce Checkout Design 2012
- Drupal Form API
- drupal7之学习在已存在的form表单中增加自定义验证(Learn how to add custom validation to an existing Drupal form)
- drupal commerce 在阿里云和盛大云上运行测试
- Drupal 7 commerce webform alert production unit price
- Insert a form into a pop-up modal with CTools and Drupal 7
- jquery validate form
- ajax validate form check
- extjs form validate 验证
- easyui form validate
- Java之序列化 (Serialization) 详细教程
- Git清理本地缓存命令
- vim基本配置
- AWT,Panel入门,多框架,多Panel
- ios UILabel 添加删除线或者下滑线
- Drupal Commerce alter checkout form and custom validate
- 278. First Bad Version
- 自定义转场效果 presentViewController实现由左向右
- 数据库死锁那些事
- SpringMVC框架入门
- 文章标题
- IIS发布 WebService 浏览器访问报错http 500问题
- 搭建sbt
- 欢迎使用CSDN-markdown编辑器