Question

I'm trying to build a simple site that will check and print out "Buy It Now Prices" for cars. I can't get the JavaScript push function to print out anything but strings.

The eBay API says that buyItNowPrice returns an Amount.

I have experimented with the other Item functions, and the only ones that are working for me are ones that return a String.

The question is, how should the line var itemPrice = item.buyItNowPrice; be formatted to output a number?

function _cb_findItemsByKeywords(root) {
    var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || [];
    var html = [];
    html.push('<table width="100%" border="0" cellspacing="0" cellpadding="3"><tbody>');
    for (var i = 0; i < items.length; ++i) {
        var item = items[i];
        var title = item.title;
        var pic = item.galleryURL;
        var viewitem = item.viewItemURL;
        var itemPrice = item.buyItNowPrice;
        var timeLeft = item.watchCount;
        if (title != null && null != viewitem) {
            html.push('<tr><td>' + '<img src="' + pic + '" border="1">' + '</td>' +
                '<td><a href="' + viewitem + '" target="_blank">' +
                title + '</a>' // end hyperlink
                +
                '<br>Item Price: ' + itemPrice +
                '<br>Time Remaining: ' + timeLeft +
                '</td></tr>');
        }
    }
    html.push('</tbody></table>');
    document.getElementById("results").innerHTML = html.join("");
}

// Create a JavaScript array of the item filters you want to use in your request
var filterarray = [{
        "name": "MaxPrice",
        "value": "250000",
        "paramName": "Currency",
        "paramValue": "USD"
    },
    {
        "name": "MinPrice",
        "value": "15000",
        "paramName": "Currency",
        "paramValue": "USD"
    },
    //{"name":"FreeShippingOnly", "value":"false", "paramName":"", "paramValue":""},
    {
        "name": "ListingType",
        "value": ["AuctionWithBIN", "FixedPrice", /*"StoreInventory"*/ ],
        "paramName": "",
        "paramValue": ""
    },
];

// Generates an indexed URL snippet from the array of item filters
var urlfilter = "";

function buildURLArray() {
    for (var i = 0; i < filterarray.length; i++) {
        var itemfilter = filterarray[i];
        for (var index in itemfilter) {
            // Check to see if the paramter has a value (some don't)
            if (itemfilter[index] !== "") {
                if (itemfilter[index] instanceof Array) {
                    for (var r = 0; r < itemfilter[index].length; r++) {
                        var value = itemfilter[index][r];
                        urlfilter += "&itemFilter\(" + i + "\)." + index + "\(" + r + "\)=" + value;
                    }
                } else {
                    urlfilter += "&itemFilter\(" + i + "\)." + index + "=" + itemfilter[index];
                }
            }
        }
    }
}
buildURLArray(filterarray);

// Construct the request
var url = "http://svcs.ebay.com/services/search/FindingService/v1";
url += "?OPERATION-NAME=findItemsByKeywords";
url += "&SERVICE-VERSION=1.0.0";
url += "&SECURITY-APPNAME=REDACTED";
url += "&GLOBAL-ID=EBAY-MOTOR";
url += "&RESPONSE-DATA-FORMAT=JSON";
url += "&callback=_cb_findItemsByKeywords";
url += "&REST-PAYLOAD";
//url += "&categoryId=6001";
url += "&keywords=Ferrari 575";
url += "&paginationInput.entriesPerPage=12";
url += urlfilter;

// Submit the request 
s = document.createElement('script'); // create script element
s.src = url;
document.body.appendChild(s);

Was it helpful?

Solution

You are reading the wrong eBay documentation. FindItemsByKeywords is part of the Finding API service. The buyItNowPrice field is found in the item.listingInfo field. Changing the code to the following will output the price.

var itemPrice = '---';
// buyItNowPrice may not be returned for all results.
if(item.listingInfo[0].buyItNowPrice) {
  itemPrice = item.listingInfo[0].buyItNowPrice[0]['@currencyId'] + ' ' + item.listingInfo[0].buyItNowPrice[0].__value__; 
} else if(item.sellingStatus[0].currentPrice) {
  itemPrice = item.sellingStatus[0].currentPrice[0]['@currencyId'] + ' ' + item.sellingStatus[0].currentPrice[0].__value__; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top