function fn_wp_add_related_ingredients_to_product()
{
	global $post;
	$aElements = get_post_meta( $post->ID, 'ingredientes' , false);
	if(count($aElements) == 0) return false;
	echo str_replace('[TITULO]', get_the_title($post->ID), do_shortcode('[elementor-template id="1570"]'));
}
// add_action( 'woocommerce_product_meta_end', 'fn_wp_add_related_ingredients_to_product', 1, 2 );
// add_filter('wp_add_related_ingredients_to_product', 'fn_wp_add_related_ingredients_to_product');

function fn_product_ingredients( $query )
{
	
	global $post;
	
	$aElements = get_post_meta( $post->ID, 'ingredientes' , false);
	$query->set( 'post__in', $aElements );
	
}
add_action( 'elementor/query/product_ingredients', 'fn_product_ingredients');

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs', 98, 1 );
function woo_custom_product_tabs( $tabs )
{
	
	global $product;

    // 1) Removing tabs
    unset( $tabs['description'] );           // Remove the description tab
    // unset( $tabs['reviews'] );               // Remove the reviews tab
    // unset( $tabs['additional_information'] );   // Remove the additional information tab
	
	$aTabs = Array();
	$aTabs['titulo'] = 			Array('title'=> 'Descripción', 'force'=>false);
	$aTabs['ingredientes'] = 			Array('title'=> 'Ingredientes', 'force'=>false);
	$aTabs['documentacion'] = 			Array('title'=> 'Documentación', 'force'=>false);
	// $aTabs['informacion_nutricional'] = Array('title'=> 'Información Nutricional', 'force'=>false);
	// $aTabs['informacion_adicional'] = 	Array('title'=> 'Información Adicional', 'force'=>true);
	
	$priority = 1;
	
	foreach($aTabs AS $key => $tab)
	{
		
		$product_data = $product->get_meta($key);
		
// 		var_dump($product_data);
		
		if($product_data != '' || $tab['force'])
		{

			// 2 Adding new tabs
			//Attribute Description tab
			$tabs[$key.'_tab'] = array(
				'title'     => __( $tab['title'], 'woocommerce' ),
				'callback'  => $key.'_tab_content'
			);

			$tabs[$key.'_tab']['priority'] = $priority;
			$priority++;

		}		
		
	}
	
	// Reorder
// 	$tabs['description']['priority'] = 5;
// 	$tabs['additional_information']['priority'] = 15;
	$tabs['reviews']['priority'] = 20;

    return $tabs;

}


function titulo_tab_content()
{
	global $post;
// 	var_dump($post);
// 	echo '++++++++++++';
	echo do_shortcode('[elementor-template id="1732"]');
}
// New Tab contents
function ingredientes_tab_content() 
{
	global $post;
	$aElements = get_post_meta( $post->ID, 'ingredientes' , false);
	if(count($aElements) == 0) echo '';
	echo str_replace('[TITULO]', get_the_title($post->ID), do_shortcode('[elementor-template id="1570"]'));
	// echo apply_filters('the_content', $product_data);
}
function documentacion_tab_content() 
{
	global $post;
	$aFiles = get_post_meta( $post->ID, 'documentacion' , false);
	if(count($aFiles) > 0)
	{
		
		$files = '<ul class="documentacion-ul">';

		foreach ($aFiles as $media_id) 
		{
			// Obtener la información del medio por su ID
			$attachment = get_post($media_id);

			// Verificar si el medio existe y es una imagen
			if ($attachment && $attachment->post_type === 'attachment') 
			{
				// Obtener el título y la URL del medio
				$titulo = get_the_title($media_id);
				$url = wp_get_attachment_url($media_id);
				$path_info = pathinfo($url);
    			$extension = isset($path_info['extension']) ? $path_info['extension'] : '';

				// Mostrar el título y el enlace
				$files.='<li><a href="' . esc_url($url) . '" target="_blank"><i class="fa-solid fa-file-pdf"></i><span class="doc-name">' . esc_html($titulo) . ' [.'.$extension.']</span></li>';
			}
		}	
		
		$files.= '</ul>';
		
		echo $files;
		
	}
	else
	{

	}

}