Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AshlinRejo/9fea248dc3a62baa2404fc552f68f4d2 to your computer and use it in GitHub Desktop.
Save AshlinRejo/9fea248dc3a62baa2404fc552f68f4d2 to your computer and use it in GitHub Desktop.
Discount rule v2: Dynamic strikeout on Shop page
add_action('wp_footer', function (){
?>
<script>
(function($) {
$(document).ready(function($) {
function wdr_dynamic_pricestrikeout(currentObject){
var awdr_qty_object = currentObject.closest('form').find('input[name="quantity"]');
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('button[name="add-to-cart"]').length) {
$product_id = form.find('button[name="add-to-cart"]').val();
var target = 'p.price';
if (form.find(target).length) {
$price_place = form.find(target).first();
} else {
$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);
}
$(document).on('click', '.quantity-button.quantity-up, .quantity-button.quantity-down', function() {
wdr_dynamic_pricestrikeout($(this));
});
$(document).on('change', '[name="quantity"]', function (){
wdr_dynamic_pricestrikeout($(this));
});
});
})(jQuery);
</script>
<?php
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment