Formulario de pedido de la tienda virtual de Herves-Vida

Formulario de pedido de la tienda virtual de Herves-Vida

En ¿Cómo añadir un campo NIF o CIF a WooCommerce 2.0? os explicamos cómo añadir un campo NIF/CIF a WooCommerce, y el código funciona perfectamente en WooCommerce 2.0, pero al haberse publicado la nueva versión 2.1, ha dejado de funcionar. Por ello abrimos esta nueva entrada con el nuevo código, al que en realidad sólo hay que cambiarle dos líneas y añadirle una nueva función, para que todos aquellos que decidan continuar con WooCommerce 2.0 puedan seguir usando el código antiguo, y los que ya habéis actualizado a WooCommerce 2.1, podáis utilizar este nuevo que hoy os proponemos.

Esta vez no vamos a entrar a detallar el código, ya que el que necesite conocer a fondo cada función puede hacerlo en la anterior entrada.

Al igual que en las funciones anteriores, vamos a añadir el siguiente código PHP al archivo functions.php de nuestra plantilla o hijo de la plantilla:

//Arreglamos la dirección predeterminada
function campos_de_direccion($campos) {
    $campos['nif'] = array(
        'label' => __('<abbr lang="es" title="Código de Identificación Fiscal">CIF</abbr>/<abbr lang="es" title="Número de Identificación Fiscal">NIF</abbr>', 'woocommerce'),
        'placeholder' => _x('Introduzca el CIF/NIF', 'placeholder', 'woocommerce'),
        'required' => false,
        'class' => array('form-row-last'),
        'clear' => true,
    );

    $campos['company']['class'][0] = 'form-row-first';
    $campos['city']['class'][0] = 'form-row-first';
    $campos['state']['class'][0] = 'form-row-last update_totals_on_change';
    $campos['postcode']['class'][0] .= ' update_totals_on_change';

    //Reordenamos los campos
    $campos_nuevos['country'] = $campos['country'];
    $campos_nuevos['first_name'] = $campos['first_name'];
    $campos_nuevos['last_name'] = $campos['last_name'];
    $campos_nuevos['company'] = $campos['company'];
    $campos_nuevos['nif'] = $campos['nif'];
    $campos_nuevos['address_1'] = $campos['address_1'];
    $campos_nuevos['address_2'] = $campos['address_2'];
    $campos_nuevos['postcode'] = $campos['postcode'];
    $campos_nuevos['city'] = $campos['city'];
    $campos_nuevos['state'] = $campos['state'];
    if (isset($campos['email'])) $campos_nuevos['email'] = $campos['email'];
    if (isset($campos['phone'])) $campos_nuevos['phone'] = $campos['phone'];

    return $campos_nuevos;
}
add_filter('woocommerce_default_address_fields' , 'campos_de_direccion');

//Nueva función para hacer compatible el código con WooCommerce 2.1
function dame_campo_personalizado($campo, $pedido) {
	$valor = get_post_meta($pedido, $campo, false);

	if (isset($valor[0])) return $valor[0];

	return NULL;
}

//Añadimos el NIF a la dirección de facturación y envío
function anade_campo_nif_direccion_facturacion($campos, $pedido) {
	$campos['nif'] = dame_campo_personalizado('_billing_nif', $pedido->id);

	return $campos;
}
add_filter('woocommerce_order_formatted_billing_address','anade_campo_nif_direccion_facturacion', 1, 2);

function anade_campo_nif_direccion_envio($campos, $pedido) {
	$campos['nif'] = dame_campo_personalizado('_shipping_nif', $pedido->id);

	return $campos;
}
add_filter('woocommerce_order_formatted_shipping_address','anade_campo_nif_direccion_envio', 1, 2);

function formato_direccion_de_facturacion($campos, $argumentos) {
    $campos['{nif}'] = $argumentos['nif'];
    $campos['{nif_upper}'] = strtoupper($argumentos['nif']);

    return $campos;
}
add_filter('woocommerce_formatted_address_replacements','formato_direccion_de_facturacion', 1, 2);

//Reordenamos los campos de la dirección predeterminada
function formato_direccion_localizacion($campos) {
    $campos['default'] = "{name}\n{company}\n{nif}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}";
    $campos['ES'] = "{name}\n{company}\n{nif}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}";

    return $campos;
}
add_filter('woocommerce_localisation_address_formats','formato_direccion_localizacion');

