• Resolved jcweb

    (@jcphoto)


    Hello everyone,

    I need help with woocommerce plugin. At the product pages I want the price to be changed instantly when someone adds more quantity “for example 2”. It shows the right price on the cart page. Is it possible? Please help.

    https://tinyurl.com/yzolnjyo

    • This topic was modified 3 years, 6 months ago by jcweb.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support abwaita a11n

    (@abwaita)

    Hi @jcphoto,

    This will require some custom coding.

    Kindly try the following code that I found and adapted from Stack Overflow (https://stackoverflow.com/a/35691378/6438880):

    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="price">'.wc_get_price_to_display($product).'</span>');
        ?>
            <script>
                jQuery(function($){
                    var price = <?php echo wc_get_price_to_display($product); ?>,
                        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
    }

    I hope this helps.
    Thanks.

    Thread Starter jcweb

    (@jcphoto)

    Hi @abwaita thanks for your help, but it’s not working!

    Plugin Support abwaita a11n

    (@abwaita)

    Hi @jcphoto,

    Thanks for getting back.

    The code is working well on my end, so it might be an issue with the way you’ve added it to your site.

    Could you try adding it using the Code Snippets plugin?

    If it still does not work, kindly share with us a screenshot of where it’s added so that we can check further.
    * I recommend https://snipboard.io for sharing screenshots here.

    Thanks.

    Thread Starter jcweb

    (@jcphoto)

    Hi @abwaita, Thanks it is working now. But going by the same process it is not working on another website. Please check as I have added the code same way.

    • This reply was modified 3 years, 6 months ago by jcweb.
    Plugin Support abwaita a11n

    (@abwaita)

    Glad to hear that it works!

    On the second site, I see that the product page template is created using Elementor and a few things are not shown after the add to cart button such as SKU, category, tags, etc.

    Thus, in the shared code above, could you try targeting another hook, perhaps woocommerce_after_add_to_cart_quantity instead of woocommerce_single_product_summary?

    Thanks.

    Thread Starter jcweb

    (@jcphoto)

    https://snipboard.io/rywcou.jpg please check ??

    Also, it doesn’t work for the variation prices.

    • This reply was modified 3 years, 6 months ago by jcweb.
    Plugin Support Stuart Duff – a11n

    (@stuartduff)

    Automattic Happiness Engineer

    Hey @jcphoto,

    Testing the code which @abwaita referenced within this reply on a default installation of WooCommerce it does indeed work correctly to dynamically update the product prices on a quantity change.

    When testing the code snippet I used a the latest version of WordPress, WooCommerce and the free Storefront theme as the only enabled software.

    Image Link: https://cloudup.com/cvz__klliN1

    If the code is not working for you on your particular site for simple products the two possibilities for that are thee below.

    • The code has been incorrectly added to the site.
    • The WordPress theme your using is preventing the code from working.

    With regards to variations, the price on those is displayed differently. This would then require a different approach to development.

    It may be possible to have a developer write some custom code that could achieve this on variable products though. If you would like to investigate that possibility further, you could reach out to some of the official WooCommerce development partners via the link below.

    https://woocommerce.com/customizations/

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Help in Woocommerce’ is closed to new replies.