I have a helper that in the click of the button, according to the id of the product it adds the same to the stand.
But I need to do the reverse, a function that removed a product from the cart according to its product id on button click.
I search a lot about it, but I still did not find anything that could help me do the function to remove the product from the cart.
Edit: I tried this function below, but it removes all the products that are in the cart, not just the product according to its id.
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item)
{
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
}
Note: The above code is in a controller.
Ajax request code:
function removeCartaotoCart(){
$j.ajax({
type:"POST",
url:"<?php echo Mage::getUrl('fol_carousel/ajax/removeCartao') ?>",
data:{
},
cache: false,
beforeSend: function(){
alert("beforeSend");
},
success: function(){
alert("Sucess removeCartao");
},
complete: function () {
alert("complete removeCartaotoCart");
},
error: function (x,y,z) {
alert("error");
alert(x);
alert(y);
alert(z);
window.location.reload();
history.go(0);
window.location.href=window.location.href;
}
});
}
Edit 2:
Button code:
<button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="addCartao('<?php echo $_product->getId(); ?>')" name="cartaoMensagem<?php echo $_product->getId(); ?>"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
Ajax request that adds the product to the cart and change the button text / Address the second request of which the code is above (this request is not so important in this question, but for information, I also put it):
function addCartao(product_id){
$j.ajax({
type: "POST",
url: "<?php echo Mage::getUrl('fol_carousel/ajax/addCartao') ?>",
data: {
product_id: product_id
},
dataType: 'json',
cache : false,
beforeSend: function () {
},
success: function (retorno) {
var button = $j('button[name=cartaoMensagem' + product_id + ']');
if($j(button).text() == 'Comprar'){
$j('#cartao').find(':button').not(button).attr('disabled',true);
$j(button).html('<span>Remover</span>');
$j('.item-custom').append('<tr id="trAppend"><td class="a-center lc-thumbnails"><img src="' + retorno['imagem'] + '" width="50" height="50" alt="' + retorno['name'] + '"></td><td><h3 class="product-name">' + retorno['name'] + '</h3></td><td class="a-center">1</td><td class="a-right"><span class="cart-price"><span class="price"> R$ ' + retorno['price'] + '</span></span></td></tr>');
getSubTotal();
getGrandTotal();
} else{
$j('#cartao').find(':button').attr('disabled',false);
$j(button).html('<span>Comprar</span>');
$j('.item-custom #trAppend').remove();
removeCartaotoCart();
getSubTotal();
getGrandTotal();
}
},
complete: function () {
},
error: function (x,y,z) {
alert("error");
alert(x);
alert(y);
alert(z);
window.location.reload();
history.go(0);
window.location.href=window.location.href;
}
});
}
Best Answer
You can do it like below.
First update your
removeCartaotoCart
function and pass the item id to remove as an argument.Now update your PHP code to get that Id and remove the item matching the id.
Update below code in your
addCartao
function