John Davidson

javascript - Return original value jquery / php

0 comments
Message:


I am using this code to run my extra button. It changes original price value to custom value. Is it possible to click again on button named „afterclickbutton“ to get price with original value - meaning get regular price, and reset whole thing, so it could be possible to click again and get custom value, then click and get regular price value etc etc.?



// Přidání bestpprice' vlastního pole
$bestprice = get_post_meta( $product->get_id(), '_bestprice', true);


if( ! empty($bestprice) ):

$bestprice = wc_get_price_to_display( $product, array( 'price' => $bestprice ) );
$reg_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$range = wc_format_sale_price( $reg_price, $bestprice );
?>
<!-- The button and hidden field -->
<div class="bestprice-wrapper"><br>
<a href="" class="get_bestprice button alt" id="get_bestprice"><?php _e('Aktivovat lepší cenu');?></a>
<input type="hidden" name="bestprice" id="bestprice" class="bestprice" value="" />
</div>
<!-- The jQuery code -->
<script type="text/javascript">
(function($){
var b = '<?php echo $bestprice; ?>',
i = 'input[name=bestprice]',
p = 'p.price',
r = '<?php echo $range; ?>',
t = 'a#get_bestprice'
u = true;
$(t).click( function(e){
e.preventDefault();
if(u){
$(p).html(r); // Replacing price with the range
$(i).val(b); // Set the best price in hidden input field
$(t).text(‚Activated'); // change button text
$(t).removeClass('alt'); // Remove button 'alt' class for styling
$(t).removeClass('get_bestprice')
$(t).addClass(‚afterclickbutton')
u = false; // Disable button


}
});
})(jQuery);
</script>
<?php
endif; }



My Answer:
Yes, it is possible to achieve this functionality by adding another click event for the "afterclickbutton" and resetting the values back to the original price. Here is an example of how you can modify the existing jQuery code to achieve this:

javascript
(function($){
var b = '',
i = 'input[name=bestprice]',
p = 'p.price',
r = '',
t = 'a#get_bestprice',
u = true;

$(t).click( function(e){
e.preventDefault();
if(u){
$(p).html(r); // Replacing price with the range
$(i).val(b); // Set the best price in hidden input field
$(t).text('Activated'); // change button text
$(t).removeClass('alt'); // Remove button 'alt' class for styling
$(t).removeClass('get_bestprice');
$(t).addClass('afterclickbutton');
u = false; // Disable button
} else {
$(p).html(''); // Reset price to original value
$(i).val(''); // Reset hidden input field
$(t).text('Aktivovat lepší cenu'); // Reset button text
$(t).addClass('get_bestprice');
$(t).removeClass('afterclickbutton');
u = true; // Enable button
}
});

// Click event for afterclickbutton
$(document).on('click', 'a.afterclickbutton', function(e){
e.preventDefault();
$(p).html(''); // Reset price to original value
$(i).val(''); // Reset hidden input field
$(t).text('Aktivovat lepší cenu'); // Reset button text
$(t).addClass('get_bestprice');
$(t).removeClass('afterclickbutton');
u = true; // Enable button
});

})(jQuery);


With this modification, clicking on the "afterclickbutton" will reset the price back to the original value and allow the user to click on the "get_bestprice" button again to get the custom value.

Rate this post

5 of 5 based on 7533 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us