Site logo

Archived topic

total Price on product page

3 replies · Started by Jusung on March 3, 2023

Viewing posts 1–4 of 4

Hello. I am trying to customize the product page now
and for the total price, I added this code

add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 31, 0 );

function action_woocommerce_single_product_summary() {
    global $product;
    
    $price = $product->get_price();
    $currency_symbol = get_woocommerce_currency_symbol();
    $min_qnty = $product->get_min_purchase_quantity();
    $initial_price = floatval($price) * intval ($min_qnty);
    
    // leave the span class 'prezzo' as it is or it won't render the price
    echo sprintf('<div id="product_total_price">%s %s</div>', __('Total:','woocommerce'), '<span class="prezzo">' . $currency_symbol . $initial_price . '</span>' );
    ?>
    <script>
    jQuery(function($) {
        // vars
        var price = <?php echo $price; ?>, 
            currency = '<?php echo $currency_symbol; ?>',
            quantity =  $( '[name=quantity]' ).val();
            
        var product_total = parseFloat( price * quantity );
        $( '#product_total_price .prezzo' ).html( currency + product_total.toFixed( 2 ) );
            
        // On change
        $( '[name=quantity]' ).change( function() {
            if ( ! ( this.value < 1 ) ) {
                product_total = parseFloat( price * this.value );

                $( '#product_total_price .prezzo' ).html( currency + product_total.toFixed( 2 ) );
            }
        });
    });
    </script>
    <?php
}

This code works well and the total price is below "add to cart" button since I gave 31.
Is there a way to put the total price on the "add to cart" button..?
I tried to use (hook: after quantity), but it breaks the page.

maybe I can make a shortcode for this function..?

It is resolved!

I just used different filter, which is this
woocommerce_after_add_to_cart_button

thank you!

Glad to hear you found the answer!

This archived topic is closed to new replies.