Welcome to Discussion Forum, where you can ask questions and receive answers from other members of the community.

ajax quick view: add to cart not showing product in minicart without refreshing the page

0 like 0 dislike
I am using a custom quick view with ajax whwn i add a product in cart using this quick view the mini cart show the count of the product but its not show the product there but after refreshing the page the product is in minicart
 
here is my code used to add to cart
 
Ajax add to cart function
 
function addtoCart(miniurl){
var miniurl=miniurl;
jQuery("#success_message").show();
jQuery("#success_message").html('<img src="/demo/media/images/loading-round.gif" alt="Loading..." width="77" height="73">');
var url;
url = jQuery('#product_addtocart_form').attr('action');
 
url = url.replace("checkout/cart", "ajax/index"); // New Code
if ('https:' == document.location.protocol) {
    url = url.replace('http:', 'https:');
}
var data = jQuery('#product_addtocart_form').serialize();
    data += '&isAjax=1';
try {
    jQuery.ajax({
        url:url,
        dataType:'json',
        type:'post',
        data:data,
        success:function (data) {
            if(data.status=="SUCCESS"){
        jQuery("#success_message").html(data.message);
         minicartAjax(miniurl);
         var qty = jQuery("#qty").val();
         var curqty = jQuery(".header-minicart .count").html();
         var newqty=parseInt(qty)+parseInt(curqty);
         jQuery(".header-minicart .count").html(newqty);
        //jQuery(".header-minicart .label").html('Cart ('+newqty+')');
 
            }
 
        }
 
    });
} catch (e) {
 
 
Custom controller for add to minicart
 
app/code/local/Minicart/Quick/controllers/minicartcontroller.php
 
    <?php
class Minicart_Custom_minicartcontroller extends Mage_Core_Controller_Front_Action{
 
    public function add_to_minicart()
{
    Mage::log("control get",null,"mini.log");
    $this->loadLayout();
    $layout = Mage::getSingleton('core/layout');
    $block = $layout->getBlock('cart_sidebar');
    $html = $block->toHtml();
    $this->getResponse()->setHeader('Content-type',
        'application/json')->setBody($this->_success($html));
    return $this;
}
}
 
app/code/local/Minicart/Quick/etc/config.xml
 
    <?xml version="1.0"?>
  <config>
     <modules>
        <Minicart_Quick>
          <version>0.1.0</version>
        </Minicart_Quick>
     </modules>
     <frontend>
       <routers>
          <routeurfrontend>
              <use>standard</use>
              <args>
                 <module>Minicart_Quick</module>
                 <frontName>Quick</frontName>
              </args>
           </routeurfrontend>
       </routers>
    </frontend>
</config>
 
And here is the method that i passed the custom controller url to the addtocart ajax function
 
<?php $miniurl=Mage::getUrl('quick/controllers/add_to_minicart');?>
<button type="button" title="Add to Cart" class="button btn-cart" id="addto-cart" onClick="addtoCart('<?php echo $miniurl;?>')">Add to Cart</button>
asked Mar 17, 2017 in Magento by harsh (8,710 points)

1 Answer

1 like 0 dislike

Hi, 

 In addtoCart funtion it seems that you have only update the quantity of mini cart but not update the  content.

You have to update the content alos  in ajax success like

 

jQuery('.top-cart-content').html(data.mini_cart);

 

And in controller where ajax request is sent add below code

 

 if ($this->getLayout()->getBlock('cart_sidebar')) {
                            $sidebarCart = $this->getLayout()->getBlock('cart_sidebar')->toHtml();
                        }
                        if ($this->getLayout()->getBlock('cart_sidebar_mini')) {
                            $mini_cart = $this->getLayout()->getBlock('cart_sidebar_mini')->toHtml();
                        }
 
$ajaxData['sidebar_cart'] = $sidebarCart;
                        $ajaxData['mini_cart'] = $mini_cart;
 
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($ajaxData));
           return;
 
 

 

 

 

 

answered Mar 22, 2017 by Gaurav (1,970 points)

Related questions

0 like 0 dislike
0 answers 323 views
0 like 0 dislike
0 answers 2,487 views
0 like 0 dislike
1 answer 185 views
0 like 0 dislike
1 answer 83 views
83 views asked May 3, 2017 in Magento by harsh (8,710 points)
1 like 0 dislike
0 answers 184 views
184 views asked Apr 3, 2017 in Magento by harsh (8,710 points)
...