How to dynamically get eBay ItemID in your listing template

If you use some JavaScript code in your listing template which requires the current listing's ItemID, you might have noticed that the shortcode [[ebay_item_id]] does not work right away, but only once the listing has been revised at least once. The problem here is that when listing a new item on eBay, WP-Lister doesn't know the ItemID (yet), so it can't populate the shortcode [[ebay_item_id]].

Now, you may have noticed that eBay adds their own javascript snippet at the beginning of the iframe which holds the listing description - where they insert some javascript variable definitions, including this one:

var ebayItemID=110123456789;

Unfortunately, this javascript snippet is not included on their mobile apps, which is why we had to come up with yet another method of fetching that ItemID dynamically.

So let's assume you have a <div> tag somewhere in your listing description which you want to hold the ItemID (it's just an example to demonstrate the code):

<p class="wpl_ebay_item_id_wrapper">eBay ID: [[ebay_item_id]]</p>

Now, this is the code that I added to my listing template's header.php, which first checks if the global variable  ebayItemID is already defined by eBay, and if it's not it fetches the ItemID via AJAX/JSONP and inserts it in the <div> tag above:

<script type="text/javascript">
function wpl_process_ebay_item_id_jsonp(ebayID) {
    // console.log('jsonp: ', ebayID);
    window.ebayItemID = ebayID;
    document.getElementsByClassName('wpl_ebay_item_id_wrapper')[0].innerHTML = ebayID;

if ( typeof window.ebayItemID == 'undefined' ) {
    var script = document.createElement('script');
    script.src = '[[admin_ajax_url]]' + '?action=wpl_ebay_item_query&callback=wpl_process_ebay_item_id_jsonp&id=' + '[[wpl_listing_id]]';

    // or document.head.appendChild(script) in modern browsers
} else {
    wpl_process_ebay_item_id_jsonp( window.ebayItemID );

View script on GitHub:

To use this in your listing template, you'll need to update WP-Lister to version or better, which adds support for the two shortcodes for  admin_ajax_url and wpl_listing_id.

Please note: According to eBay might be going to disallow javascript ("active content") eventually... is sounds as if in 2017, nobody would be allowed to use javascript in their listings anymore. We will have to see whether they will actually go through with this plan, but keep in mind that not all sellers are allowed to use JavaScript and not all eBay sites allow using JavaScript in a listing template.

Still need help? Contact Us Contact Us