Mage_Checkout_CartController Class Reference

Inheritance diagram for Mage_Checkout_CartController:

Mage_Core_Controller_Front_Action Mage_Core_Controller_Varien_Action

List of all members.

Public Member Functions

 indexAction ()
 addAction ()
 addgroupAction ()
 updatePostAction ()
 deleteAction ()
 estimatePostAction ()
 estimateUpdatePostAction ()
 couponPostAction ()

Protected Member Functions

 _getCart ()
 _getSession ()
 _getQuote ()
 _goBack ()
 _initProduct ()

Protected Attributes

 $_cookieCheckActions = array('add')


Detailed Description

Shopping cart controller

Definition at line 30 of file CartController.php.


Member Function Documentation

_getCart (  )  [protected]

Retrieve shopping cart model object

Returns:
Mage_Checkout_Model_Cart

Definition at line 44 of file CartController.php.

00045     {
00046         return Mage::getSingleton('checkout/cart');
00047     }

_getQuote (  )  [protected]

Get current active quote instance

Returns:
Mage_Sales_Model_Quote

Definition at line 64 of file CartController.php.

00065     {
00066         return $this->_getCart()->getQuote();
00067     }

_getSession (  )  [protected]

Get checkout session model instance

Returns:
Mage_Checkout_Model_Session

Definition at line 54 of file CartController.php.

00055     {
00056         return Mage::getSingleton('checkout/session');
00057     }

_goBack (  )  [protected]

Set back redirect url to response

Returns:
Mage_Checkout_CartController

Definition at line 74 of file CartController.php.

00075     {
00076         if (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
00077             && !$this->getRequest()->getParam('in_cart')
00078             && $backUrl = $this->_getRefererUrl()) {
00079 
00080             $this->getResponse()->setRedirect($backUrl);
00081         } else {
00082             if (($this->getRequest()->getActionName() == 'add') && !$this->getRequest()->getParam('in_cart')) {
00083                 $this->_getSession()->setContinueShoppingUrl($this->_getRefererUrl());
00084             }
00085             $this->_redirect('checkout/cart');
00086         }
00087         return $this;
00088     }

_initProduct (  )  [protected]

Initialize product instance from request data

Returns:
Mage_Catalog_Model_Product || false

Definition at line 95 of file CartController.php.

00096     {
00097         $productId = (int) $this->getRequest()->getParam('product');
00098         if ($productId) {
00099             $product = Mage::getModel('catalog/product')
00100                 ->setStoreId(Mage::app()->getStore()->getId())
00101                 ->load($productId);
00102             if ($product->getId()) {
00103                 return $product;
00104             }
00105         }
00106         return false;
00107     }

addAction (  ) 

Add product to shopping cart action

Check product availability

Todo:
remove wishlist observer processAddToCart

Definition at line 149 of file CartController.php.

00150     {
00151         $cart   = $this->_getCart();
00152         $params = $this->getRequest()->getParams();
00153 
00154         $product= $this->_initProduct();
00155         $related= $this->getRequest()->getParam('related_product');
00156 
00157         /**
00158          * Check product availability
00159          */
00160         if (!$product) {
00161             $this->_goBack();
00162             return;
00163         }
00164 
00165 
00166         try {
00167             $cart->addProduct($product, $params);
00168             if (!empty($related)) {
00169                 $cart->addProductsByIds(explode(',', $related));
00170             }
00171 
00172             $cart->save();
00173 
00174             $this->_getSession()->setCartWasUpdated(true);
00175 
00176             /**
00177              * @todo remove wishlist observer processAddToCart
00178              */
00179             Mage::dispatchEvent('checkout_cart_add_product_complete',
00180                 array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
00181             );
00182             $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());
00183             if (!$this->_getSession()->getNoCartRedirect(true)) {
00184                 $this->_getSession()->addSuccess($message);
00185                 $this->_goBack();
00186             }
00187         }
00188         catch (Mage_Core_Exception $e) {
00189             if ($this->_getSession()->getUseNotice(true)) {
00190                 $this->_getSession()->addNotice($e->getMessage());
00191             } else {
00192                 $messages = array_unique(explode("\n", $e->getMessage()));
00193                 foreach ($messages as $message) {
00194                     $this->_getSession()->addError($message);
00195                 }
00196             }
00197 
00198             $url = $this->_getSession()->getRedirectUrl(true);
00199             if ($url) {
00200                 $this->getResponse()->setRedirect($url);
00201             } else {
00202                 $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
00203             }
00204         }
00205         catch (Exception $e) {
00206             $this->_getSession()->addException($e, $this->__('Can not add item to shopping cart'));
00207             $this->_goBack();
00208         }
00209     }

