var product_data = []; var product_data_direcao = []; var product_data_wp = [];
$(document).ready(function() {
getDataWP();
$('#direcao_produtos').load("https://www.direcaoconcursos.com.br/professores/igor-cintra main .card-holder", function() { createDataDirecao();
product_data = product_data_wp.concat(product_data_direcao); product_data = shuffle(product_data); populate(); })
$( document ).on( "click", ".view_product", function(event) { event.preventDefault(); var site = $(this).attr("product-site"); var product_url = $(this).attr("product-url");
if(site == "direcao"){ $("#div_redir").show(); setTimeout(function(){ window.open('https://www.direcaoconcursos.com.br'+product_url, '_blank'); $("#div_redir").hide(); }, 3000); } else{ window.open(product_url, '_blank'); }
});
$( document ).on( "change", "#select_site", function(event) { event.preventDefault(); filterSite($(this).val()); });
$( document ).on( "keyup", "#search", function(event) { event.preventDefault(); filterStr($(this).val()); });
})
function filterStr(str){ product_data = product_data_wp.concat(product_data_direcao); if(str != ""){ product_data = product_data.filter( item => item.name.toLowerCase().includes(str.toLowerCase()) ); } populate(); }
function filterSite(site){ product_data = product_data_wp.concat(product_data_direcao); if(site != ""){ product_data = product_data.filter((item) => { return item.site === site; }) } populate(); }
function createDataDirecao(){
$( "#direcao_produtos .card-holder" ).each(function( index ) { var item = {}; item["name"] = $(this).find(".curso-titulo").text(); item["url"] = $(this).find(".btn-acao").attr("href"); item["price"] = $(this).find(".preco").eq(1).text().replace("R$","").replace(",","."); item["img"] = $(this).find("img").eq(0).attr("data-src"); item["site"] = "direcao"; product_data_direcao[index] = item; }); }
function getDataWP(){ $.get( "https://teste.igorcintra.com.br/wp-admin/admin-ajax.php?action=products", function( data ) { console.log(data); product_data_wp = data;
product_data = product_data_wp.concat(product_data_direcao); product_data = shuffle(product_data); populate(); }); }
function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle... while (0 !== currentIndex) {
// Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1;
// And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; }
return array; }
function populate(){ $("#div_lista_produtos").html("");
for ( var i = 0; i < product_data.length; i++ ) { var item = product_data[i]; var img = "https://www.haffs.com.br/igor/assets/images/igor/logo.png"; if(item.site == "direcao" ){ img = "https://www.direcaoconcursos.com.br/wp-content/uploads/2019/06/logo-direcao-concursos.png"; } var item_html = `
`;
if(item.name != ""){ $("#div_lista_produtos").append(item_html); }
}
$("#total").text(product_data.length);
}