Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Total up the cost of all items in an Amazon wishlist
var list = $$("#item-page-wrapper .list-items table tbody .lineItemMainInfo .lineItemPart strong");
var total=0;
for(i=0; i<list.length;i++){
total += parseFloat(list[i].innerText.replace("$",""));
}
alert(list.length+" items for a total of: $"+total.toFixed(2));
@stevenpetryk

This comment has been minimized.

Copy link

@stevenpetryk stevenpetryk commented May 5, 2013

Wow, funny how this was exactly what I needed. Thanks!

@Dae314

This comment has been minimized.

Copy link

@Dae314 Dae314 commented May 29, 2013

var list = $$("#item-page-wrapper .list-items table tbody .lineItemMainInfo .lineItemPart strong");
var total=0;
var numHidden = 0;
var temp=0;
for(i=0; i<list.length;i++){
  temp = parseFloat(list[i].innerText.replace("$",""));
  numHidden += isNaN(temp);
  total += isNaN(temp) ? 0 : temp;
}
alert(list.length - numHidden +" items for a total of: $"+total.toFixed(2) + " less " + numHidden + " Hidden Items");

I tried this script just now, but some hidden price items on my list borked it. I put a quick fix in to ignore the hidden items and added a little text at the end to report how many hidden items were found.

@yrrah

This comment has been minimized.

Copy link

@yrrah yrrah commented Jul 16, 2013

I fixed this since it wasn't working for me.

It takes the bolded price or the "used and new" price if that is absent, then multiplies by quantity desired.

var total = 0;
var itemCount = 0;
var allItems = jQuery('tbody').filter(function() 
    {
        if (typeof this.attributes[0] !== "undefined")
        return this.attributes[0].nodeValue.match(/item.[0-9]+/);
    });
for(var i=0;i<allItems.length;i++){
    var price = 0;
    var pString = allItems.eq(i).find(".wlPriceBold").text();
    var quantity =0;
    var qString = allItems.eq(i).find(".quantityValueText").text();     
    if(qString==" ")quantity=1;else quantity = parseInt(qString); 
    if(pString.indexOf("$") !== -1){
        price = parseFloat(pString.replace("$","")); 
    }else{
        pString = allItems.eq(i).find(".wlPrice").text();
        if(pString.indexOf("$") !== -1){
            price = parseFloat(pString.replace("$","")); 
        }else{
            return;
        }
    }
    total += price*quantity;
    itemCount += quantity;
}
alert(itemCount+" items for a total of: $"+total.toFixed(2));
@krustygstring

This comment has been minimized.

Copy link

@krustygstring krustygstring commented Dec 20, 2013

Very nice, just what I was looking for thanks. In case it's any use I made a teeny modification to allow for sterling or other currency symbols for people using amazon.co.uk (tested and working on that) etc.

var total = 0;
var itemCount = 0;
var currencySymbol="£";
var allItems = jQuery('tbody').filter(function() 
    {
        if (typeof this.attributes[0] !== "undefined")
        return this.attributes[0].nodeValue.match(/item.[0-9]+/);
    });
for(var i=0;i<allItems.length;i++){
    var price = 0;
    var pString = allItems.eq(i).find(".wlPriceBold").text();
    var quantity =0;
    var qString = allItems.eq(i).find(".quantityValueText").text();     
    if(qString==" ")quantity=1;else quantity = parseInt(qString); 
    if(pString.indexOf(currencySymbol) !== -1){
        price = parseFloat(pString.replace(currencySymbol,"")); 
    }else{
        pString = allItems.eq(i).find(".wlPrice").text();
        if(pString.indexOf(currencySymbol) !== -1){
            price = parseFloat(pString.replace(currencySymbol,"")); 
        }else{
            return;
        }
    }
    total += price*quantity;
    itemCount += quantity;
}
alert(itemCount+" items for a total of: " +currencySymbol+total.toFixed(2));
@smerchek

This comment has been minimized.

Copy link

@smerchek smerchek commented Jan 15, 2014

Obviously, these are all pretty fragile. the others didn't work, but I ended up using the following selector and original loop.

var list = $$("span.a-color-price.a-size-base");
var total=0;
for(i=0; i<list.length;i++){
  total += parseFloat(list[i].innerText.replace("$",""));
}
alert(list.length+" items for a total of: $"+total.toFixed(2));
@heathdutton

This comment has been minimized.

Copy link

@heathdutton heathdutton commented Jan 30, 2014

