douxia6163 2018-01-03 09:35
浏览 28
已采纳

Woocommerce自定义结帐需要现场行为问题

I copied some code into my functions.php file to include a NIF / CIF field in the checkout page. It had to be a required and that worked just fine, but for some reason it has stopped working and now a customer can checkout without entering anything into the field.

function woo_custom_field_checkout($checkout) {
  echo '<div id="additional_checkout_field">';
  woocommerce_form_field( 'nif', array( // Identificador del campo 
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'required'      => true,            // ¿El campo es obligatorio 'true' o 'false'?
    'label'       => __('NIF / CIF'),   // Nombre del campo 
    'placeholder'   => __('Ej: 12345678X'), // Texto de apoyo que se muestra dentro del campo
  ), $checkout->get_value( 'nif' ));    // Identificador del campo 
  echo '</div>'; 
}
add_action( 'woocommerce_after_checkout_billing_form', 'woo_custom_field_checkout' );

/*
* INCLUYE NIF/CIF EN LOS DETALLES DEL PEDIDO CON EL NUEVO CAMPO
*/
function woo_custom_field_checkout_update_order($order_id) {
  if ( ! empty( $_POST['nif'] ) ) {
    update_post_meta( $order_id, 'NIF', sanitize_text_field( $_POST['nif'] ) );
  }
}
add_action( 'woocommerce_checkout_update_order_meta', 'woo_custom_field_checkout_update_order' );
/*
* MUESTRA EL VALOR DEL CAMPO NIF/CIF LA PÁGINA DE MODIFICACIÓN DEL PEDIDO
*/
function woo_custom_field_checkout_edit_order($order){
  echo '<p><strong>'.__('NIF').':</strong> ' . get_post_meta( $order->id, 'NIF', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'woo_custom_field_checkout_edit_order', 10, 1 );
/*
* INCLUYE EL CAMPO NIF/CIF EN EL CORREO ELECTRÓNICO DE AVISO A TU CLIENTE
*/
function woo_custom_field_checkout_email($keys) {
  $keys[] = 'NIF';
  return $keys;
}
add_filter('woocommerce_email_order_meta_keys', 'woo_custom_field_checkout_email');
  • 写回答

1条回答 默认 最新

  • douliu1092 2018-01-03 10:02
    关注

    The missing code is the following, that will output a error notice if the field is empty:

    // VALIDA EL CAMPO CUANDO SE PUBLIQUE EL FORMULARIO DE PAGO
    add_action('woocommerce_checkout_process', 'custom_checkout_field_process');
    function custom_checkout_field_process() {
        // Check if set, if its not set add an error.
        if ( ! $_POST['nif'] )
            wc_add_notice( __( 'Por favor, ingrese el campo NIF/CIF.' ), 'error' );
    }
    

    Code goes in function.php file of your active child theme (or active theme).

    Tested and works. If the field is not filled, when submitted customer will get this notice:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度