addgroupAction (  ) 

Definition at line 211 of file CartController.php.

00212     {
00213         $orderItemIds = $this->getRequest()->getParam('order_items', array());
00214         if (is_array($orderItemIds)) {
00215             $itemsCollection = Mage::getModel('sales/order_item')
00216                 ->getCollection()
00217                 ->addIdFilter($orderItemIds)
00218                 ->load();
00219             /* @var $itemsCollection Mage_Sales_Model_Mysql4_Order_Item_Collection */
00220             $cart = $this->_getCart();
00221             foreach ($itemsCollection as $item) {
00222                 try {
00223                     $cart->addOrderItem($item, 1);
00224                 }
00225                 catch (Mage_Core_Exception $e) {
00226                     if ($this->_getSession()->getUseNotice(true)) {
00227                         $this->_getSession()->addNotice($e->getMessage());
00228                     } else {
00229                         $this->_getSession()->addError($e->getMessage());
00230                     }
00231                 }
00232                 catch (Exception $e) {
00233                     $this->_getSession()->addException($e, $this->__('Can not add item to shopping cart'));
00234                     $this->_goBack();
00235                 }
00236             }
00237             $cart->save();
00238             $this->_getSession()->setCartWasUpdated(true);
00239         }
00240         $this->_goBack();
00241     }

couponPostAction (  ) 

Initialize coupon

No reason continue with empty shopping cart

Definition at line 317 of file CartController.php.

00318     {
00319         /**
00320          * No reason continue with empty shopping cart
00321          */
00322         if (!$this->_getCart()->getQuote()->getItemsCount()) {
00323             $this->_goBack();
00324             return;
00325         }
00326 
00327         $couponCode = (string) $this->getRequest()->getParam('coupon_code');
00328         if ($this->getRequest()->getParam('remove') == 1) {
00329             $couponCode = '';
00330         }
00331         $oldCouponCode = $this->_getQuote()->getCouponCode();
00332 
00333         if (!strlen($couponCode) && !strlen($oldCouponCode)) {
00334             $this->_goBack();
00335             return;
00336         }
00337 
00338         try {
00339             $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
00340             $this->_getQuote()->setCouponCode(strlen($couponCode) ? $couponCode : '')
00341                 ->collectTotals()
00342                 ->save();
00343 
00344             if ($couponCode) {
00345                 if ($couponCode == $this->_getQuote()->getCouponCode()) {
00346                     $this->_getSession()->addSuccess(
00347                         $this->__('Coupon code "%s" was applied successfully.', Mage::helper('core')->htmlEscape($couponCode))
00348                     );
00349                 }
00350                 else {
00351                     $this->_getSession()->addError(
00352                         $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->htmlEscape($couponCode))
00353                     );
00354                 }
00355             } else {
00356                 $this->_getSession()->addSuccess($this->__('Coupon code was canceled successfully.'));
00357             }
00358 
00359         }
00360         catch (Mage_Core_Exception $e) {
00361             $this->_getSession()->addError($e->getMessage());
00362         }
00363         catch (Exception $e) {
00364             $this->_getSession()->addError($this->__('Can not apply coupon code.'));
00365         }
00366 
00367         $this->_goBack();
00368     }

deleteAction (  ) 

Delete shoping cart item action

Definition at line 269 of file CartController.php.

