Advertisement
andhiirawan

Magento: Checkout - controller - CartController.php

Nov 24th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.59 KB | None | 0 0
  1. <?php
  2. //Path : app\code\local\ABC\Checkout\controllers\CartController.php
  3. //http://magento.stackexchange.com/questions/45132/adding-a-product-to-the-cart-via-querystring-using-sku
  4. require_once(Mage::getModuleDir('controllers','Mage_Checkout').DS.'CartController.php');
  5. class ABC_Checkout_CartController extends Mage_Checkout_CartController
  6. {
  7.  
  8.     protected function _initProduct()
  9.     {
  10.         $sku = $this->getRequest()->getParam('sku');
  11.         $productId = (int) $this->getRequest()->getParam('product');
  12.  
  13.         if($sku){
  14.             $params = $this->getRequest()->getParams();
  15.             $productId = Mage::helper('checkout/cart')->getProductIdByParams($params, false);
  16.             if(! $productId){
  17.                 return false;
  18.             }
  19.         }
  20.  
  21.         if ($productId) {
  22.             $product = Mage::getModel('catalog/product')
  23.                 ->setStoreId(Mage::app()->getStore()->getId())
  24.                 ->load($productId);
  25.             if ($product->getId()) {
  26.                 return $product;
  27.             }
  28.         }
  29.         return false;
  30.     }
  31.  
  32.     /**
  33.      * Shopping cart display action
  34.      */
  35.     public function indexAction()
  36.     {            
  37.         //remove last order id to prevent update order/status API again
  38.         Mage::getModel('checkout/session')->unsLastOrderId();
  39.  
  40.         parent::indexAction();
  41.     }
  42.  
  43.     /**
  44.      * Custom Add product to shopping cart action
  45.      */
  46.     public function addAction()
  47.     {
  48.         $cart = $this->_getCart();
  49.         $params = $this->getRequest()->getParams();
  50.  
  51.         if ($params["isAjax"] == 1) {
  52.             $response = array();
  53.             try {
  54.                 if (isset($params['qty'])) {
  55.                     $filter = new Zend_Filter_LocalizedToNormalized(
  56.                         array('locale' => Mage::app()->getLocale()->getLocaleCode())
  57.                     );
  58.                     $params['qty'] = $filter->filter($params['qty']);
  59.                 }
  60.  
  61.                 $product = $this->_initProduct();
  62.                 $related = $this->getRequest()->getParam('related_product');
  63.  
  64.                 /**
  65.                  * Check product availability
  66.                  */
  67.                 if (!$product) {
  68.                     $this->_goBack();
  69.                     return;
  70.                 }
  71.  
  72.                 $cart->addProduct($product, $params);
  73.                 if (!empty($related)) {
  74.                     $cart->addProductsByIds(explode(',', $related));
  75.                 }
  76.  
  77.                 $cart->save();
  78.  
  79.                 $this->_getSession()->setCartWasUpdated(true);
  80.  
  81.                 /**
  82.                  * @todo remove wishlist observer processAddToCart
  83.                  */
  84.                 Mage::dispatchEvent('checkout_cart_add_product_complete',
  85.                     array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
  86.                 );
  87.  
  88.                 if (!$this->_getSession()->getNoCartRedirect(true)) {
  89.                     if (!$cart->getQuote()->getHasError()) {
  90.                         $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
  91.                         $this->loadLayout();
  92.                         $response["message"] = $message;
  93.                         if(Mage::getSingleton('core/session')->getSwitchView() == "desktop") {
  94.                             $response["right_slide"] = $this->getLayout()->getBlock('sp_help_rightslide')->toHtml();
  95.                             $response["sidebar"] = $this->getLayout()->getBlock('cart_sidebar')->toHtml();
  96.                         }else{
  97.                             $this->_getSession()->addSuccess($message);
  98.                         }
  99.                     }
  100.                 }
  101.             } catch (Mage_Core_Exception $e) {
  102.                 if ($this->_getSession()->getUseNotice(true)) {
  103.                     $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
  104.                 } else {
  105.                     $messages = array_unique(explode("\n", $e->getMessage()));
  106.                     foreach ($messages as $message) {
  107.                         $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
  108.                     }
  109.                 }
  110.  
  111.                 $url = $this->_getSession()->getRedirectUrl(true);
  112.                 if ($url) {
  113.                     $this->getResponse()->setRedirect($url);
  114.                 } else {
  115.                     $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
  116.                 }
  117.             } catch (Exception $e) {
  118.                 $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
  119.                 Mage::logException($e);
  120.             }
  121.             $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
  122.             return;
  123.         } else {
  124.             return parent::addAction();  
  125.         }
  126.     }
  127.  
  128.  
  129.     /**
  130.      * Delete shoping cart item action
  131.      */
  132.     public function deleteAction()
  133.     {
  134.         $id = (int) $this->getRequest()->getParam('id');
  135.         if ($id) {
  136.             try {  
  137.                 $productName = "";
  138.                 foreach ($this->_getCart()->getItems() as $item) {
  139.                     if ($item->getId() == $id) {
  140.                         $productName = $item->getName();
  141.                         break;
  142.                     }
  143.                 }
  144.                 $this->_getCart()->removeItem($id)
  145.                   ->save();
  146.                 $message = $this->__('%s has already removed from your cart.', Mage::helper('core')->escapeHtml($productName));
  147.                 $this->_getSession()->addSuccess($message);
  148.             } catch (Exception $e) {
  149.                 $this->_getSession()->addError($this->__('Cannot remove the item.'));
  150.                 Mage::logException($e);
  151.             }
  152.         }
  153.         $this->_redirectReferer(Mage::getUrl('*/*'));
  154.     }
  155.  
  156.  
  157.     /**
  158.      * Update shopping cart data action
  159.      */
  160.     public function updatePostAction()
  161.     {
  162.         $updateAction = (string)$this->getRequest()->getParam('update_cart_action');
  163.         $redirect_url = (string)$this->getRequest()->getParam('redirect_url');  
  164.                  
  165.         switch ($updateAction) {
  166.             case 'empty_cart':
  167.                 $this->_emptyShoppingCart();
  168.                 break;
  169.                
  170.             case 'update_qty': // back to shopping cart        
  171.                 //refresh stock - call connector api - get stock qty real time                
  172.                 Mage::getModel('abc_stock/data')->refreshStockOnepage();    
  173.  
  174.                 $this->_updateShoppingCart();
  175.                 break;
  176.            
  177.             case 'continue_shopping': // go to product page
  178.                
  179.                 $this->_updateShoppingCart();
  180.                 break;
  181.                                
  182.             default: // go to checkout page          
  183.                 //refresh stock - call connector api - get stock qty real time                
  184.                 Mage::getModel('abc_stock/data')->refreshStockOnepage();  
  185.                 list($stock_status, $total_items_has_stock, $total_items) = Mage::getModel('abc_stock/data')->checkStockOnepage();  
  186.                                                
  187.                 if($total_items_has_stock != $total_items){
  188.                     Mage::getSingleton('customer/session')->setIsAllStockAvailable(FALSE);
  189.                     Mage::getSingleton('checkout/session')->addError($this->__('Sorry, you can not place an order at this time. Please try again later.'));
  190.                     $this->_redirectUrl(Mage::getUrl('checkout/cart'));
  191.                    
  192.                     return;
  193.                 }else{                  
  194.                     $this->_updateShoppingCart();
  195.                 }  
  196.         }
  197.  
  198.  
  199.         if($redirect_url){
  200.             $this->_redirectUrl($redirect_url);
  201.         }else{
  202.             $this->_goBack();
  203.         }
  204.        
  205.     }
  206.  
  207.     public function addMassAction() {
  208.         $product_list = $this->getRequest()->getPost("product_list");
  209.         $products = explode(",", $product_list);
  210.         $cart = Mage::getModel('checkout/cart');
  211.         $cart->init();
  212.         /* @var $pModel Mage_Catalog_Model_Product */
  213.         foreach ($products as $product_id) {
  214.             if ($product_id == '') {
  215.                 continue;
  216.             }
  217.             $pModel = Mage::getModel('catalog/product')->load($product_id);
  218.                 try {
  219.                     $cart->addProduct($pModel, array('qty' => '1'));
  220.                 } catch (Exception $e) {
  221.                     continue;
  222.                 }
  223.         }
  224.         $cart->save();
  225.         if ($this->getRequest()->isXmlHttpRequest()) {
  226.             exit('1');
  227.         }
  228.         $this->_redirect('checkout/cart');
  229.  
  230.     }
  231.  
  232. }
  233.  
  234. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement