Created
January 20, 2022 13:58
-
-
Save AshlinRejo/f228e66cb1fd8ddc7ff726be1d5898d2 to your computer and use it in GitHub Desktop.
Discount rules v2: Dynamic strikeout customization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('wp_footer', function (){ | |
?> | |
<script> | |
jQuery(document).ready(function(){ | |
$(document).on('change', '[name="quantity"]', function (){ | |
var awdr_qty_object = $(this); | |
setTimeout(function(){ | |
var $qty = awdr_qty_object.val(); | |
var $product_id = 0; | |
var $price_place = ""; | |
var form = awdr_qty_object.closest("form"); | |
if (form.find('a[name="add-to-cart"]').length) { | |
$product_id = form.find('a[name="add-to-cart"]').attr('data-value'); | |
var target = 'div.product .price > div'; | |
if(awdr_params.custom_target_simple_product != undefined){ | |
if(awdr_params.custom_target_simple_product != ""){ | |
target = awdr_params.custom_target_simple_product; | |
} | |
} | |
$price_place = $(target).first(); | |
} else if (form.find('input[name="variation_id"]').length) { | |
$product_id = form.find('input[name="variation_id"]').val(); | |
var target = 'div.product .woocommerce-variation-price'; | |
if(awdr_params.custom_target_variable_product != undefined){ | |
if(awdr_params.custom_target_variable_product != ""){ | |
target = awdr_params.custom_target_variable_product; | |
} | |
} | |
$price_place = $(target); | |
if (!$(target+' .price').length) { | |
$price_place.html("<div class='price'></div>"); | |
} | |
$price_place = $(target+' .price') | |
} | |
if(!$product_id || $product_id == 0){ | |
if(awdr_params.custom_simple_product_id_selector != undefined){ | |
if(awdr_params.custom_simple_product_id_selector != ""){ | |
let simple_product_id_selector = awdr_params.custom_simple_product_id_selector; | |
$product_id = $(simple_product_id_selector).val(); | |
let target = 'div.product p.price'; | |
if(awdr_params.custom_target_simple_product != undefined){ | |
if(awdr_params.custom_target_simple_product != ""){ | |
target = awdr_params.custom_target_simple_product; | |
} | |
} | |
$price_place = $(target).first(); | |
} | |
} | |
if(awdr_params.custom_variable_product_id_selector != undefined){ | |
if(awdr_params.custom_variable_product_id_selector != ""){ | |
let variable_product_id_selector = awdr_params.custom_variable_product_id_selector; | |
$product_id = $(variable_product_id_selector).val(); | |
let target = 'div.product .woocommerce-variation-price'; | |
if(awdr_params.custom_target_variable_product != undefined){ | |
if(awdr_params.custom_target_variable_product != ""){ | |
target = awdr_params.custom_target_variable_product; | |
} | |
} | |
if (!$(target+' .price').length) { | |
$price_place.html("<div class='price'></div>"); | |
} | |
$price_place = $(target+' .price') | |
} | |
} | |
} | |
if (!$product_id || !$price_place || $product_id == 0) { | |
return; | |
} | |
var data = { | |
action: 'wdr_ajax', | |
method: 'get_price_html', | |
product_id: $product_id, | |
qty: $qty, | |
awdr_nonce: awdr_params.nonce, | |
}; | |
$.ajax({ | |
url: awdr_params.ajaxurl, | |
data: data, | |
type: 'POST', | |
success: function (response) { | |
if (response.price_html) { | |
$price_place.html(response.price_html) | |
} else { | |
if(response.original_price_html != undefined){ | |
$price_place.html(response.original_price_html) | |
} | |
} | |
}, | |
error: function (response) { | |
$price_place.html("") | |
} | |
}); | |
}, 0); | |
}); | |
}); | |
</script> | |
<?php | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment