您可以覆盖woocommerce templates 将它们放在主题中。覆盖,例如。form-billing.php
模板,将其放置在yourtheme/woocommerce/checkout/form-billing.php
目录
您还可以添加filter 覆盖表单字段呈现函数。甚至overwrite 整个功能。
要添加新字段,请参阅WooCommerce documentation.
我正在粘贴文档中的代码,以添加字段,而不是只将答案保留在链接中:
/**
* Add the field to the checkout
*/
add_action( \'woocommerce_after_order_notes\', \'wpse_287663_custom_checkout_field\' );
function wpse_287663_custom_checkout_field( $checkout ) {
echo \'<div id="custom_checkout_field"><h2>\' . __(\'My Field\') . \'</h2>\';
woocommerce_form_field( \'field_name\', array(
\'type\' => \'text\',
\'class\' => array(\'my-field-class form-row-wide\'),
\'label\' => __(\'Fill in this field\'),
\'placeholder\' => __(\'Enter something\'),
), $checkout->get_value( \'field_name\' ));
echo \'</div>\';
}
/**
* Process the checkout
*/
add_action(\'woocommerce_checkout_process\', \'wpse_287663_custom_checkout_field_process\');
function wpse_287663_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST[\'field_name\'] )
wc_add_notice( __( \'Please enter something into this new shiny field.\' ), \'error\' );
}
/**
* Update the order meta with field value
*/
add_action( \'woocommerce_checkout_update_order_meta\', \'wpse_287663_custom_checkout_field_update_order_meta\' );
function wpse_287663_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST[\'field_name\'] ) ) {
update_post_meta( $order_id, \'My Field\', sanitize_text_field( $_POST[\'field_name\'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( \'woocommerce_admin_order_data_after_billing_address\', \'wpse_287663_custom_checkout_field_display_admin_order_meta\', 10, 1 );
function wpse_287663_custom_checkout_field_display_admin_order_meta($order){
echo \'<p><strong>\'.__(\'My Field\').\':</strong> \' . get_post_meta( $order->id, \'My Field\', true ) . \'</p>\';
}