How to use dynamic categories in your eBay listing

To embed dynamic eBay store categories in your listing template, you can use the example code snippet below and tweak it according to your needs.

Add a <div id="categories_wrapper">Categories</div> somewhere in your listing template, add the following snippet to the header.php or footer.php section and replace "www.example.com" with your domain:

<script async type="text/javascript"> 
 
function loadDynamicCategories() {

    $.getJSON('http://www.example.com/wp-admin/admin-ajax.php?action=wpl_ebay_store_categories&callback=?', function(data) {
        $('#categories_wrapper').append('<ul></ul>');
        $.each(data, function(index) {
    		$('#categories_wrapper ul').append('<li id="'+data[index].cat_id+'"><a href="'+data[index].url+'">'+data[index].cat_name+'</a></li>');
    	});
    });

    // add optional link to store home page at the top
    $('#categories_wrapper ul').prepend('<li><a href="'+'[[ebay_store_url]]'+'">Store Home</a></li>');

}

function loadjQuery() {
    var head    = document.getElementsByTagName("head")[0];
    var script  = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js";
    head.appendChild(script);
    waitUntiljQueryIsLoaded();
}

function waitUntiljQueryIsLoaded() {
    if ( typeof window.jQuery !== 'undefined' && window.jQuery ) {
        if (typeof jQueryLoaded == 'function') {
            jQueryLoaded();
        }
    } else {
        window.setTimeout( waitUntiljQueryIsLoaded, 100 );
    }
}

// payload - executed when jQuery has been loaded
function jQueryLoaded() {
    jQuery(document).ready(function(){
       
        // load categories
        loadDynamicCategories();
       
    });     
}

// init
loadjQuery();

</script>

View script on GitHub: https://gist.github.com/wp-lab/58ef11c28a0187db7104

This will first load jQuery from the Cloudflare CDN, then load your categories via AJAX from your site and add them as an unordered list (<ul><li>) to the wrapper <div> tag.

Please note that while many sellers and even third party services use external javascript libraries like jQuery in their eBay listings, the official eBay listing policy still states that loading external javascript code is prohibited. So use it on your own risk.

For more information on that subject see  this post on lastdropofink.co.uk. The code above is partly based on their implementation.

Still need help? Contact Us Contact Us