Example #1
0
 /**
  * Retrives frame object
  * @param Varien_Object $object Category Tree Node
  * @return Varien_Object
  */
 protected function _getFrame(Varien_Object $object, $level = 1)
 {
     $frame = new Varien_Object();
     $frame->setFrameId('category' . $object->getId());
     $frame->setFrameCategoryId($object->getId());
     $frame->setHeader($object->getName());
     $frame->setLevel($level + 1);
     if (Mage::getStoreConfig('catalog/frontend/flat_catalog_category')) {
         $frame->setChildren($object->getChildrenNodes());
     } else {
         $frame->setChildren($object->getChildren());
     }
     $frame->setChildrenCount($this->__getChildrenCount($object));
     if (Mage::getStoreConfig('catalog/frontend/flat_catalog_category')) {
         if (is_array($frame->getChildrenNodes()) || is_object($frame->getChildrenNodes()) && get_class($frame->getChildrenNodes()) == 'Varien_Data_Tree_Node_Collection') {
             foreach ($frame->getChildrenNodes() as $child) {
                 $this->_frames[] = $this->_getFrame($child, $level + 1);
             }
         }
     } else {
         if (is_array($frame->getChildren()) || is_object($frame->getChildren()) && get_class($frame->getChildren()) == 'Varien_Data_Tree_Node_Collection') {
             foreach ($frame->getChildren() as $child) {
                 $this->_frames[] = $this->_getFrame($child, $level + 1);
             }
         }
     }
     return $frame;
 }
Example #2
0
 /**
  * Add product to shopping cart action
  *
  * @return Mage_Core_Controller_Varien_Action
  * @throws Exception
  */
 public function addAction()
 {
     $cart = $this->_getCart();
     $params = $this->getRequest()->getParams();
     $response = new Varien_Object();
     $response->setError(0);
     try {
         if (isset($params['qty'])) {
             $filter = new Zend_Filter_LocalizedToNormalized(array('locale' => Mage::app()->getLocale()->getLocaleCode()));
             $params['qty'] = $filter->filter($params['qty']);
         }
         $product = $this->_initProduct();
         $related = $this->getRequest()->getParam('related_product');
         /**
          * Check product availability
          */
         if (!$product) {
             //                $this->_goBack();
             //                return;
             $response->setError(1);
             $response->setErrorMessage('Unable to find Product.');
         }
         $cart->addProduct($product, $params);
         if (!empty($related)) {
             $cart->addProductsByIds(explode(',', $related));
         }
         $cart->save();
         $this->_getSession()->setCartWasUpdated(true);
         /**
          * @todo remove wishlist observer processAddToCart
          */
         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()));
             $this->loadLayout();
             $content = $this->getLayout()->getBlock('floatingcart')->toHtml();
             $header = $this->getLayout()->getBlock('topCart')->setIsAjax()->toHtml();
             $response->setContent($content);
             $response->setHeader($header);
             $response->setQty($this->_getCart()->getSummaryQty());
             $response->setSuccessMessage($message);
         } else {
             $response->setError(1);
             $response->setErrorMessage('An error occurred while adding product to cart.');
         }
     } catch (Mage_Core_Exception $e) {
         if ($this->_getSession()->getUseNotice(true)) {
             $response->setError(2);
             $response->setErrorMessage(Mage::helper('core')->escapeHtml($e->getMessage()));
         } else {
             $messages = array_unique(explode("\n", $e->getMessage()));
             foreach ($messages as $message) {
                 $msg .= Mage::helper('core')->escapeHtml($message);
             }
             $response->setError(1);
             $response->setErrorMessage(Mage::helper('core')->escapeHtml($msg));
         }
     } catch (Exception $e) {
         Mage::logException($e);
         $response->setError(1);
         $response->setErrorMessage($e, $this->__('Cannot add the item to shopping cart.'));
     }
     $this->getResponse()->setBody($response->toJson());
 }