Skip to content

Instantly share code, notes, and snippets.

@brandalismo
Created March 21, 2017 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandalismo/386e4b236087e37ea334accb97c4dc1d to your computer and use it in GitHub Desktop.
Save brandalismo/386e4b236087e37ea334accb97c4dc1d to your computer and use it in GitHub Desktop.
/*
* AÑADIR CAMPO NIF/CIF EN EL FORMULARIO DE PAGO
*/
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' => __('99999999D'), // 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' );
/*
* ACTUALIZAR 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' );
/*
* MOSTRAR VALOR DEL CAMPO NIF LA PÁGINA DE EDICIÓ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 );
/*
* INCLUIR CAMPO NIF EN EL EMAIL DE NOTIFICACIÓN AL CLIENTE
*/
function woo_custom_field_checkout_email($keys) {
$keys[] = 'NIF';
return $keys;
}
add_filter('woocommerce_email_order_meta_keys', 'woo_custom_field_checkout_email');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment