How to show individual error on woocommerce checkout page

ぐ巨炮叔叔 提交于 2021-02-08 02:06:14

问题


Woocommerce checkout page error messages image

Currently woocommerce showing error as above shown in image. I want individual error message below each input box as shown in below image. Please help.

Expected output image


回答1:


You need to add PHP snippet and css.

add_filter( 'woocommerce_form_field', 'checkout_fields_in_label_error', 10, 4 );

function checkout_fields_in_label_error( $field, $key, $args, $value ) {
   if ( strpos( $field, '</span>' ) !== false && $args['required'] ) {
      $error = '<span class="error" style="display:none">';
      $error .= sprintf( __( '%s is a required field.', 'woocommerce' ), $args['label'] );
      $error .= '</span>';
      $field = substr_replace( $field, $error, strpos( $field, '</span>' ), 0);
   }
   return $field;
}

CSS:

.woocommerce-checkout p.woocommerce-invalid-required-field span.error {
   color: #e2401c;
   display: block !important;
   font-weight: bold;
}


来源:https://stackoverflow.com/questions/45499504/how-to-show-individual-error-on-woocommerce-checkout-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!