jQuery(document).on('found_variation', '.variations_form', function(event, variation){
let product_id=variation.variation_id;
let $targetDiv=jQuery('.lowest_price.single[data-product-id="'+product_id+'"]');
if($targetDiv.length===0){
$targetDiv=jQuery('.lowest_price.single');
}
ny_lowest_prices_ajax_trigger(product_id, $targetDiv);
});
jQuery(document).ready(function(){
jQuery('.lowest_price.list').each(function(){
let $this=jQuery(this);
let product_id=$this.data('product-id');
ny_lowest_prices_ajax_trigger(product_id, $this);
});
});
function ny_lowest_prices_ajax_trigger(product_id, $targetDiv){
jQuery.ajax({
type:"post",
dataType:"json",
url:ny_lowest_prices_ajax_object.ajax_url,
data:{
action: "ny_lowest_prices",
security:ny_lowest_prices_ajax_object.security,
post_id:product_id
},
success: function(response){
if(response.success&&response.data){
$targetDiv.html(response.data);
}else{
fetchRegularPrice(product_id, $targetDiv);
}},
error: function(){
fetchRegularPrice(product_id, $targetDiv);
}});
}
function fetchRegularPrice(product_id, $targetDiv){
jQuery.ajax({
type:"post",
dataType:"json",
url:ny_lowest_prices_ajax_object.ajax_url,
data:{
action: "ny_lowest_prices_regular_fallback",
security:ny_lowest_prices_ajax_object.security,
post_id:product_id
},
success: function(response){
if(response.success&&response.data){
$targetDiv.html(response.data);
}else{
$targetDiv.html('');
}}
});
};