0

How to get cart qty without reload the page. I am using ajax cart module if i add to cart product add into cart but qty not increment if i reload the page only it increment

minicart.phtml

<?php
    $_cartQty = $this->getSummaryCount();
    if(empty($_cartQty)) {
        $_cartQty = 0;
   }
?>
<div class="bucket-wrap">
  <a href="<?php echo $this->helper('checkout/cart')->getCartUrl(); ?>" data-target-element="#header-cart" class="buket_icon skip-link skip-cart <?php if($_cartQty <= 0): ?> no-count<?php endif; ?>"><img src="<?php echo $this->getSkinUrl('images/bucket.png') ?>"/>
      <span><?php echo Mage::getSingleton('checkout/cart')->getSummaryQty(); ?></span></a>
</div>
<span class="arrow-icon hidden"></span>
<div id="header-cart" class="block block-cart skip-content hidden">
    <?php echo $this->getChildHtml('minicart_content');?>
</div>

controller

//reload my cart
    public function cartAction()
    {   
        $_SERVER['REQUEST_URI'] = str_replace(Mage::getBaseUrl(), '/', $this->_getRefererUrl());
        $myCart = Mage::app()->getLayout()->createBlock('checkout/cart_sidebar', 'cart_sidebar')
                             ->setTemplate('checkout/cart/sidebar.phtml');
        $this->getResponse()->setBody($myCart->toHtml());
    }

   //reload top cart
    public function mcartAction()
    {
        $template = Mage::getStoreConfig('amcart/reloading/path');
        if(!$template){
            $template = "checkout/cart/mini.phtml";
        }
        $_SERVER['REQUEST_URI'] = str_replace(Mage::getBaseUrl(), '/', $this->_getRefererUrl());
        $myCart = Mage::app()->getLayout()->createBlock('checkout/cart_sidebar', 'cart_sidebar')
                             ->setTemplate($template);
        $myCart->addItemRender('grouped', 'checkout/cart_item_renderer_grouped', 'checkout/cart/sidebar/default.phtml');
        $myCart->addItemRender('configurable', 'checkout/cart_item_renderer_configurable', 'checkout/cart/sidebar/default.phtml');

        $html = $myCart->toHtml();
        if($html == ""){
            $myCart->setTemplate('checkout/cart/sidebar_header.phtml');
            $html = $myCart->toHtml();
        }
        if($html == ""){
            $this->loadLayout('default');         
            $block = Mage::app()->getLayout('default')->getBlock('minicart_head');
            if(is_object($block))
                $html = $block->toHtml();
        }
        $this->getResponse()->setBody($html);

    }

     //reload shoppingcart
    public function checkoutAction()
    {
        $_SERVER['REQUEST_URI'] = str_replace(Mage::getBaseUrl(), '/', $this->_getRefererUrl());
        $this->loadLayout(array('checkout_cart_index')); 
        $myCart = Mage::app()->getLayout('checkout_cart_index')->getBlock('checkout.cart');
        $this->getResponse()->setBody($myCart->toHtml());
    }

    //reload minicart
    public function minicartAction()
    {
        $_SERVER['REQUEST_URI'] = str_replace(Mage::getBaseUrl(), '/', $this->_getRefererUrl());
        $myCart = Mage::app()->getLayout()->createBlock('checkout/cart_sidebar', 'cart_sidebar')
                             ->setTemplate('amasty/amcart/checkout/cart/mini_cart.phtml');
        $this->getResponse()->setBody($myCart->toHtml());
    }
    //reload count
        public function dataAction()
        {
            $block = Mage::app()->getLayout()->createBlock('amcart/config', 'amcart.config');
            if (Mage::getSingleton('checkout/cart')->getSummaryQty() == 1){
                 $html = $this->__('There is') .' <a href="'.$block->getUrl('checkout/cart').'" id="am-a-count">1' . $this->__(' item') . '</a> '.$this->__('in your cart.');    
            }
            else{
                 $html = $this->__('There are') .' <a href="'.$block->getUrl('checkout/cart').'" id="am-a-count">'.Mage::getSingleton('checkout/cart')->getSummaryQty().  $this->__(' items') . '</a> '.  $this->__('in your cart.');    
            }
            $cart = Mage::getSingleton('checkout/cart');
            $priceHtml = Mage::helper('checkout')->formatPrice($this->getSubtotal($cart));
            if ($_subtotalInclTax = $this->getSubtotalInclTax($cart)){
                    $priceHtml .= '<br />(' . Mage::helper('checkout')->formatPrice($_subtotalInclTax) .' ' . Mage::helper('tax')->getIncExcText(true). ')';
            }
            $result = array(
                'count' => $html,
                'price' => $priceHtml
            );
            $this->getResponse()->setBody(Zend_Json::encode($result));
        }

        //creating finale popup 
        private function addToCartResponse($product, $cart, $IsProductView, $params, $text){
           $result = array(
                      'title'     =>  $this->__('Information'), 
                      'message'   =>  '<p>' . $this->__('You have added product to cart.') . '</p>', 
                      'b1_name'   =>  $this->__('View cart'), 
                      'b2_name'   =>  $this->__('Continue'), 
                      'count'     =>  Mage::getSingleton('checkout/cart')->getSummaryQty()>1?' (' . Mage::getSingleton('checkout/cart')->getSummaryQty(). $this->__(' items)'):' (' . Mage::getSingleton('checkout/cart')->getSummaryQty(). $this->__(' item)'),
                      'b1_action' =>  'document.location = "'.Mage::helper('checkout/cart')->getCartUrl().'";', 
                      'b2_action' =>  'jQuery.confirm.hide();',
                      'is_add_to_cart' =>  '1'
            );

Problem is min cart is not reloading

  • In which Action you have a hit for AddToCart? – Amit Bera Jun 8 '16 at 6:12
  • Sorry i am not getting – Magento 2 Jun 8 '16 at 6:25
  • which action you have hit for Ajax Cart? – Amit Bera Jun 8 '16 at 6:33
  • Sorry i am new from this i thnink mcartAction() – Magento 2 Jun 8 '16 at 6:36
0

As per as,your comment,AddtoCart is execute from mcartAction.

I have seen that your tried to return some block 'minicart_head','checkout/cart_sidebar' etc from that Action using $this->getResponse()->setBody($html);

I assume that your minicart section <div id="header-cart" has been updated properly and you have issue with class="bucket-wrap" section qty update.

If you will return Current Cart qty from mcartAction then it will work perfectly.

$result[html]=$html;
  $result[qty]=Mage::getSingleton('checkout/cart')->getSummaryQty();;
  $this->getResponse()->setHeader('Content-type','application/json',true);
  $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

Note: I have return JSON at here.So need to change something at Ajax success method

| improve this answer | |

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.