I have a view where I (by using a shortcode) insert a cookie value as a class attribute. This works fine when initially loading the page, but when clicking submit at the custom filters and the AJAX refresh option is selected, the cookie value returns blank. The strange part is, that the text ‘cookie’ and the $attr[0] value is returned, but just not the cookie value.
This is the shortcode script in functions.php
add_shortcode('get_cookie_value', 'get_cookie_value_func');
function get_cookie_value_func($attr) {
return 'cookie '.$attr[0].' = '.$_COOKIE[$attr[0]];
}
And this is the relevant part of my view:
<div class="grid products [get_cookie_value 'grid-products-[wpv-post-id]']">
Any Ideas?
I really don’t want to use the full page refresh option as it jumps to the top of the page and the view is not.
Thanks in advance 🙂
The cookie is saved from this jQuery function (when opening a parent container and not in conjunction with the AJAX refresh:
set_cookie_value (name, value, minutes=60) {
var date = new Date();
date.setTime(date.getTime() + (minutes * 60*1000));
jQuery.cookie(name, value, {expires: date});
}
In this case you're going to need to callback the function after our AJAX refresh has ran. So you will need to add your function again with the AJAX callback function for the search.
jQuery( document ).on( 'js_event_wpv_parametric_search_results_updated', function( event, data ) {
/**
* data.view_unique_id (string) The View unique ID hash
* data.layout (object) The jQuery object for the View layout wrapper
*/
set_cookie_value (name, value, minutes=60) {
var date = new Date();
date.setTime(date.getTime() + (minutes * 60*1000));
jQuery.cookie(name, value, {expires: date});
}
});