// Crear el rol de profesional
function custom_add_roles() 
{
    add_role(
        'profesional',
        __( 'Profesional' ),
        array(
            'read'         => true
        )
    );
}
add_action( 'init', 'custom_add_roles' );

// Hook para cambiar el precio según el rol del usuario
add_filter( 'woocommerce_product_get_price', 'custom_price_based_on_user_role', 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'custom_price_based_on_user_role', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_price_based_on_user_role', 10, 2 );
add_filter( 'woocommerce_product_variation_get_regular_price', 'custom_price_based_on_user_role', 10, 2 );

// add_filter('woocommerce_product_get_price', 'custom_price_based_on_user_role', 99, 2 );
// add_filter('woocommerce_product_variation_get_price', 'custom_price_based_on_user_role', 99, 2 );

function custom_price_based_on_user_role( $price, $product ) 
{
	
    if ( is_user_logged_in() ) 
	{
		$price = (float) $price;
        $user = wp_get_current_user();
        if ( in_array( 'profesional', (array) $user->roles ) ) 
		{
			
			/*
			if ( $product->is_type( 'variable' ) ) 
			{
				$price = $price * 0.7516;
				$price = get_post_meta( $product->ID, '_profesional_price', true );
			}
			else
			{
            	$price = $price * 0.7516;
            }
			*/
			
			$price = get_post_meta( $product->get_id(), '_profesional_price', true );
			
        }

    }
    return $price;
}

// Mostrar el "Precio Profesional" en el frontend junto al precio del producto
add_filter( 'woocommerce_get_price_html', 'display_custom_simple_product_price', 10, 2 );
function display_custom_simple_product_price( $price_html, $product ) 
{

    if ( ! $product->is_type( 'simple' ) ) {
        return $price_html;
    }

    $additional_price = get_post_meta( $product->get_id(), '_profesional_price', true );

    if ( $additional_price ) 
	{
		if ( is_user_logged_in() ) 
		{
			$user = wp_get_current_user();
			if ( !in_array( 'profesional', (array) $user->roles ) ) 
			{			
				$price_html .= '<span class="additional-price">' . sprintf( __( ' Precio Profesional: %s', 'woocommerce' ), wc_price( $additional_price ) ) . '</span>';
			}
		}
    }

    return $price_html;
}






// FORMULARIO

// Añadir campo de "Precio Profesional" en productos variaciones
add_action( 'woocommerce_variation_options_pricing', 'custom_variation_price_field', 10, 3 );

function custom_variation_price_field( $loop, $variation_data, $variation ) 
{
	if ( is_user_logged_in() ) 
	{
        $user = wp_get_current_user();
        if ( !in_array( 'profesional', (array) $user->roles ) ) 
		{		
        	woocommerce_wp_text_input( array(
        		'id' => '_profesional_price[' . $variation->ID . ']',
       			'class' => 'short',
        		'label' => __( 'Precio Profesionales', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
        		'value' => get_post_meta( $variation->ID, '_profesional_price', true )
   		 	) );	
		}
	}
}

add_action( 'woocommerce_save_product_variation', 'save_custom_variation_price_field', 10, 2 );

function save_custom_variation_price_field( $variation_id, $i ) {
    $additional_price = $_POST['_profesional_price'][ $variation_id ];
    if ( isset( $additional_price ) ) {
        update_post_meta( $variation_id, '_profesional_price', esc_attr( $additional_price ) );
    }
}

add_filter( 'woocommerce_variation_price_html', 'display_custom_variation_price', 10, 2 );
function display_custom_variation_price( $price_html, $variation ) 
{
	
    $additional_price = get_post_meta( $variation->ID, '_profesional_price', true );

    if ( $additional_price ) {
        $price_html .= '<span class="additional-price">' . sprintf( __( '+%s', 'woocommerce' ), wc_price( $additional_price ) ) . '</span>';
    }

    return $price_html;
}

// Añadir campo de "Precio Profesional" en productos simples
add_action( 'woocommerce_product_options_pricing', 'custom_simple_product_price_field' );

function custom_simple_product_price_field() {
    global $post;

	if ( is_user_logged_in() ) 
	{
        $user = wp_get_current_user();
        if ( !in_array( 'profesional', (array) $user->roles ) ) 
		{		
	
    		woocommerce_wp_text_input( array(
        		'id' => '_profesional_price',
        		'class' => 'short',
        		'label' => __( 'Precio Profesionales', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
        		'desc_tip' => true,
        		'description' => __( 'Introduce un precio especial para profesionales.', 'woocommerce' ),
        		'value' => get_post_meta( $post->ID, '_profesional_price', true )
    		) );
			
		}
		
	}
}

// Guardar el campo de "Precio Profesional" en productos simples
add_action( 'woocommerce_process_product_meta', 'save_custom_simple_product_price_field' );

function save_custom_simple_product_price_field( $post_id ) {
    if ( isset( $_POST['_profesional_price'] ) ) {
        update_post_meta( $post_id, '_profesional_price', esc_attr( $_POST['_profesional_price'] ) );
    }
}