1

I have some buttons to buy and remove products in the cart and I do this through two ajax requests.

Everything is working properly, however for a new product to be added, it is mandatory that the product be removed because the buttons are disabled.

I want to change this structure, so that every time buy button is clicked, the product in the cart is removed and overwritten by the same one that was clicked.

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(); ?>" id="cartaoMensagem<?php echo $_product->getId(); ?>"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<button style="display: none;" type="button" id="cartaoMensagemRemover<?php echo $_product->getId(); ?>" title="Remover" class="button btn-cart" onclick="removeCartaotoCart('<?php echo $_product->getId(); ?>')" name="cartaoMensagem<?php echo $_product->getId(); ?>"><span><span>Remove</span></span></button>

Ajax request code:

function addCartao(product_id){
                $j('#cartaoMensagem'+product_id).hide();
                $j('#cartaoMensagemRemover'+product_id).show();
                $j.ajax({
                  type: "POST",
                  url: "<?php echo Mage::getUrl('fol_carousel/ajax/addCartao') ?>",
                  data: {
                    product_id: product_id
                  },
                  dataType: 'json',
                  cache : false,
                  beforeSend: function () {
                    //alert("beforeSend");
                  },
                  success: function (retorno) {
                    var button = $j('#cartaoMensagemRemover'+product_id);

                    $j('#cartao').find(':button').not(button).attr('disabled',true);
                    $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();
                  },
                  complete: function () {
                    //alert("complete");
                  },
                  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;
                  }
              });
            }

            function removeCartaotoCart(itemId){
                $j('#cartaoMensagemRemover'+itemId).hide();
                $j('#cartaoMensagem'+itemId).show();
                $j.ajax({
                    type:"POST",
                    url:"<?php echo Mage::getUrl('fol_carousel/ajax/removeCartao') ?>",
                    data:{
                        itemId: itemId
                    },
                    cache: false,
                    beforeSend: function(){
                        //alert("beforeSend");
                    },
                    success: function(retorno){
                        var button = $j('#cartaoMensagemRemover'+itemId);
                        $j('#cartao').find(':button').attr('disabled',false);
                        $j('.item-custom #trAppend').remove();
                        getSubTotal();
                        getGrandTotal();
                        //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;
                  }
                });
            }
0

In order for the logical change to be possible, a variable was created to receive the product id so that it is in the cart and the buy button is clicked, to be withdrawn.

Ajax request code updated:

var productSelected = "";
            function addCartao(product_id){
                alert(product_id);
                if( productSelected != "" ){
                    removeCartaotoCart(productSelected);  // Remove the item in cart, if there is one.
                  }
                $j('#cartaoMensagem'+product_id).hide();
                $j('#cartaoMensagemRemover'+product_id).show();
                $j('#cartaoMensagemRemover'+product_id).css({'background-color': '#000000'});
                $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('#cartaoMensagemRemover'+product_id);
                    productSelected = product_id;  
                    alert(productSelected);

                    $j('.item-custom').append('<tr id="trAppend"<?php echo $_product->getId(); ?>><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();
                  },
                  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;
                  }
              });
            }

            function removeCartaotoCart(itemId){
                productSelected = ""; 
                $j('#cartaoMensagemRemover'+itemId).hide();
                $j('#cartaoMensagem'+itemId).show();
                $j.ajax({
                    type:"POST",
                    url:"<?php echo Mage::getUrl('fol_carousel/ajax/removeCartao') ?>",
                    data:{
                        itemId: itemId
                    },
                    cache: false,
                    beforeSend: function(){

                    },
                    success: function(retorno){
                        var button = $j('#cartaoMensagemRemover'+itemId);                            
                        $j('.item-custom #trAppend').remove();
                        getSubTotal();
                        getGrandTotal();
                        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;
                  }
                });
            }

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.