Created
February 5, 2019 10:47
-
-
Save ohyeahdev/48364c40fbc99649299e444cb84ed2bc to your computer and use it in GitHub Desktop.
Código de la ponencia de campos personalizado para WooCommerce de SemanaWP 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Description: Código para SemanaWP 3. Como crear campos personalizados para Woocommerce | |
* Author: Óscar Abad Folgueira | |
* Author URI: https://www.ohyeahdev.com | |
*/ | |
/* SemanaWP #3: Añadir campos personalizado a WooCommerce */ | |
/* | |
* Añadir campo tipo texto: (Segunda descripción) | |
*/ | |
function ohyd_crear_campo_texto() { | |
$args = array( | |
'id' => 'segunda_descripcion_producto', | |
'label' => __( 'Segunda Descripción', 'ohyd_semanawp' ), | |
'class' => 'ohyd_segunda_descripcion', | |
'desc_tip' => true, | |
'description' => __( 'Introduce la segunda descripción del producto.', 'ohyd_semanawp' ), | |
); | |
woocommerce_wp_text_input( $args ); | |
} | |
add_action( 'woocommerce_product_options_general_product_data', 'ohyd_crear_campo_texto' ); | |
/* | |
* Guardar campo: (Segunda descripción) | |
*/ | |
function ohyd_guardar_campo_segunda_descripcion( $post_id ) { | |
$product = wc_get_product( $post_id ); | |
$title = isset( $_POST['segunda_descripcion_producto'] ) ? $_POST['segunda_descripcion_producto'] : ''; | |
$product->update_meta_data( 'segunda_descripcion_producto', sanitize_text_field( $title ) ); | |
$product->save(); | |
} | |
add_action( 'woocommerce_process_product_meta', 'ohyd_guardar_campo_segunda_descripcion' ); | |
/* | |
* Mostrar campo en el front: (Segunda descripción) | |
*/ | |
function ohyd_mostrar_segunda_descripcion() { | |
global $post; | |
$product = wc_get_product( $post->ID ); | |
$title = $product->get_meta( 'segunda_descripcion_producto' ); | |
if( $title ) { | |
printf( '<div style="font-weight: bold; color: red;" >%s</div><br />', esc_html( $title ) ); | |
} | |
} | |
add_action( 'woocommerce_before_add_to_cart_button', 'ohyd_mostrar_segunda_descripcion' ); | |
/****** Página del Checkout *********/ | |
/** | |
* Añadir un campo al Checkout | |
*/ | |
add_action('woocommerce_after_order_notes', 'ohyd_func_checkout_campo_personalizado'); | |
function ohyd_func_checkout_campo_personalizado($checkout) | |
{ | |
echo '<div id="ohyd_campo_personalizado_checkout_id"><h2>' . __('TÍTULO') . '</h2>'; | |
woocommerce_form_field('ohyd_checkout_campo_personalizado', array( | |
'type' => 'text', | |
'class' => array( | |
'ohyd_clase_campo_personalizado_checkout' | |
) , | |
'label' => __('Campo Personalizado') , | |
'placeholder' => __('Introduce el campo personalizado') , | |
'required' => true, | |
) , $checkout->get_value('ohyd_checkout_campo_personalizado')); | |
echo '</div>'; | |
} | |
/** | |
* Añadir campo NIF a detalles de facturación en checkout | |
*/ | |
add_filter( 'woocommerce_checkout_fields','ohyd_aniadir_nif_checkout'); | |
function ohyd_aniadir_nif_checkout( $fields ){ | |
$fields['billing']['nif'] = array( | |
'label' => 'NIF', | |
'placeholder' => 'Introduce tu NIF', | |
'required' => false, | |
'clear' => false, | |
'type' => 'text', | |
); | |
return $fields; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment