Support » Plugin: WP eCommerce » How to use AJAX to update Shopping Cart Item Count

  • WPEC
    Gold Cart 2.9.7

    I know, I’m missing probably very basics of PHP and AJAX and this might be a lame question. Never mind, I’ve got following JS/jQuery code to invoke an AJAX when visitor clicks any of the Add to Cart buttons

    // Update the number by shopping cart icon when item is added to the cart
    function itemCount() {
    $.ajax( {
    url : "cart-count.php",
    type : "POST",
    data : "id=1",
    success : function(msg) {
    alert (msg); //or use data string to show something else
    $(document).on('click', 'input.wpsc_buy_button', itemCount );

    I also have following PHP code

    $cart_count = wpsc_cart_item_count();

    How can I call the PHP code via AJAX and store its result in a JS variable, so I can place it somewhere in front-end of header of my website?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    Did you figure it out how to fix it on your header? I managed to get it working but when you add new items the number doesn’t update until you go to another page or hit refresh.

    Here is the code I used on my header.php <?php echo wpsc_cart_item_count(); ?>



    I got as far as you did, but it wouldn’t update automatically on click on the ‘Add to Cart’ button.

    So in the end, I took the advantage of using the AJAX functionality in the shopping cart widget and I used jQuery to listen for click on ‘Add to Cart’ button and then I count the number of rows in the shopping cart table.

    It’s kind of hack, but it will have to do now. It’s a shame that the WPEC support is so vague or should I say virtually non-existent.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use AJAX to update Shopping Cart Item Count’ is closed to new replies.