<script>(function() {
// function to get GET variables from the address bar
function getParameterByName($from, $name) {
let match = RegExp(`[?&]` + $name + `=(?!=&)([^&]*)`).exec($from);
return match && decodeURIComponent(match[1].replace(/\+/g, ` `));
}
// start recording an array with site sorting data and GET variables upon detecting sort_by
let $from = window.location.href;
if (getParameterByName($from, "sort_by") !== null) {
let $hotengine_cookie_conv_to_get = [
{"sort_by": getParameterByName($from, "sort_by")},
{"order_by": getParameterByName($from, "order_by")},
{"order_by_param": getParameterByName($from, "order_by_param")},
{"shop_new_goods": getParameterByName($from, "shop_new_goods")},
{"shop_sale": getParameterByName($from, "shop_sale")},
{"shop_bestsellers": getParameterByName($from, "shop_bestsellers")},
{"shop_discount": getParameterByName($from, "shop_discount")},
{"shop_reduced_price": getParameterByName($from, "shop_reduced_price")},
{"sort_instock_priority": getParameterByName($from, "sort_instock_priority")},
{"price_from": getParameterByName($from, "price_from")},
{"price_to": getParameterByName($from, "price_to")},
{"NUM_ONPAGE": getParameterByName($from, "NUM_ONPAGE")},
{"smart_search": getParameterByName($from, "smart_search")},
{"search": getParameterByName($from, "search")},
];
// record Cookie with data in JSON format
document.cookie = `hotengine_cookie_conv_to_get=${JSON.stringify($hotengine_cookie_conv_to_get)}; path=/; max-age=31536000;`;
}
// to delete Cookie use document.cookie=`hotengine_cookie_conv_to_get=; path=/; max-age=0;`;
})();
</script>
В результате, если посетитель сортирует товары, то из адресной строки вынимаются данные «
» и другие. Данные записываются в Cookie в json формате. После чего при открытии страниц в дальнейшем, сайт при обнаружении Cookie «
», конвертирует имена (индексы) массива, в GET переменные с соответствующими индексами и значениями. Таким образом выбрав сортировку сайта один раз, посетитель будет видеть ее до тех пор, пока Cookie не будет изменен.