Ajax does not run correctly

Asked

Viewed 85 times

0

I have an Ajax request that I make for my controller and it returns a array with necessary data, however, the return of the request is correct because giving a console.log(retorno) he shows me on the console the correct values, however he is a bit unstable, hours he makes the function .html() and there are times he does not even returning me the values correctly.

Code of the Ajax request:

$j(document).ready(function(){
        $j.ajax({
            type: "POST",
            url: "<?php echo Mage::getUrl('fol_carousel/ajax/atualizaCarrinho') ?>",
            data:{

            },
            dataType: 'json',
            cache: false,
            beforeSend: function(){
            },
            success: function(retorno){
                //alert(retorno);
                console.log(retorno);
                //console.log(retorno['itensCarrinho']);
                $j('#cart-sidebar').html(retorno['produto']);
                if(retorno['itensCarrinho'] > 0){
                    console.log('Fez');
                    $j('.count-cart').html('<br/><br/>'+retorno['itensCarrinho']+'');
                }
                $j('.subtotal .label').html('Subtotal: R$'+retorno['subtotal']);
            },
            complete: function(){

            },
            error: function(x,y,z){
                alert(x);
                alert(y);
                alert(z);
                //alert("Erro!");
                /*window.location.reload();
                history.go(0);
                window.location.href = "{{store url=''}}";*/
            }
        });
    })

My controller code that requires:

public function atualizaCarrinhoAction(){
            $cart = Mage::getModel('checkout/cart')->getQuote();
            $itensCart = Mage::helper('checkout/cart')->getSummaryCount();
            foreach ($cart->getAllVIsibleItems() as $item){
                $product = $item->getProduct();
                $id = $product->getId();
                $productName = $product->getName();
                $productPrice = number_format((float)$product->getFinalPrice(), 2, ',', '');
                $product = Mage::getModel('catalog/product')->load($id); 
                $productUrl = $item->getProduct()->getProductUrl();
                $image   = Mage::helper('catalog/image')->init($product, 'small_image')->resize(50);
                $media = (string) $image;
                $img = $media;
                $qty =  $item->getQty();

                $item = $cart->getItemByProduct($product);
                if ($item !== false) {
                    $id_quota = $item->getId();
                }
                $url_del = Mage::getUrl('checkout/cart/delete', array('id' => $id_quota))."form_key/".Mage::getSingleton('core/session')->getFormKey();

                $html['produto'].='<li class="item last odd">
                        <a href="'. $productUrl .'" title="'. $productName .'" class="product-image"><img src="'. $img .'" width="50" height="50" alt="'. $productName .'"></a>
                            <div class="product-details">
                                <a href="'. $url_del .'" title="Excluir Este Produto" onclick="return confirm("Deseja realmente retirar este produto do seu carrinho?");" class="btn-remove">Excluir Este Produto</a>
                                <a href="" title="Editar produto" class="btn-edit">Editar produto</a>
                                <p class="product-name"><a href="'. $productUrl .'">'. $productName .'</a></p>
                                <strong>'. $qty .'</strong> x <span class="price">R$'. $productPrice .'</span></div>
                        </li>';
            }
            $html['itensCarrinho'] = $itensCart;
            //Pegando o SubTotal do carrinho
            $subtotal = Mage::helper('checkout/cart')->getQuote()->getSubtotal();
            $subtotalCart = number_format((float)$subtotal, 2, ',', '');
            $html['subtotal'] = $subtotalCart;

            echo json_encode($html);
        }
  • 1

    Please avoid extensive discussions in the comments, if necessary, move this conversa for a chat.

  • Please avoid long discussions in the comments; your talk was moved to the chat

1 answer

0


The problem with this question was precisely the lack of the element with the id="cart-sidebar" and that consequently made the function .html() be unstable, hours worked and had hours that did not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.