00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 class Mage_Checkout_CartController extends Mage_Core_Controller_Front_Action
00031 {
00032
00033
00034
00035
00036
00037 protected $_cookieCheckActions = array('add');
00038
00039
00040
00041
00042
00043
00044 protected function _getCart()
00045 {
00046 return Mage::getSingleton('checkout/cart');
00047 }
00048
00049
00050
00051
00052
00053
00054 protected function _getSession()
00055 {
00056 return Mage::getSingleton('checkout/session');
00057 }
00058
00059
00060
00061
00062
00063
00064 protected function _getQuote()
00065 {
00066 return $this->_getCart()->getQuote();
00067 }
00068
00069
00070
00071
00072
00073
00074 protected function _goBack()
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 }
00089
00090
00091
00092
00093
00094
00095 protected function _initProduct()
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 }
00108
00109
00110
00111
00112 public function indexAction()
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
00133
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 }
00145
00146
00147
00148
00149 public function addAction()
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
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
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 }
00210
00211 public function addgroupAction()
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
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 }
00242
00243
00244
00245
00246 public function updatePostAction()
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 }
00265
00266
00267
00268
00269 public function deleteAction()
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 }
00282
00283
00284
00285
00286 public function estimatePostAction()
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 }
00304
00305 public function estimateUpdatePostAction()
00306 {
00307 $code = (string) $this->getRequest()->getParam('estimate_method');
00308 if (!empty($code)) {
00309 $this->_getQuote()->getShippingAddress()->setShippingMethod($code)->save();
00310 }
00311 $this->_goBack();
00312 }
00313
00314
00315
00316
00317 public function couponPostAction()
00318 {
00319
00320
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 }
00369 }