//Arreglamos el formulario de envío
function formulario_de_envio($campos) {
    $campos['shipping_email'] = array(
        'label' => __('Email Address', 'woocommerce'),
        'required' => false,
        'class' => array('form-row-first'),
        'validate' => array( 'email' ),
    );
    $campos['shipping_phone'] = array(
        'label' => __('Phone', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-last'),
        'clear' => true,
    );
    $campos['shipping_postcode'] = array(
        'label' => __( 'Postcode / Zip', 'woocommerce' ),
        'placeholder' => __( 'Postcode / Zip', 'woocommerce' ),
        'required' => true,
        'class' => array( 'form-row-wide', 'address-field' ),
        'clear' => true,
        'custom_attributes' => array(
            'autocomplete' => 'no'
        )
    );

    return $campos;
}
add_filter('woocommerce_shipping_fields' , 'formulario_de_envio');

//Arreglamos el formulario de cobro
function formulario_de_cobro($campos) {
    $campos['billing_postcode'] = array(
        'label' => __( 'Postcode / Zip', 'woocommerce' ),
        'placeholder' => __( 'Postcode / Zip', 'woocommerce' ),
        'required' => true,
        'class' => array( 'form-row-wide', 'address-field' ),
        'clear' => true,
        'custom_attributes' => array(
            'autocomplete' => 'no'
        )
    );

    return $campos;
}
add_filter('woocommerce_billing_fields' , 'formulario_de_cobro');

//Añade el campo CIF/NIF a usuarios
function anade_campos_administracion_usuarios($campos) {
    $campos['billing']['fields']['billing_nif'] = array(
            'label' => __('CIF/NIF', 'woocommerce'),
            'description' => ''
    );

    $campos['shipping']['fields']['shipping_nif'] = array(
            'label' => __('CIF/NIF', 'woocommerce'),
            'description' => ''
    );
    $campos['shipping']['fields']['shipping_email'] = array(
            'label' => __('Email', 'woocommerce'),
            'description' => ''
    );
    $campos['shipping']['fields']['shipping_phone'] = array(
            'label' => __('Telephone', 'woocommerce'),
            'description' => ''
    );

    //Reordenamos los campos
    $campos_nuevos['billing']['title'] = $campos['billing']['title'];
    $campos_nuevos['billing']['fields']['billing_first_name'] = $campos['billing']['fields']['billing_first_name'];
    $campos_nuevos['billing']['fields']['billing_last_name'] = $campos['billing']['fields']['billing_last_name'];
    $campos_nuevos['billing']['fields']['billing_company'] = $campos['billing']['fields']['billing_company'];
    $campos_nuevos['billing']['fields']['billing_nif'] = $campos['billing']['fields']['billing_nif'];
    $campos_nuevos['billing']['fields']['billing_address_1'] = $campos['billing']['fields']['billing_address_1'];
    $campos_nuevos['billing']['fields']['billing_address_2'] = $campos['billing']['fields']['billing_address_2'];
    $campos_nuevos['billing']['fields']['billing_postcode'] = $campos['billing']['fields']['billing_postcode'];
    $campos_nuevos['billing']['fields']['billing_city'] = $campos['billing']['fields']['billing_city'];
    $campos_nuevos['billing']['fields']['billing_state'] = $campos['billing']['fields']['billing_state'];
    $campos_nuevos['billing']['fields']['billing_country'] = $campos['billing']['fields']['billing_country'];
    $campos_nuevos['billing']['fields']['billing_phone'] = $campos['billing']['fields']['billing_phone'];
    $campos_nuevos['billing']['fields']['billing_email'] = $campos['billing']['fields']['billing_email'];

    $campos_nuevos['shipping']['title'] = $campos['shipping']['title'];
    $campos_nuevos['shipping']['fields']['shipping_first_name'] = $campos['shipping']['fields']['shipping_first_name'];
    $campos_nuevos['shipping']['fields']['shipping_last_name'] = $campos['shipping']['fields']['shipping_last_name'];
    $campos_nuevos['shipping']['fields']['shipping_company'] = $campos['shipping']['fields']['shipping_company'];
    $campos_nuevos['shipping']['fields']['shipping_nif'] = $campos['shipping']['fields']['shipping_nif'];
    $campos_nuevos['shipping']['fields']['shipping_address_1'] = $campos['shipping']['fields']['shipping_address_1'];
    $campos_nuevos['shipping']['fields']['shipping_address_2'] = $campos['shipping']['fields']['shipping_address_2'];
    $campos_nuevos['shipping']['fields']['shipping_postcode'] = $campos['shipping']['fields']['shipping_postcode'];
    $campos_nuevos['shipping']['fields']['shipping_city'] = $campos['shipping']['fields']['shipping_city'];
    $campos_nuevos['shipping']['fields']['shipping_state'] = $campos['shipping']['fields']['shipping_state'];
    $campos_nuevos['shipping']['fields']['shipping_country'] = $campos['shipping']['fields']['shipping_country'];
    $campos_nuevos['shipping']['fields']['shipping_phone'] = $campos['shipping']['fields']['shipping_phone'];
    $campos_nuevos['shipping']['fields']['shipping_email'] = $campos['shipping']['fields']['shipping_email'];

    $campos_nuevos = apply_filters('wcbcf_customer_meta_fields', $campos_nuevos);

    return $campos_nuevos;
}
add_filter('woocommerce_customer_meta_fields', 'anade_campos_administracion_usuarios');

//Añadimos el NIF a la dirección de facturación y envío
function anade_campo_nif_usuario_direccion_facturacion($campos, $usuario) {
    $campos['nif'] = get_user_meta($usuario, 'billing_nif', true);

    return $campos;
}
add_filter('woocommerce_user_column_billing_address','anade_campo_nif_usuario_direccion_facturacion', 1, 2);
function anade_campo_nif_usuario_direccion_envio($campos, $usuario) {
    $campos['nif'] = get_user_meta($usuario, 'shipping_nif', true);

    return $campos;
}
add_filter('woocommerce_user_column_shipping_address','anade_campo_nif_usuario_direccion_envio', 1, 2);

//Añade el campo NIF a Editar mi dirección
function anade_campo_nif_editar_direccion($campos, $usuario, $nombre) {
    $campos['nif'] = get_user_meta($usuario, $nombre . '_nif', true);

    //Ordena los campos
    $campos_nuevos['first_name'] = $campos['first_name'];
    $campos_nuevos['last_name'] = $campos['last_name'];
    $campos_nuevos['company'] = $campos['company'];
    $campos_nuevos['nif'] = $campos['nif'];
    $campos_nuevos['address_1'] = $campos['address_1'];
    $campos_nuevos['address_2'] = $campos['address_2'];
    $campos_nuevos['postcode'] = $campos['postcode'];
    $campos_nuevos['city'] = $campos['city'];
    $campos_nuevos['state'] = $campos['state'];
    $campos_nuevos['country'] = $campos['country'];

    return $campos_nuevos;
}
add_filter('woocommerce_my_account_my_address_formatted_address', 'anade_campo_nif_editar_direccion', 10, 3);

//Añade el campo NIF a Detalles del pedido
function anade_campo_nif_editar_direccion_pedido($campos) {
    $campos['nif'] = array(
        'label' => __('CIF/NIF', 'woocommerce'),
        'show'  => false
    );

    //Ordena los campos
    $campos_nuevos['first_name'] = $campos['first_name'];
    $campos_nuevos['last_name'] = $campos['last_name'];
    $campos_nuevos['company'] = $campos['company'];
    $campos_nuevos['nif'] = $campos['nif'];
    $campos_nuevos['address_1'] = $campos['address_1'];
    $campos_nuevos['address_2'] = $campos['address_2'];
    $campos_nuevos['postcode'] = $campos['postcode'];
    $campos_nuevos['city'] = $campos['city'];
    $campos_nuevos['state'] = $campos['state'];
    $campos_nuevos['country'] = $campos['country'];

    return $campos_nuevos;
}
add_filter('woocommerce_admin_billing_fields', 'anade_campo_nif_editar_direccion_pedido');
add_filter('woocommerce_admin_shipping_fields', 'anade_campo_nif_editar_direccion_pedido');

function carga_hoja_de_estilo_editar_direccion_pedido() {
    echo '</pre>
<style type="text/css"><!--
#order_data .order_data_column ._billing_company_field, #order_data .order_data_column ._shipping_company_field { float: left; margin: 9px 0 0; padding: 0; width: 48%; }
        #order_data .order_data_column ._billing_nif_field, #order_data .order_data_column ._shipping_nif_field { float: right; margin: 9px 0 0; padding: 0; width: 48%; }
--></style>
<pre>';
}
add_action('woocommerce_admin_order_data_after_billing_address', 'carga_hoja_de_estilo_editar_direccion_pedido');

Tras modificarlo y guardarlo, veremos que los errores y problemas que nos habían aparecido con WooCommerce 2.1 desaparecerán.

Esperamos que os resulte útil y que nos aviséis de los posibles problemas que os aparezcan para ir actualizando el código cuando sea necesario.