Add Price on Add To Cart Button
March 31, 2021
TutorialsWooFood
woocommerce food orderingwordpress food ordering plugin
The following snippet will copy the price automatically and add it inside the add to cart button
add_action("wp_footer", "wpslash_hook_to_add_the_price_in_button");
function wpslash_hook_to_add_the_price_in_button()
{
?>
<script>
jQuery(document).ready(function()
{
jQuery(document).on('change', '.extra-options-accordion select, .extra-options-accordion input , .input-text.qty', function()
{
var price = jQuery('.wf_product_view .price:first').text();
jQuery('.wf_product_view .single_add_to_cart_button').html(price+' | Add to cart');
});
jQuery( this ).on( 'found_variation', function( event, variation ) {
var price = variation.price_html;
jQuery('.wf_product_view .single_add_to_cart_button').html(price+' | Add to cart');
});
});
</script>
<?php
}
function wpslash_include_price_in_add_to_cart_button( $text, $product ) {
$text = strip_tags(wc_price($product->get_price()))." | ".$text;
return $text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wpslash_include_price_in_add_to_cart_button', 10, 2 );