/**
  * Add wishlist item to shopping cart and remove from wishlist
  *
  * If Product has required options - item removed from wishlist and redirect
  * to product view page with message about needed defined required options
  */
 public function cartAction()
 {
     if ($this->getRequest()->getParam('callback')) {
         $ajaxData = array();
         $itemId = (int) $this->getRequest()->getParam('item');
         /* @var $item Mage_Wishlist_Model_Item */
         $item = Mage::getModel('wishlist/item')->load($itemId);
         if (!$item->getId()) {
             $ajaxData['status'] = 0;
             $ajaxData['url_wislist'] = Mage::getBaseUrl() . 'wishlist/index/configure/id/' . $itemId;
             $ajaxData['message'] = $this->__('This product no existed in wishlist');
             $this->getResponse()->setBody($this->getRequest()->getParam('callback') . '(' . Mage::helper('core')->jsonEncode($ajaxData) . ')');
             return;
             exit;
         }
         $wishlist = $this->_getWishlist($item->getWishlistId());
         if (!$wishlist) {
             return $this->_redirect('*/*');
         }
         // Set qty
         $qty = $this->getRequest()->getParam('qty');
         if (is_array($qty)) {
             if (isset($qty[$itemId])) {
                 $qty = $qty[$itemId];
             } else {
                 $qty = 1;
             }
         }
         $qty = $this->_processLocalizedQty($qty);
         if ($qty) {
             $item->setQty($qty);
         }
         /* @var $session Mage_Wishlist_Model_Session */
         $session = Mage::getSingleton('wishlist/session');
         $cart = Mage::getSingleton('checkout/cart');
         $redirectUrl = Mage::getUrl('*/*');
         try {
             $options = Mage::getModel('wishlist/item_option')->getCollection()->addItemFilter(array($itemId));
             $item->setOptions($options->getOptionsByItem($itemId));
             $buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest($this->getRequest()->getParams(), array('current_config' => $item->getBuyRequest()));
             $item->mergeBuyRequest($buyRequest);
             $item->addToCart($cart, true);
             $cart->save()->getQuote()->collectTotals();
             $wishlist->save();
             Mage::helper('wishlist')->calculate();
             if (Mage::helper('checkout/cart')->getShouldRedirectToCart()) {
                 $redirectUrl = Mage::helper('checkout/cart')->getCartUrl();
             } else {
                 if ($this->_getRefererUrl()) {
                     $redirectUrl = $this->_getRefererUrl();
                 }
             }
             Mage::helper('wishlist')->calculate();
         } catch (Mage_Core_Exception $e) {
             if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
                 //$session->addError(Mage::helper('wishlist')->__('This product(s) is currently out of stock'));
             } else {
                 if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) {
                     // Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
                     $redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
                 } else {
                     Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
                     $redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
                 }
             }
             $ajaxData['status'] = 0;
             $ajaxData['url_wislist'] = $redirectUrl;
             $ajaxData['message'] = $this->__('Please chose product options');
             $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($ajaxData));
             return;
             exit;
         } catch (Exception $e) {
             // $session->addException($e, Mage::helper('wishlist')->__('Cannot add item to shopping cart'));
             $ajaxData['status'] = 0;
             $ajaxData['url_wislist'] = $redirectUrl;
             $ajaxData['message'] = Mage::helper('wishlist')->__('Cannot add item to shopping cart');
             $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($ajaxData));
             return;
             exit;
         }
         Mage::helper('wishlist')->calculate();
         $this->loadLayout();
         $sidebarWishlist = "";
         if ($this->getLayout()->getBlock('wishlist_sidebar')) {
             $sidebarWishlist = $this->getLayout()->getBlock('wishlist_sidebar')->toHtml();
         }
         $toplink = "";
         if ($this->getLayout()->getBlock('top.links')) {
             $toplink = $this->getLayout()->getBlock('top.links')->toHtml();
         }
         $sidebarCart = "";
         if ($this->getLayout()->getBlock('cart_sidebar')) {
             $sidebarCart = $this->getLayout()->getBlock('cart_sidebar')->toHtml();
         }
         $mini_cart = "";
         if ($this->getLayout()->getBlock('cart_sidebar_mini')) {
             $mini_cart = $this->getLayout()->getBlock('cart_sidebar_mini')->toHtml();
         }
         $ajaxData['status'] = 1;
         $ajaxData['wishlist_sidebar'] = $sidebarWishlist;
         $ajaxData['top_link'] = $toplink;
         $ajaxData['type_sidebar'] = 'wishlist';
         $ajaxData['sidebar_cart'] = $sidebarCart;
         $ajaxData['mini_cart'] = $mini_cart;
         //        $product = Mage::getModel('catalog/product')->load($item->getProductId());
         //        $pimage = Mage::helper('catalog/image')->init($product, 'small_image')->resize(55);
         //        $ajaxData['product_info'] = Mage::helper('ajaxcartsuper/data')->productHtml($product->getName(), $product->getProductUrl(), $pimage);
         $this->getResponse()->setBody($this->getRequest()->getParam('callback') . '(' . Mage::helper('core')->jsonEncode($ajaxData) . ')');
     } else {
         parent::cartAction();
     }
 }
예제 #2
0
 /**
  * Add wishlist item to shopping cart and remove from wishlist
  *
  * If Product has required options - item removed from wishlist and redirect
  * to product view page with message about needed defined required options
  */
 public function cartAction()
 {
     $params = $this->getRequest()->getParams();
     if (isset($params['isAjax']) && $params['isAjax'] == 1) {
         $response = array();
         if (!$this->_validateFormKey()) {
             $response['status'] = 'ERROR';
             $response['message'] = $this->__('Some fields in form are required.');
         }
         $itemId = (int) $this->getRequest()->getParam('item');
         /* @var $item Mage_Wishlist_Model_Item */
         $item = Mage::getModel('wishlist/item')->load($itemId);
         if (!$item->getId()) {
             $response['status'] = 'ERROR';
             $response['message'] = $this->__('Unable to find Product ID');
         }
         $wishlist = $this->_getWishlist($item->getWishlistId());
         if (!$wishlist) {
             $response['status'] = 'ERROR';
             $response['message'] = $this->__('Empty wishlist');
         }
         // Set qty
         $qty = $this->getRequest()->getParam('qty');
         if (is_array($qty)) {
             if (isset($qty[$itemId])) {
                 $qty = $qty[$itemId];
             } else {
                 $qty = 1;
             }
         }
         $qty = $this->_processLocalizedQty($qty);
         if ($qty) {
             $item->setQty($qty);
         }
         /* @var $session Mage_Wishlist_Model_Session */
         $session = Mage::getSingleton('wishlist/session');
         $cart = Mage::getSingleton('checkout/cart');
         $redirectUrl = Mage::getUrl('*/*');
         try {
             $options = Mage::getModel('wishlist/item_option')->getCollection()->addItemFilter(array($itemId));
             $item->setOptions($options->getOptionsByItem($itemId));
             $buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest($this->getRequest()->getParams(), array('current_config' => $item->getBuyRequest()));
             $item->mergeBuyRequest($buyRequest);
             if ($item->addToCart($cart, true)) {
                 $cart->save()->getQuote()->collectTotals();
             }
             $wishlist->save();
             Mage::helper('wishlist')->calculate();
             if (Mage::helper('checkout/cart')->getShouldRedirectToCart()) {
                 $redirectUrl = Mage::helper('checkout/cart')->getCartUrl();
             } else {
                 if ($this->_getRefererUrl()) {
                     $redirectUrl = $this->_getRefererUrl();
                 }
             }
             Mage::helper('wishlist')->calculate();
             $product = $item->getProduct();
             $message = Mage::app()->getLayout()->createBlock("page/html")->assign("product", $product)->setTemplate('venustheme/tempcp/cart_success.phtml')->toHtml();
             $response['status'] = 'SUCCESS';
             $response['message'] = $message;
         } catch (Mage_Core_Exception $e) {
             if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
                 $response['status'] = 'ERROR';
                 $response['message'] = $this->__('This product(s) is currently out of stock');
             } else {
                 if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) {
                     Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
                     $redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
                     $response['status'] = 'ERROR';
                     $response['message'] = $e->getMessage();
                 } else {
                     Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
                     $redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
                     $response['status'] = 'ERROR';
                     $response['message'] = $e->getMessage();
                 }
             }
         } catch (Exception $e) {
             Mage::logException($e);
             $session->addException($e, $this->__('Cannot add item to shopping cart'));
             $response['status'] = 'ERROR';
             $response['message'] = $this->__('Cannot add item to shopping cart');
         }
         Mage::helper('wishlist')->calculate();
         $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
         return;
     } else {
         return parent::cartAction();
     }
 }