Please submit a private ticket if you need to share sensitive information, such as license details and admin credentials.

Okay
  Public Ticket #3767766
Price on product page is not adjusted, only in cart
Open

Comments

  •  2
    Ellisabeth started the conversation

    Hi, the Price on the product page is not adjusted when the product quantity is changed. Only in the cart when you open that you see the new price.

    I use the Divi theme and Divi theme builder, but when I disable the Divi theme builder template, the price is still not adjusted on the product page.

    If not, how can fix this? It is rather urgent...


    Thanks, Elisabeth

  •  1,281
    Dustin replied

    Hi Ellisabeth,

    By default, the price isn't changed when the quantity is changed. Buyers will be confused because they don't know whether it is the total or a new price.

    If you still want that, please add the custom code below. (How to add custom code? ↗):

    add_action( 'wp_enqueue_scripts', function () {
    	wp_localize_script( 'woopq-frontend', 'woopq_vars', [
    			'price_format'             => get_woocommerce_price_format(),
    			'price_decimals'           => wc_get_price_decimals(),
    			'price_thousand_separator' => wc_get_price_thousand_separator(),
    			'price_decimal_separator'  => wc_get_price_decimal_separator(),
    			'currency_symbol'          => get_woocommerce_currency_symbol()
    		]
    	);
    }, 99 );
    
    add_action( 'wp_footer', function () {
    	if ( ! is_product() ) {
    		return;
    	}
    	?>
        <script type="text/javascript">
          (function($) {
            $(function() {
              // ready
              if (!$('.variations_form').length) {
                var $price = $('.summary > .price');
                var $product = $price.closest('.product');
                var sale_price = 0;
                var regular_price = 0;
    
                if ($price.find('ins').length) {
                  regular_price = parseFloat($price.find('del').text().replace(',', '.').replace(/[^0-9.]/g, ''));
                  sale_price = parseFloat($price.find('ins').text().replace(',', '.').replace(/[^0-9.]/g, ''));
                } else {
                  sale_price = parseFloat($price.text().replace(',', '.').replace(/[^0-9.]/g, ''));
                }
    
                $product.attr('data-price', sale_price);
                $product.attr('data-price-regular', regular_price);
    
                woopq_update_price();
              }
            });
    
            $(document).on('change', 'form.cart .qty', function() {
              woopq_update_price();
            });
    
            $(document).on('found_variation', function(e, t) {
              let $product = $(e['target']).closest('.product');
    
              $product.attr('data-price', t.display_price);
              $product.attr('data-price-regular', t.display_regular_price);
    
              woopq_update_price();
            });
    
            function woopq_update_price() {
              var $qty = $('form.cart .qty');
              var qty = parseFloat($qty.val());
              var $product = $qty.closest('.product');
              var $price = $product.find('.summary > .price');
    
              if ($(document).find('.woocommerce-variation-price > .price').length) {
                $price = $(document).find('.woocommerce-variation-price > .price');
              }
    
              var sale_price = parseFloat($product.attr('data-price'));
              var regular_price = parseFloat($product.attr('data-price-regular'));
    
              if ((qty > 0) && (sale_price > 0)) {
                if (regular_price > sale_price) {
                  $price.find('del').html(
                      woopq_format_money(qty * regular_price, woopq_vars.price_decimals, woopq_vars.currency_symbol,
                          woopq_vars.price_thousand_separator,
                          woopq_vars.price_decimal_separator));
                  $price.find('ins').html(
                      woopq_format_money(qty * sale_price, woopq_vars.price_decimals, woopq_vars.currency_symbol,
                          woopq_vars.price_thousand_separator,
                          woopq_vars.price_decimal_separator));
                } else {
                  $price.html(
                      woopq_format_money(qty * sale_price, woopq_vars.price_decimals, woopq_vars.currency_symbol,
                          woopq_vars.price_thousand_separator,
                          woopq_vars.price_decimal_separator));
                }
              }
            }
    
            function woopq_format_money(number, places, symbol, thousand, decimal) {
              number = number || 0;
              places = !isNaN(places = Math.abs(places)) ? places : 2;
              symbol = symbol !== undefined ? symbol : '$';
              thousand = thousand || ',';
              decimal = decimal || '.';
    
              var negative = number < 0 ? '-' : '',
                  i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + '',
                  j = 0;
    
              if (i.length > 3) {
                j = i.length % 3;
              }
    
              return symbol + negative + (
                  j ? i.substr(0, j) + thousand : ''
              ) + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) + (
                  places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : ''
              );
            }
          })(jQuery);
        </script>
    	<?php
    }, 99 );

    Let me know if it works as you expected.

    Best regards,
    Dustin

  •  2
    Ellisabeth replied

    Hi Dustin,

    1. The code only works when I disable the Divi theme builder template. This is a pity, but not a big problem.

    2. The currency symbol has changed from € to $   Our default currency is €  and the product price needs to be in € too. I looked at the code you sent and made a small change (see screenshot in link here: https://prnt.sc/dATgzExD0BqK). I adjusted the dollar symbol in this line to the euro symbol.

    That seems to work fine,  but can you confirm this is correct or please adjust the code correctly for me?

    Thank you, Elisabeth