Kriuchko
1/8/2019 - 10:27 AM

Price qty change qty and price woocommerce change price by qty

Javascript woocommerce change price by qty

Simple product with one or sale price

—-- JS

function initCountSimpleQtyPrice() {

  var qtyInput = jQuery('.product-type-simple .quantity .qty');
  var countedPrice = jQuery('.sale.product-type-simple .summary .price ins .woocommerce-Price-amount');
  var variationCountedPrice = jQuery('#countedprice');

  if( countedPrice.length == false && variationCountedPrice.length == false ){
    countedPrice = jQuery('.product-type-variable .summary .price .woocommerce-Price-amount');
    qtyInput = jQuery('.product-type-variable .quantity .qty');
  }

  if( countedPrice.length == false ){
    countedPrice = jQuery('.product-type-simple .summary .price .woocommerce-Price-amount');
    qtyInput = jQuery('.product-type-simple .quantity .qty');
  }

  if( qtyInput.length != false && countedPrice.length != false ){

    var priceHtml = countedPrice.html();
    var splitedPriceHtml = priceHtml.split('</span>');

    qtyInput.on( "input",function (e) {

      if( qtyInput.val() > 0 ){

        countPrice = qtyInput.val()*splitedPriceHtml[1];

        countedPrice.html( splitedPriceHtml[0]+countPrice.toFixed(2) );

      }

    });
  }

}

function initCountVariationQtyPrice() {

  var qtyInput = jQuery('.product-type-variable .quantity .qty');
  var variantPrice;
  var countedPrice = jQuery('#countedprice');
  var priceHtml;
  var splitedPriceHtml;

  if( qtyInput.length != false && countedPrice.length != false ){

    priceHtml = countedPrice.html();
    splitedPriceHtml = priceHtml.split('</span>');

    qtyInput.on( "input",function (e) {

      if( qtyInput.val() > 0 ){

        countPrice = qtyInput.val()*splitedPriceHtml[1];

        countedPrice.html( splitedPriceHtml[0]+countPrice.toFixed(2) );

      }

    });
  }

  jQuery( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {

    qtyInput = jQuery('.product-type-variable .quantity .qty');
    variantPrice = jQuery('.product-type-variable .single_variation_wrap .woocommerce-variation-price .woocommerce-Price-amount');
    countedPrice = jQuery('#countedprice');

    priceHtml = variantPrice.html();
    splitedPriceHtml = priceHtml.split('</span>');

    countPrice = qtyInput.val()*splitedPriceHtml[1];

    countedPrice.html( splitedPriceHtml[0]+countPrice.toFixed(2) );

  } );

}
—-- HTML

<p class="price">
    <span class="woocommerce-Price-amount amount">
        <span class="woocommerce-Price-currencySymbol">&pound;</span>100.00
    </span>
</p>

и

<?php if( count( $available_variations ) > 1 ): ?>
  <div class="woocommerce-variation">
    <div class="custom-qtyprice">
          <span class="price">
              <span id="countedprice"></span>
          </span>
      </div>
  </div>
<?php endif; ?>