0

I have a cart page where the quantity is not the correct quantity as the products are sold in bulk (1, 10, 100 items).

What I am trying to achieve is getting the number of items in the pack, multiply by the quantity and display the total number of items.

Here's a JSFiddle and the code:

jQuery(document).ready(function($){
$( ".variant_title" ).each(function() {
  var str = $(this).text();
  var qty = $(this).nextUntil('input[id^=updates_]').val();
  console.log(qty);
  if ( str.indexOf('jackets') > -1 ) {
      unities = str.slice(18,21);
      if (unities === '100' || unities === '10 ') {

        console.log(unities);
        totalUnities = unities * qty;
        console.log(totalUnities); 
        $(this).append('<br />' + totalUnities + ' Unities');
      }
  } else {
      unities = str.slice(18,19);
      if (unities === '1') {
        console.log(unities);
        totalUnities = unities * qty;
        $(this).append('<br />' + totalUnities + ' Unity');
      }
  }
});
});

Table example

enter image description here

My problem now is traversing the DOM and getting the value of the input field next to the description and multiplying.

Does anyone know where am I making a mistake here?

Thanks

  • 2
    Please include all relevant code in the question. If the third party link dies, or becomes unavailable your question becomes un-answerable. – Rory McCrossan Oct 26 '15 at 14:00

2 Answers2

0
  var str = $(this).text();
  var tr = $(this).closest('tr');  
  var qty = $(tr).find('input').val();
  console.log(qty);

Yo need to get your parent element (tr here) and find the input to get your quantity. hope this help

alias_boubou
  • 206
  • 6
  • 11
0

nextUntil(-selector-) method selects all prior elements to -selector-
use this approach to get quantity:

var str = $(this).text();
var qty = $(this).parents('.item').next('td.qty').find("input[id^='updates_']").val();

http://jsfiddle.net/eetyr6ae/3/

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • I did, but I don't have enough points to be able to vote but it says once I do have enough points, the vote will be computed. Thanks RomanPerekhrest. – leandroiwai Oct 27 '15 at 11:41