I have a Ajax request that I make for my controller and it returns me a array
with necessary data, however, the return of the request comes correct because giving a console.log(retorno)
it shows me in the console the correct values , but it is somewhat unstable, hours it does the .html()
function and has hours it does not, even returning the values correctly.
Ajax request code:
$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=''}}";*/
}
});
})
Code of my controller which requirement:
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);
}