Im currently having a problem with ajax and magento. I'm trying to get the current amount of items in a cart, after the user has clicked on the "put in cart"-button of an product, without reloading the page.
So far I've tried it with a poorly written Ajax, which hasn't worked, because I'm an absolute beginner with Ajax.
The process should be following:
User clicks on "add to cart" -> script requests new cart quantity with ajax -> jquery adds the current amount into another DOM-Element (lets name it #cartqty)
My current non-ajax solution doesn't work, which looks like this:
$( '.product-add-btn' ).click(function() {
$( '#cartqty' ).html('<?php echo $this->helper('checkout/cart')->getSummaryCount(); ?>');
});
The problem is NOT how to add a product with ajax to the cart. The problem is how to get the current amount of items in the cart with ajax after a product has been added to the cart, just for clarification.
My current code just gives back the first known cart item quantity, which is say 4. When I add something again, the quantity doesn't update, it just stays at 4, even if there are 5 products in the cart.
I really hope some can help a beginner.
Best Answer
Call
an ajax
onbutton click
to new url.Get data from that New Url.so you need to
new Action on exit any controller
orcreate new controller and get data
.<script> $( '.product-add-btn' ).click(function() { new Ajax.Request(url, { method:'get', onLoading:function(){ }, onSuccess: function(transport) { if (200 == transport.status){ var response = transport.responseText.evalJSON( ); $( '#cartqty' ).html(response.count ); }else{ alert('Something went wrong...'); } }, onFailure: function() { alert('Something went wrong...'); } }); }); </script>
Need to create a controller by create new module:
Full module:
Amit_Custommodule.xml at app/etc/modules/
.Code:
config.xml
isapp/code/community/Amit/Custommodule/etc
Code:
app/code/community/Amit/Custommodule/controllers/
Code:
And script: