Factura debes ingresar el RUT.', 'error' ); }
if ( '' === $razon ) { wc_add_notice( 'Para Factura debes ingresar la Razón social.', 'error' ); }
if ( '' === $giro ) { wc_add_notice( 'Para Factura debes ingresar el Giro.', 'error' ); }
if ( '' !== $rut && ! dacsa_validar_rut( $rut ) ) {
wc_add_notice( 'El RUT ingresado no es válido. Revísalo (ej: 12.345.678-9).', 'error' );
}
} );
/* 4) Guardar en el pedido */
add_action( 'woocommerce_checkout_create_order', function ( $order ) {
$tipo = ( isset( $_POST['billing_tipo_documento'] ) && 'factura' === $_POST['billing_tipo_documento'] ) ? 'factura' : 'boleta';
$order->update_meta_data( '_billing_tipo_documento', $tipo );
if ( 'factura' === $tipo ) {
$order->update_meta_data( '_billing_rut', sanitize_text_field( wp_unslash( $_POST['billing_rut'] ?? '' ) ) );
$order->update_meta_data( '_billing_razon_social', sanitize_text_field( wp_unslash( $_POST['billing_razon_social'] ?? '' ) ) );
$order->update_meta_data( '_billing_giro', sanitize_text_field( wp_unslash( $_POST['billing_giro'] ?? '' ) ) );
}
}, 10, 1 );
/* 5) Mostrar en el detalle del pedido (admin) */
add_action( 'woocommerce_admin_order_data_after_billing_address', function ( $order ) {
$tipo = $order->get_meta( '_billing_tipo_documento' ) ?: 'boleta';
echo '
Tipo de documento: ' . esc_html( ucfirst( $tipo ) ) . '
';
if ( 'factura' === $tipo ) {
echo '';
echo 'RUT: ' . esc_html( $order->get_meta( '_billing_rut' ) ) . ' ';
echo 'Razón social: ' . esc_html( $order->get_meta( '_billing_razon_social' ) ) . ' ';
echo 'Giro: ' . esc_html( $order->get_meta( '_billing_giro' ) );
echo '
';
}
}, 10, 1 );
/* 6) Incluir en los emails (cliente y admin) */
add_filter( 'woocommerce_email_order_meta_fields', function ( $fields, $sent_to_admin, $order ) {
$tipo = $order->get_meta( '_billing_tipo_documento' ) ?: 'boleta';
$fields['dacsa_tipo_doc'] = array( 'label' => 'Tipo de documento', 'value' => ucfirst( $tipo ) );
if ( 'factura' === $tipo ) {
$fields['dacsa_rut'] = array( 'label' => 'RUT', 'value' => $order->get_meta( '_billing_rut' ) );
$fields['dacsa_razon'] = array( 'label' => 'Razón social', 'value' => $order->get_meta( '_billing_razon_social' ) );
$fields['dacsa_giro'] = array( 'label' => 'Giro', 'value' => $order->get_meta( '_billing_giro' ) );
}
return $fields;
}, 10, 3 );
/* Helper: validación de RUT chileno (dígito verificador, módulo 11) */
if ( ! function_exists( 'dacsa_validar_rut' ) ) {
function dacsa_validar_rut( $rut ) {
$rut = preg_replace( '/[^0-9kK]/', '', $rut );
if ( strlen( $rut ) < 2 ) { return false; }
$dv = strtoupper( substr( $rut, -1 ) );
$num = substr( $rut, 0, -1 );
if ( ! ctype_digit( $num ) ) { return false; }
$suma = 0; $mul = 2;
for ( $i = strlen( $num ) - 1; $i >= 0; $i-- ) {
$suma += intval( $num[ $i ] ) * $mul;
$mul = ( 7 === $mul ) ? 2 : $mul + 1;
}
$res = 11 - ( $suma % 11 );
$dvCalc = ( 11 === $res ) ? '0' : ( ( 10 === $res ) ? 'K' : strval( $res ) );
return $dv === $dvCalc;
}
}
Ir al contenido
“SUNWIDE 165/65R14 RS-ZERO 79T” se ha añadido a tu carrito.
Ver carrito
GOODYEAR 205/60R15 WRANGLER WORKHORSE AT 91H
Neumático para camionetas y todoterrenos que buscan el mejor desempeño Off Road y un gran comportamiento en pavimento. Su gran resistencia a daños y compuestos de última generación lo transforman en la mejor alternativa disponible en el mercado.