Пример #1
0
 public function updateItemOptionsAction()
 {
     $cart = $this->_getCart();
     $id = (int) $this->getRequest()->getParam('product');
     $params = $this->getRequest()->getParams();
     $product = $this->_initProduct();
     if (!isset($params['options'])) {
         $params['options'] = array();
     }
     if ($params['isAjax'] == 1) {
         $response = array();
         try {
             if (isset($params['qty'])) {
                 $filter = new Zend_Filter_LocalizedToNormalized(array('locale' => Mage::app()->getLocale()->getLocaleCode()));
                 $params['qty'] = $filter->filter($params['qty']);
             }
             $qq = Mage::getModel('checkout/cart')->getQuote();
             foreach ($qq->getAllItems() as $item) {
                 if ($id == $item->getProductId()) {
                     $quoteItem = $cart->getQuote()->getItemById($item->getId());
                     $itemId = $item->getId();
                 }
             }
             if (!$quoteItem) {
                 Mage::throwException($this->__('Quote item is not found.'));
             }
             $item = $cart->updateItem($itemId, new Varien_Object($params));
             if (is_string($item)) {
                 Mage::throwException($item);
             }
             if ($item->getHasError()) {
                 Mage::throwException($item->getMessage());
             }
             $related = $this->getRequest()->getParam('related_product');
             if (!empty($related)) {
                 $cart->addProductsByIds(explode(',', $related));
             }
             $cart->save();
             $this->_getSession()->setCartWasUpdated(true);
             Mage::dispatchEvent('checkout_cart_add_product_complete', array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()));
             if (!$cart->getQuote()->getHasError()) {
                 $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
                 $response['status'] = 'SUCCESS';
                 $response['message'] = $message;
                 $response['cart_id'] = $cart->getQuote()->getId();
                 //New Code Here
                 $this->loadLayout();
                 //$toplink = $this->getLayout()->getBlock('top.links')->toHtml();
                 $sidebar_block = $this->getLayout()->getBlock('topcart');
                 Mage::register('referrer_url', $this->_getRefererUrl());
                 $sidebar = $sidebar_block->toHtml();
                 //$response['toplink'] = $toplink;
                 $response['sidebar'] = $sidebar;
             }
         } catch (Mage_Core_Exception $e) {
             $msg = "";
             if ($this->_getSession()->getUseNotice(true)) {
                 $msg = $e->getMessage();
             } else {
                 $messages = array_unique(explode("\n", $e->getMessage()));
                 foreach ($messages as $message) {
                     $msg .= $message . '<br/>';
                 }
             }
             $response['status'] = 'ERROR';
             $response['message'] = $msg;
         } catch (Exception $e) {
             $response['status'] = 'ERROR';
             $response['message'] = $this->__('Cannot add the item to shopping cart.');
             Mage::logException($e);
         }
         //$this->getResponse()->setHeader('Content-type', 'application/json');
         $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
         return;
     } else {
         return parent::updateAction();
     }
 }