1
Advanced tooltip for post filter

Hi,

Previously I build an advanced tooltip for my client that has products in their webshop that requires some explanation when filtering them.

You can find a working example here:

https://firesafetyshop.nl/assortiment/brandblussers/

Click on the green ‘uitleg’ button on the filter part. It gets the tooltip information from the terms that I added with ACF.

Would be nice if this solution can be baked into the Divi module, now it’s some work to get this done for my clients and to be honest it’s a really great UX tooltip for all webshops that have products with advanced filtering options.

Join the conversion, Login to Reply


user-img gerwin - 18 July 2024

// tooltip trial

//

function tooltip_brandklasse()

{

ob_start();

// Get all taxonomies related to the "product" post type

$taxonomies = get_object_taxonomies('product', 'objects');

if (!empty($taxonomies)) {

// Loop through taxonomies

foreach ($taxonomies as $taxonomy) {

$terms = get_terms(array(

'taxonomy' => $taxonomy->name,

'hide_empty' => false

));

$tooltip_content = '';

// Loop through terms

foreach ($terms as $term) {

// Check if the ACF field 'uitleg' has a value

if (get_field('uitleg', $term)) {

$tooltip_content .= '<li>';

$tooltip_content .= '<span class="naam">' . $term->name . '</span>';

$tooltip_content .= '<span class="value">' . get_field('uitleg', $term) . '</span>';

$tooltip_content .= '</li>';

}

}

// If there's tooltip content, display it

if (!empty($tooltip_content)) {

// Output the filter options with a unique data-filter-name attribute

$filterName = 'filter-' . strtolower(str_replace('_', '-', $taxonomy->name));

?>

<div class="tooltip_info_container" data-tooltip-name="<?php echo $taxonomy->name; ?>">

<div class="tooltip_main_info" >

<div class="close_tooltip brandklasse"></div>

<h3>

<?php echo $taxonomy->label; ?>

</h3>

</div>

<div class="tooltip_explaining" data-tooltip-name="<?php echo $taxonomy->name; ?>">

<ul>

<?php echo $tooltip_content; ?>

</ul>

</div>

</div>

<script>

jQuery(document).ready(function($) {

$('.et_pb_contact_field_options_list[data-filter-option="<?php echo $taxonomy->name; ?>"]').find('label').each(function() {

$(this).attr('data-filter-name', '<?php echo $filterName; ?>');

});

});

</script>

<?php

}

}

}

return ob_get_clean();

}

add_shortcode('brandklasse', 'tooltip_brandklasse');

function insert_tooltip_toggle_script() {

?>

<script>

jQuery(document).ready(function($) {

$('.divi-filter-item').each(function() {

var tooltipName = $(this).data('filter-option');

var tooltipContainer = $('.tooltip_info_container[data-tooltip-name="'   tooltipName   '"]');

if (tooltipContainer.length) {

$(this).prepend('<div class="toggle-div" data-tooltip-name="'   tooltipName   '">Toggle Tooltip</div>');

}

});

$('.divi-filter-item label, .divi-filter-item .toggle-div').on('click', function() {

var tooltipName = $(this).data('tooltip-name');

var tooltipContainer = $('.tooltip_info_container[data-tooltip-name="'   tooltipName   '"]');

tooltipContainer.toggle();

});

});

</script>

<?php

}

add_action('wp_footer', 'insert_tooltip_toggle_script');
Upvoters
1
Feature Details

Status: In Review

Board: Divi Machine

Created: 18 July 2024

Last Updated: 18 July 2024

Posted By: gerwin

© 2024 Divi Engine