Let's not forget to support items costing over 999 ;)

var list = $$("span.a-size-base.a-color-price");
var total=0;
for(i=0; i<list.length;i++){
  total += parseFloat(list[i].innerText.replace("$","").replace(",",""));
}
alert(list.length+" items for a total of: $"+total.toFixed(2));
@sensecall

This comment has been minimized.

Copy link

@sensecall sensecall commented Jun 5, 2014

Updated to work on amazon.co.uk, with '£' rather than dollars:

var list = $$("span.a-size-base.a-color-price");
var total=0;
var numHidden = 0;
var temp=0;
for(i=0; i<list.length;i++){
  temp = parseFloat(list[i].innerText.replace("£",""));
  numHidden += isNaN(temp);
  total += isNaN(temp) ? 0 : temp;
}
alert(list.length+" items for a total of: £"+total.toFixed(2));
@LukeChannings

This comment has been minimized.

Copy link

@LukeChannings LukeChannings commented Jun 15, 2014

I think I'll throw my contribution in here. I've written a function that is currency agnostic, supports quantities, and has an option for suffixed currency symbols.

I've tested on amazon.co.uk and amazon.com. Unfortunately for wishlists with > 1 page this is still not sufficient. (Should be possible to XHR the next page and run the same fn though.

/**
 * calculate the total cost of Amazon wishlist items.
 * @param  {Bool} doAlert if true an alert message will be shown with the total.
 *                        (false by default)
 * @param  {Bool} suffixCurrencySymbol true if the currency symbol
 *                                     occurs after the value. e.g. 5.50$ vs $5.50
 * @param  {Bool} useLowerInRange if true the lower price in a range
 *                                e.g. $5.99 - $8.99 will be used. (higher by default)
 * @return {String}   the total cost to two decimal places
 */
function amazonWishListTotalCost(doAlert, suffixCurrencySymbol, useLowerInRange) {

  var totalPrice = 0
    , currencySymbol = ''
    , itemPrices = document.querySelectorAll('[id*=itemPrice_]')
    , itemMultipliers = document.querySelectorAll('[id*="itemRequested_"]')
    , currentPrice, currentMultiplier, i

  for (i = 0; i < itemPrices.length; i += 1) {

    currentPrice = itemPrices[i].innerText

    if (~currentPrice.indexOf('-')) {
      currentPrice = currentPrice.split('-') [useLowerInRange? 0 : 1]
    }

    if (!currencySymbol) {
      currencySymbol = suffixCurrencySymbol? currentPrice.charAt(currentPrice.length - 1)
                                           : currentPrice.charAt(0)
    }

    currentPrice = ''.slice.apply(currentPrice, suffixCurrencySymbol ? [0,-1] : [1])
    currentMultiplier = itemMultipliers[i] ? itemMultipliers[i].innerHTML : '1'
    totalPrice += parseFloat(currentPrice) * parseInt(currentMultiplier, 10)
  }

  totalPrice = totalPrice.toFixed(2)

  if (doAlert) alert(totalPrice)

  return currencySymbol + totalPrice
}
@Woundorf

This comment has been minimized.

Copy link

@Woundorf Woundorf commented Dec 4, 2014

A small improvement over the previous one. This one works with Firefox (uses textContent instead of innerText), has a few customizable variables at the top (could be converted to parameters), and allows currencies of more than one character. It's currently customized for amazon.es.

/**
 * calculate the total cost of Amazon wishlist items.
 * @param  {Bool} doAlert if true an alert message will be shown with the total.
 *                        (false by default)
 * @param  {Bool} suffixCurrencySymbol true if the currency symbol
 *                                     occurs after the value. e.g. 5.50$ vs $5.50
 * @param  {Bool} useLowerInRange if true the lower price in a range
 *                                e.g. $5.99 - $8.99 will be used. (higher by default)
 * @return {String}   the total cost to two decimal places
 */
