Question

Now add and remove products are taking place via ajax. But when the product is out of stock I want to remove the proceed to checkout button via ajax.

Once that out of stock product has been removed from cart, proceed to checkout button should be displayed again.

public function deleteAction()
    {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $itemId = (int) $this->getRequest()->getParam('id');
            $msg = '';
            if ($itemId) {
                try {
                    $this->_getCart()->removeItem($itemId)
                      ->save();
                    $msg = $this->__('Item was removed successfully.');
                } catch (Exception $e) {
                    $msg = $this->__('Cannot remove the item.');
                    // $this->_getSession()->addError($this->__('Cannot remove the item.'));
                    Mage::logException($e);
                }
            }
            //$this->_redirectReferer(Mage::getUrl('*/*'));
            $layout = $this->getLayout();
            $update = $layout->getUpdate();

            $update->load('born_checkout_cartheader');
            $layout->generateXml();
            $layout->generateBlocks();
            $minicartHtml = $layout->getOutput();
            $update->setCacheId(null);
            $update->load('born_checkout_cart');
            $layout->generateXml();
            $layout->generateBlocks();
            $cartHtml = $layout->getOutput();
            $result = array('minicartHtml' => $minicartHtml,'cartHtml' => $cartHtml,'msg' => $msg);
            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        } else {
            parent::deleteAction();
        }
    }
Was it helpful?

Solution

I believe "proceed to checkout" button is getting removed when you have an error in your cart items(May happen when item goes out-of-stock).

During ajax update the checkout session is not updated with the Error flag which leads to this problem. There is an Error Flag check on the cart.phtml page. You can see a code like this <?php if(!$this->hasError()): ?> <ul class="checkout-types"> ..... </ul> <?php endif; ?>

The Flag is set on Mage_CatalogInventory_Model_Stock_Item::checkQuoteItemQty() so what happens is the error flag will be updated only when you refresh the page.

SOLUTION

You need to update the Error flag in your controller BEFORE Generating your Blocks.

//get all cart items and check for errors. $cartItems = Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems(); $checkoutSession = Mage::getSingleton('checkout/session'); foreach ($cartItems as $item) { if ($checkoutSession) { $baseMessages = $item->getMessage(false); if ($baseMessages) { foreach ($baseMessages as $message) { $messages[] = array( 'text' => $message, 'type' => $item->getHasError() ? 'error' : 'notice' ); } } /* @var $collection Mage_Core_Model_Message_Collection */ $collection = $checkoutSession->getQuoteItemMessages($item->getId(), true); if ($collection) { $additionalMessages = $collection->getItems(); foreach ($additionalMessages as $message) { /* @var $message Mage_Core_Model_Message_Abstract */ $messages[] = array( 'text' => $message->getCode(), 'type' => ($message->getType() == Mage_Core_Model_Message::ERROR) ? 'error' : 'notice' ); } } } } $errMsgCount = count($messages, COUNT_RECURSIVE); //update checkout/session to show checkout button if no error if(!$errMsgCount){ $checkoutSession->getQuote()->setHasError(FALSE); }

Place the above code in your controller action before generating blocks.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top