Price inside Add to Cart shows only when there is a Sale Price

56 Views Asked by At

I'm using script to add the price to the Add to Cart in WooCommerce but for some reason the script seems to break when there aren't any variations set with a 'sale price'.

Would anyone have an idea why this is breaking? I have tried it in and outside of the button but experiencing the same issue. There are no errors in the console to reference.

Example of what is happening: https://i.stack.imgur.com/f6beM.jpg

<?php
add_action( 'woocommerce_after_add_to_cart_form', 'bbloomer_echo_variation_info' );

function bbloomer_echo_variation_info() {
    global $product;
    if ( ! $product->is_type( 'variable' ) ) return;

    $attributes = $product->get_variation_attributes();

    wc_enqueue_js( "
        $(document).on('found_variation', function( event, variation ) {   
                $('.pricePoint').html(variation.price_html);  
            });
        " );
    }
?>

<button type="submit" class="pricePoint single_add_to_cart_button button alt" title="Add to Cart"></button>

Thank you!

1

There are 1 best solutions below

0
On

I was able to work out the fix:

I just needed to add this to the function as the "price_html" was not being displayed by default.

add_filter( 'woocommerce_show_variation_price', '__return_true' );