function amazonWishListTotalCost(doAlert, suffixCurrencySymbol, useLowerInRange) {
  var currencySymbol = 'EUR ';  // edit as needed
  var thousandsSeparator = '.'; // edit as needed
  var decimalSeparator = ',';   // edit as needed

  var totalPrice = 0;
  var itemPrices = document.querySelectorAll('[id*=itemPrice_]');
  var itemMultipliers = document.querySelectorAll('[id*="itemRequested_"]');

  for (var i = 0; i < itemPrices.length; i += 1) {
    var currentPrice = itemPrices[i].textContent;

    if (~currentPrice.indexOf('-')) {
      currentPrice = currentPrice.split('-') [useLowerInRange? 0 : 1];
    }

    // Replace special characters to match parseFloat() format and trim spaces at both ends
    currentPrice = currentPrice.replace(thousandsSeparator, '').replace(decimalSeparator, '.').trim();

    if (!currencySymbol) {
      currencySymbol = suffixCurrencySymbol? currentPrice.charAt(currentPrice.length - 1)
                                           : currentPrice.charAt(0);
    }

    currentPrice = ''.slice.apply(currentPrice, suffixCurrencySymbol ? [0,-currencySymbol.length] : [currencySymbol.length]);
    var currentMultiplier = itemMultipliers[i] ? itemMultipliers[i].innerHTML : '1';
    totalPrice += parseFloat(currentPrice) * parseInt(currentMultiplier, 10);
  }

  totalPrice = totalPrice.toFixed(2);

  if (doAlert) alert(totalPrice);

  return currencySymbol + totalPrice;
}
@ryanjm

This comment has been minimized.

Copy link

@ryanjm ryanjm commented May 8, 2015

Made it a bookmarklet:

javascript: (function(){
var list = document.querySelectorAll('span.a-color-price.a-size-base');
var total=0;
for(i=0; i<list.length;i++){
  total += parseFloat(list[i].innerText.replace("$",""));
}
alert(list.length+" items for a total of: $"+total.toFixed(2));
}() );
@mayel

This comment has been minimized.

Copy link

@mayel mayel commented Aug 14, 2015

Fixed it to work when any items are unavailable:

var list = $$("span.a-size-base.a-color-price");
var total=0;
for(i=0; i<list.length;i++){
  var n = parseFloat(list[i].innerText.replace("$","").replace(",",""));  
  if(n === Number(n)) total += parseFloat(n);
}
alert(list.length+" items for a total of: $"+total.toFixed(2));
@imemohit

This comment has been minimized.

Copy link

@imemohit imemohit commented Jul 29, 2017

For amazon.in, the following works for me:

var list = $$('.price-section');
var total=0;
for(i=0; i<list.length;i++){
  total += (parseFloat(list[i].innerText.replace(',','')));
}
alert(list.length+" items for a total of: Rs. "+total.toFixed(2));
@mrworf

This comment has been minimized.

Copy link

@mrworf mrworf commented Sep 14, 2017

Here's my $0.05

var list = $$('.a-price'); var total=0; for(i=0; i<list.length;i++){ total += parseFloat(list[i].innerText.split("\n")[0].substring(1).replace(',','')); } "Total: $" + Math.ceil(total);

Paste that into the console with the relevant whishlist visible on Amazon.com and it will give you the total (rounded up to nearest dollar)

CAVEAT
If you don't scroll to the bottom before running it, you'll only get whatever is visible on screen due to dynamic loading amazon now uses.

@jmverges

This comment has been minimized.

Copy link

@jmverges jmverges commented Feb 17, 2018

amazon.es

var list = $$(".a-offscreen");
var total=0;
var numHidden = 0;
var temp=0;
for(i=0; i<list.length;i++){
temp = parseFloat(list[i].innerText.replace(" €","").replace(',','.'));
numHidden += isNaN(temp);
total += isNaN(temp) ? 0 : temp;
}
console.log(list.length - numHidden +" items for a total of: "+total.toFixed(2) + "€ less " + numHidden + " Hidden Items");

@gabesantos1

This comment has been minimized.

Copy link

@gabesantos1 gabesantos1 commented Nov 5, 2018

amazon br

var list = $$('.a-price'); var total=0;

for (var i = 0; i < list.length; i++) {
	total += parseFloat(list[i].innerText.split("R$")[1].replace(',','.')) ;
}

console.log("Total = " + total);
@silasjmatson

This comment has been minimized.

Copy link

@silasjmatson silasjmatson commented Apr 24, 2019

You can use the data-price attribute on the li. Tested on amazon com.

$$("li[data-price]").reduce((amount, li) => amount + parseFloat(li.getAttribute('data-price'), 0), 0.0)
@namero999

This comment has been minimized.

Copy link

@namero999 namero999 commented Sep 16, 2020

This version takes into account elements without a price (such as items not available)

$$("li[data-price]").reduce((amount, li) => amount + Math.max(0, parseFloat(li.dataset['price'])), 0.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.