00270     {
00271         $id = (int) $this->getRequest()->getParam('id');
00272         if ($id) {
00273             try {
00274                 $this->_getCart()->removeItem($id)
00275                   ->save();
00276             } catch (Exception $e) {
00277                 $this->_getSession()->addError($this->__('Cannot remove item'));
00278             }
00279         }
00280         $this->_redirectReferer(Mage::getUrl('*/*'));
00281     }

estimatePostAction (  ) 

Initialize shipping information

Definition at line 286 of file CartController.php.

00287     {
00288         $country    = (string) $this->getRequest()->getParam('country_id');
00289         $postcode   = (string) $this->getRequest()->getParam('estimate_postcode');
00290         $city       = (string) $this->getRequest()->getParam('estimate_city');
00291         $regionId   = (string) $this->getRequest()->getParam('region_id');
00292         $region     = (string) $this->getRequest()->getParam('region');
00293 
00294         $this->_getQuote()->getShippingAddress()
00295             ->setCountryId($country)
00296             ->setCity($city)
00297             ->setPostcode($postcode)
00298             ->setRegionId($regionId)
00299             ->setRegion($region)
00300             ->setCollectShippingRates(true);
00301         $this->_getQuote()->save();
00302         $this->_goBack();
00303     }

estimateUpdatePostAction (  ) 

Definition at line 305 of file CartController.php.

00306     {
00307         $code = (string) $this->getRequest()->getParam('estimate_method');
00308         if (!empty($code)) {
00309             $this->_getQuote()->getShippingAddress()->setShippingMethod($code)/*->collectTotals()*/->save();
00310         }
00311         $this->_goBack();
00312     }

indexAction (  ) 

Shopping cart display action

if customer enteres shopping cart we should mark quote as modified bc he can has checkout page in another window.

Definition at line 112 of file CartController.php.

00113     {
00114         $cart = $this->_getCart();
00115         if ($cart->getQuote()->getItemsCount()) {
00116             $cart->init();
00117             $cart->save();
00118 
00119             if (!$this->_getQuote()->validateMinimumAmount()) {
00120                 $warning = Mage::getStoreConfig('sales/minimum_order/description');
00121                 $cart->getCheckoutSession()->addNotice($warning);
00122             }
00123         }
00124 
00125         foreach ($cart->getQuote()->getMessages() as $message) {
00126             if ($message) {
00127                 $cart->getCheckoutSession()->addMessage($message);
00128             }
00129         }
00130 
00131         /**
00132          * if customer enteres shopping cart we should mark quote
00133          * as modified bc he can has checkout page in another window.
00134          */
00135         $this->_getSession()->setCartWasUpdated(true);
00136 
00137         Varien_Profiler::start(__METHOD__ . 'cart_display');
00138         $this->loadLayout();
00139         $this->_initLayoutMessages('checkout/session');
00140         $this->_initLayoutMessages('catalog/session');
00141         $this->getLayout()->getBlock('head')->setTitle($this->__('Shopping Cart'));
00142         $this->renderLayout();
00143         Varien_Profiler::stop(__METHOD__ . 'cart_display');
00144     }

updatePostAction (  ) 

Update shoping cart data action

Definition at line 246 of file CartController.php.

00247     {
00248         try {
00249             $cartData = $this->getRequest()->getParam('cart');
00250             if (is_array($cartData)) {
00251                 $cart = $this->_getCart();
00252                 $cart->updateItems($cartData)
00253                     ->save();
00254             }
00255             $this->_getSession()->setCartWasUpdated(true);
00256         }
00257         catch (Mage_Core_Exception $e) {
00258             $this->_getSession()->addError($e->getMessage());
00259         }
00260         catch (Exception $e) {
00261             $this->_getSession()->addException($e, $this->__('Cannot update shopping cart'));
00262         }
00263         $this->_goBack();
00264     }


Member Data Documentation

$_cookieCheckActions = array('add') [protected]

Reimplemented from Mage_Core_Controller_Varien_Action.

Definition at line 37 of file CartController.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:51 2009 for Magento by  doxygen 1.5.8