rayvic
rayvic

Reputation: 105

Changing Price Total With Quantity

I am working in the order form field in woocommerce. I am not very good in coding but i have found a code to change price when quantity changes.

The product price vary depending on the user selection.

The problem is total Price increment or decrement +10/-10 in product Total field when change in quantity field.

Eg: The product price after selection is 35 but when increasing the quantity instead of multiplying by 2 with the total price, its just showing 10 then 20 then 30 then 40(adding 10 when increment quantity field.)

I have also add pictures for better understanding.

Any help .?

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>',__('Product Total:','woocommerce'),'<span class="price1">'.$product->get_price().'</span>');
?>
    <script>
        jQuery(function($){
            var price1 = <?php echo $product->get_price(); ?>,
                currency = '<?php echo get_woocommerce_currency_symbol(); ?>';

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

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

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

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

enter image description here

Upvotes: 0

Views: 1440

Answers (1)

osama
osama

Reputation: 71

try this for cvariable products

// Variable Products

add_filter( 'woocommerce_available_variation', 'variable_product_total_amount_qty_change', 10, 3 );
function variable_product_total_amount_qty_change( $data, $product, $variation ) {

    $data['price_html'] .= '<div id="product_total_price" class="total-price-qty variable_products">Product Total: <span class="price">'.$data['display_price'].'</span></div>';
?>
    <script>
   jQuery(function($){
       jQuery( '.variations_form' ).each( function() {
    jQuery(this).on( 'found_variation', function( event, variation ) {
        console.log(variation);//all details here
var price = variation.display_price;//selectedprice
        console.log(price);
var 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').fadeIn();
 $('#product_total_price .price').html( currency + product_total.toFixed(2));
                } else {
 $('#product_total_price').fadeOut();
                }
        });
                });
            });
        });  

    </script>
<?php

return $data;
}

Upvotes: 2

Related Questions