2

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;
                  }
              });
            }
  • Can you add the JS code or PHP action code where you are using this code? – Jaimin Sutariya Sep 1 '17 at 18:27
  • Yes I can. I'll add – Matheus Portela Sep 1 '17 at 18:29
2

You can do it like below.

First update your removeCartaotoCart function and pass the item id to remove as an argument.

function removeCartaotoCart(itemId){
    $j.ajax({
        type:"POST",
        url:"<?php echo Mage::getUrl('fol_carousel/ajax/removeCartao') ?>/itemId/"+itemId,
        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;
      }
    });
}

Now update your PHP code to get that Id and remove the item matching the id.

$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
$productId = $this->getRequest()->getParam('itemId');
foreach ($items as $item) 
{
    if ($item->getProductId() == $productId) {
       $itemId = $item->getItemId();
       $cartHelper->getCart()->removeItem($itemId)->save();
    }
}

Update below code in your addCartao function

else{
    $j('#cartao').find(':button').attr('disabled',false);
    $j(button).html('<span>Comprar</span>');
    $j('.item-custom #trAppend').remove();
    removeCartaotoCart(product_id); // Pass Product ID here
    getSubTotal();
    getGrandTotal();
}
  • It did not work out and as I am using the magento own buy button, when I click and call this function, instead of removing the product, it adds another. – Matheus Portela Sep 1 '17 at 18:48
  • @MatheusPortela, How are you using Buy button to remove a product? Can you please add the code for the button too in your question? – Jaimin Sutariya Sep 1 '17 at 18:56
  • Yes, I can add yes. The next thing happens, when the Buy button is clicked, its text changes to remove and the product is added to the cart. What I wanted to do is when the button with the remove text was clicked, the same product was removed. – Matheus Portela Sep 1 '17 at 19:03
  • I updated the question with all the information. – Matheus Portela Sep 1 '17 at 19:12
  • 1
    Jaimin, I updated your answer with a code syntax correction that is required for the button to also send the parameters to the controller. – Matheus Portela Sep 2 '17 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.