Display the total price on the WooCommerce single product page with the original product price * minimum quantity

I'm looking to create a PHP code snippet that display the total price on the WooCommerce single product page with the original product price * minimum quantity

Based on WooCommerce - auto update total price when quantity changed answer code, this is what i have so far:

add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
    global $woocommerce, $product; 
    // let's setup our divs 
    echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Prezzo Totale:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
    ?>
    <script>
        jQuery(function($){
            var price = <?php echo $product->get_price(); ?>,
                currency = '<?php echo get_woocommerce_currency_symbol(); ?>';

            $('[name=quantity]').change(function(){
                if (!(this.value < 1)) {

                    var product_total = parseFloat(price * this.value);

                    $('#product_total_price .price').html( currency + product_total.toFixed(2));

                }
            });
        });
    </script>
    <?php
}

The problem is that it shows the price for the quantity of one product when viewing the single product page. The change event occurs when the quantity has been changed, therefore no calculations are performed before this event has taken place.

I have a B2B website, so when customers view the single product page, they immediately see the minimum quantity, so how can I get the total price right away for the minimum quantity for each product?

Solutions

$product->get_price() will return price for 1 product.

You can use get_min_purchase_quantity() and multiply into price for first time.

add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
    global $woocommerce, $product; 
    // let's setup our divs 
$price = $product->get_price();
$min_qnty = $product->get_min_purchase_quantity();
$initial_price = floatval($price) * intval ($min_qnty);
    echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Prezzo Totale:','woocommerce'),'<span class="price">'.$initial_price.'</span>');
    ?>
    <script>
        jQuery(function($){
            var price = <?php echo $product->get_price(); ?>,
                currency = '<?php echo get_woocommerce_currency_symbol(); ?>';

            $('[name=quantity]').change(function(){
                if (!(this.value < 1)) {

                    var product_total = parseFloat(price * this.value);

                    $('#product_total_price .price').html( currency + product_total.toFixed(2));

                }
            });
        });
    </script>
    <?php
}

The following code takes into account the product quantity when loading the page, then the price is updated when changing the product quantity.

Explanation via comment tags added in the code

function action_woocommerce_single_product_summary() {
    global $product;
    
    // Getters
    $price = $product->get_price();
    $currency_symbol = get_woocommerce_currency_symbol();
    
    // let's setup our div
    echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>', __('Product Total:','woocommerce'), '<span class="price">' . $currency_symbol . $price . '</span>' );
    ?>
    <script>
    jQuery(function($) {
        // jQuery variables
        var price = <?php echo $price; ?>, 
            currency = '<?php echo $currency_symbol; ?>',
            quantity =  $( '[name=quantity]' ).val();
            
        // Code to run when the document is ready
        var product_total = parseFloat( price * quantity );
        $( '#product_total_price .price' ).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 .price' ).html( currency + product_total.toFixed( 2 ) );
            }
        });
    });
    </script>
    <?php

}
// We are going to hook this on priority 31, so that it would display below add to cart button.
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 31, 0 );

Similar questions

get minimum price variation and maximum price variation according to specific category woocommerce
I want to get minimum price and maximum price based on categories. The woocommerce query which gives me minimum and maximum product price range but i want it on the bases of category. for example: category="music,clothing". Here is query for minimum and maximum price: Please suggest me how can i get it according to category selected.
Resize uploaded original images to a minimum automatically
Facebook recommends to use for sharing images with a minimum size of 600 x 315 pixels and an 1.91:1 aspect ratio. Is there a way to automatically resize down original images uploaded to Wordpress to a size as close as possible to the recommended? I tried some plugins for resizing, but if images will not have the recommended 1.91:1 aspect ratio, one...
Woocommerce minimum order by category/quantity
I'm wondering if it would be possible to create an if statement where if a customer hasn't added enough (quantity - not price) of a specific product category to their order, a message shows up saying they need to add more to avoid a surcharge. I'm thinking something along the lines of the minimum order amount snippet documented here: Any help would...
Progressive minimum cart items quantity count limitation in Woocommerce
In Woocommerce, I would like to set some requirements to be approved before customer to be able to checkout. I would like customer to have quantity of 6 before they can proceed to checkout. Quantity is not related just to one product, they can combine more products until they reach total quantity. If have exactly 6 , then can proceed. Next stage, m...
Set minimum input quantity even on ajax add to cart in Woocommerce
I have added the below code on my function.php (minimum as 6 qty for every product) it is reflect on my product category page… But while clicking the add to cart button (Ajax) the cart page only have 1 qty not 6 qty… How to fix this? Need qty 6 on cart page for each product.
Setting up an error message for minimum quantity in WooCommerce
I set up a minimum quantity for a product category in WooCommerce. In my case I set the minimum quantity to 5 items. The code I use works fine but I would like to add two error messages for the customer: 1) If the customer tries to change the the quantity to less than the minimum by clicking the "-" symbol I would like to have something like: "The ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .