Marketpress AJAX add to cart callback?

In looking at the ajax-cart.js file, it looks like there's no way to produce a callback (to perform other tasks) without changing ajax-cart.js itself. This functionality would be handy in cases (like the one I'm looking at now) where the cart is hidden from the user's view until something gets added to it. I'd like a way to tell Marketpress "hey when you run your add to cart, also run my custom javascript function."

  • 3SixtyEvolve

    Greetings @murdaugh

    Thank you for your post. I actually wanted to see if anyone else requested something like this on the forum, but didn't find any. So in this case your post would qualify as a feature request, I guess :slight_smile:

    Allow me to move your post to our Feature Suggestions and Feedback Forum and I will ask the Developer for feedback on this.

    Just in case you decide to change the ajax-cart.js itself (until the Developer provides feedback). I had a quick look around to see if I could find something that would help you achieve what you want. I found this useful Ajax function:

    .ajaxSuccess()

    Might be useful to you (not sure) :slight_smile:

    Well, all the best in setting up your site and MarketPress. And please don't hesitate to be in touch should you need further support with the Plugin.

    Have a good weekend!

    Gina

  • webdevcanada

    Hey there, I know this is an old topic, but perhaps this may help someone.

    Here's what I have used to detect a successful "add to cart" ajax call.

    Not sure if its 100% correct, but it works for me:

    $( document ).ajaxSuccess(function( event, xhr, settings ) {
    	var result = xhr.responseText.split('||', 2);
    	if (result[0] == 'error') {
    		// UNSUCCESSFUL ACTION
    	} else {
    		if (result[0] > 0) {
    			// SUCCESSFUL ACTION
    		}
    	}
    });

    And here is what I use if you just want to COUNT the amount of different items in the cart on update. This is useful if you just want to display a small "Checkout: 2 Items" (or something like that) in your website's header in case you don't want to use the shopping cart widget.

    $( document ).ajaxSuccess(function( event, xhr, settings ) {
    	var result = xhr.responseText.split('||', 2);
    	if (result[0] == 'error') {
    		// UNSUCCESSFUL ACTION
    	} else {
    		if (result[0] > 0) {
    			// SUCCESSFUL ACTION
    			var getResult = result[1];
    
                    	// Match the term 'itemscope' for each different product
                    	var matchItems = getResult.match(/itemscope/g);
    
                     	// Use 'matchItems.length' to display the number of items
                    	$("#someDIV").html(matchItems.length);
    		}else{
                    	// CART IS EMPTY - USER EMPTIED CART
                    	$("#someDIV").html(0);
    		}
    	}
    });

    Regards.

  • webdevcanada

    Perhaps a more practical example would be to display the TOTAL QUANTITY of all items.

    $( document ).ajaxSuccess(function( event, xhr, settings ) {
            var result = xhr.responseText.split('||', 2);
            if (result[0] == 'error') {
            } else {
                if (result[0] > 0) {
                    var getResult = result[1];
                    addQTY = 0;
                    $('.mp_cart_col_quant',getResult).each(function(e) {
                        if (parseInt($(this).html())){
                            addQTY = addQTY+parseInt($(this).html());
                        }
                    });
                    $("#ItemsInCart").html(addQTY);
                }else{
                    $("#ItemsInCart").html(0);
                }
            }
    });

    Then wherever you want to display this streamlined cart, you would use something like this:

    <span id="ItemsInCart"><? echo mp_items_count_in_cart(); ?></span>

    This will ensure that the total number of items show on page load / reload.

    Regards.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.