• Hi there,

    Would it be possible to change the price on the product page according to the quantity?

    So when minimum quantity = 2 and price per piece = 75 than total price = 150 ?

    Currently the total price is only displayed in the Shopping cart

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author WPClever

    (@wpclever)

    Hi @jeanp

    Please add the below snippet (How to add custom code?) and let me know if it works or not.

    add_action( 'wp_enqueue_scripts', 'woopq_add_localize', 99 );
    function woopq_add_localize() {
    	wp_localize_script( 'woopq-frontend', 'woopq_vars', array(
    			'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()
    		)
    	);
    }
    
    add_action( 'wp_footer', 'woopq_change_price_js', 99 );
    function woopq_change_price_js() {
    	?>
        <script type="text/javascript">
          jQuery(document).on('change', 'form.cart .qty', function() {
            var $this = jQuery(this);
            var $this_product = $this.closest('.product');
            var $this_price = $this_product.find('.summary .price');
            var qty = parseFloat($this.val());
    
            if ($this_product.attr('data-price') === undefined) {
    
              if ($this_price.find('ins').length) {
                var regular_price = parseFloat($this_price.find('del').text().replace(',', '.').replace(/[^0-9.]/g, ''));
                var price = parseFloat($this_price.find('ins').text().replace(',', '.').replace(/[^0-9.]/g, ''));
              } else {
                var regular_price = 0;
                var price = parseFloat($this_price.text().replace(',', '.').replace(/[^0-9.]/g, ''));
              }
    
              $this_product.attr('data-price', price);
              $this_product.attr('data-price-regular', regular_price);
            } else {
              var price = parseFloat($this_product.attr('data-price'));
              var regular_price = parseFloat($this_product.attr('data-price-regular'));
            }
    
            if ((qty > 0) && (price > 0)) {
              if (regular_price > 0) {
                $this_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));
                $this_price.find('ins').html(
                    woopq_format_money(qty * price, woopq_vars.price_decimals, woopq_vars.currency_symbol,
                        woopq_vars.price_thousand_separator,
                        woopq_vars.price_decimal_separator));
              } else {
                $this_price.html(
                    woopq_format_money(qty * 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) : ''
            );
          }
        </script>
    	<?php
    }
Viewing 1 replies (of 1 total)

The topic ‘Price changes with quantity’ is closed to new replies.