1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29:
30: class Mage_Checkout_CartController extends Mage_Core_Controller_Front_Action
31: {
32: 33: 34: 35: 36:
37: protected $_cookieCheckActions = array('add');
38:
39: 40: 41: 42: 43:
44: protected function _getCart()
45: {
46: return Mage::getSingleton('checkout/cart');
47: }
48:
49: 50: 51: 52: 53:
54: protected function _getSession()
55: {
56: return Mage::getSingleton('checkout/session');
57: }
58:
59: 60: 61: 62: 63:
64: protected function _getQuote()
65: {
66: return $this->_getCart()->getQuote();
67: }
68:
69: 70: 71: 72: 73:
74: protected function _goBack()
75: {
76: $returnUrl = $this->getRequest()->getParam('return_url');
77: if ($returnUrl) {
78:
79: if (!$this->_isUrlInternal($returnUrl)) {
80: throw new Mage_Exception('External urls redirect to "' . $returnUrl . '" denied!');
81: }
82:
83: $this->_getSession()->getMessages(true);
84: $this->getResponse()->setRedirect($returnUrl);
85: } elseif (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
86: && !$this->getRequest()->getParam('in_cart')
87: && $backUrl = $this->_getRefererUrl()
88: ) {
89: $this->getResponse()->setRedirect($backUrl);
90: } else {
91: if (($this->getRequest()->getActionName() == 'add') && !$this->getRequest()->getParam('in_cart')) {
92: $this->_getSession()->setContinueShoppingUrl($this->_getRefererUrl());
93: }
94: $this->_redirect('checkout/cart');
95: }
96: return $this;
97: }
98:
99: 100: 101: 102: 103:
104: protected function _initProduct()
105: {
106: $productId = (int) $this->getRequest()->getParam('product');
107: if ($productId) {
108: $product = Mage::getModel('catalog/product')
109: ->setStoreId(Mage::app()->getStore()->getId())
110: ->load($productId);
111: if ($product->getId()) {
112: return $product;
113: }
114: }
115: return false;
116: }
117:
118: 119: 120:
121: public function indexAction()
122: {
123: $cart = $this->_getCart();
124: if ($cart->getQuote()->getItemsCount()) {
125: $cart->init();
126: $cart->save();
127:
128: if (!$this->_getQuote()->validateMinimumAmount()) {
129: $minimumAmount = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())
130: ->toCurrency(Mage::getStoreConfig('sales/minimum_order/amount'));
131:
132: $warning = Mage::getStoreConfig('sales/minimum_order/description')
133: ? Mage::getStoreConfig('sales/minimum_order/description')
134: : Mage::helper('checkout')->__('Minimum order amount is %s', $minimumAmount);
135:
136: $cart->getCheckoutSession()->addNotice($warning);
137: }
138: }
139:
140:
141: $messages = array();
142: foreach ($cart->getQuote()->getMessages() as $message) {
143: if ($message) {
144:
145: $message->setCode(Mage::helper('core')->escapeHtml($message->getCode()));
146: $messages[] = $message;
147: }
148: }
149: $cart->getCheckoutSession()->addUniqueMessages($messages);
150:
151: 152: 153: 154:
155: $this->_getSession()->setCartWasUpdated(true);
156:
157: Varien_Profiler::start(__METHOD__ . 'cart_display');
158: $this
159: ->loadLayout()
160: ->_initLayoutMessages('checkout/session')
161: ->_initLayoutMessages('catalog/session')
162: ->getLayout()->getBlock('head')->setTitle($this->__('Shopping Cart'));
163: $this->renderLayout();
164: Varien_Profiler::stop(__METHOD__ . 'cart_display');
165: }
166:
167: 168: 169:
170: public function addAction()
171: {
172: $cart = $this->_getCart();
173: $params = $this->getRequest()->getParams();
174: try {
175: if (isset($params['qty'])) {
176: $filter = new Zend_Filter_LocalizedToNormalized(
177: array('locale' => Mage::app()->getLocale()->getLocaleCode())
178: );
179: $params['qty'] = $filter->filter($params['qty']);
180: }
181:
182: $product = $this->_initProduct();
183: $related = $this->getRequest()->getParam('related_product');
184:
185: 186: 187:
188: if (!$product) {
189: $this->_goBack();
190: return;
191: }
192:
193: $cart->addProduct($product, $params);
194: if (!empty($related)) {
195: $cart->addProductsByIds(explode(',', $related));
196: }
197:
198: $cart->save();
199:
200: $this->_getSession()->setCartWasUpdated(true);
201:
202: 203: 204:
205: Mage::dispatchEvent('checkout_cart_add_product_complete',
206: array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
207: );
208:
209: if (!$this->_getSession()->getNoCartRedirect(true)) {
210: if (!$cart->getQuote()->getHasError()){
211: $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
212: $this->_getSession()->addSuccess($message);
213: }
214: $this->_goBack();
215: }
216: } catch (Mage_Core_Exception $e) {
217: if ($this->_getSession()->getUseNotice(true)) {
218: $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
219: } else {
220: $messages = array_unique(explode("\n", $e->getMessage()));
221: foreach ($messages as $message) {
222: $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
223: }
224: }
225:
226: $url = $this->_getSession()->getRedirectUrl(true);
227: if ($url) {
228: $this->getResponse()->setRedirect($url);
229: } else {
230: $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
231: }
232: } catch (Exception $e) {
233: $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
234: Mage::logException($e);
235: $this->_goBack();
236: }
237: }
238:
239: public function addgroupAction()
240: {
241: $orderItemIds = $this->getRequest()->getParam('order_items', array());
242: if (is_array($orderItemIds)) {
243: $itemsCollection = Mage::getModel('sales/order_item')
244: ->getCollection()
245: ->addIdFilter($orderItemIds)
246: ->load();
247:
248: $cart = $this->_getCart();
249: foreach ($itemsCollection as $item) {
250: try {
251: $cart->addOrderItem($item, 1);
252: } catch (Mage_Core_Exception $e) {
253: if ($this->_getSession()->getUseNotice(true)) {
254: $this->_getSession()->addNotice($e->getMessage());
255: } else {
256: $this->_getSession()->addError($e->getMessage());
257: }
258: } catch (Exception $e) {
259: $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
260: Mage::logException($e);
261: $this->_goBack();
262: }
263: }
264: $cart->save();
265: $this->_getSession()->setCartWasUpdated(true);
266: }
267: $this->_goBack();
268: }
269:
270: 271: 272:
273: public function configureAction()
274: {
275:
276: $id = (int) $this->getRequest()->getParam('id');
277: $quoteItem = null;
278: $cart = $this->_getCart();
279: if ($id) {
280: $quoteItem = $cart->getQuote()->getItemById($id);
281: }
282:
283: if (!$quoteItem) {
284: $this->_getSession()->addError($this->__('Quote item is not found.'));
285: $this->_redirect('checkout/cart');
286: return;
287: }
288:
289: try {
290: $params = new Varien_Object();
291: $params->setCategoryId(false);
292: $params->setConfigureMode(true);
293: $params->setBuyRequest($quoteItem->getBuyRequest());
294:
295: Mage::helper('catalog/product_view')->prepareAndRender($quoteItem->getProduct()->getId(), $this, $params);
296: } catch (Exception $e) {
297: $this->_getSession()->addError($this->__('Cannot configure product.'));
298: Mage::logException($e);
299: $this->_goBack();
300: return;
301: }
302: }
303:
304: 305: 306:
307: public function updateItemOptionsAction()
308: {
309: $cart = $this->_getCart();
310: $id = (int) $this->getRequest()->getParam('id');
311: $params = $this->getRequest()->getParams();
312:
313: if (!isset($params['options'])) {
314: $params['options'] = array();
315: }
316: try {
317: if (isset($params['qty'])) {
318: $filter = new Zend_Filter_LocalizedToNormalized(
319: array('locale' => Mage::app()->getLocale()->getLocaleCode())
320: );
321: $params['qty'] = $filter->filter($params['qty']);
322: }
323:
324: $quoteItem = $cart->getQuote()->getItemById($id);
325: if (!$quoteItem) {
326: Mage::throwException($this->__('Quote item is not found.'));
327: }
328:
329: $item = $cart->updateItem($id, new Varien_Object($params));
330: if (is_string($item)) {
331: Mage::throwException($item);
332: }
333: if ($item->getHasError()) {
334: Mage::throwException($item->getMessage());
335: }
336:
337: $related = $this->getRequest()->getParam('related_product');
338: if (!empty($related)) {
339: $cart->addProductsByIds(explode(',', $related));
340: }
341:
342: $cart->save();
343:
344: $this->_getSession()->setCartWasUpdated(true);
345:
346: Mage::dispatchEvent('checkout_cart_update_item_complete',
347: array('item' => $item, 'request' => $this->getRequest(), 'response' => $this->getResponse())
348: );
349: if (!$this->_getSession()->getNoCartRedirect(true)) {
350: if (!$cart->getQuote()->getHasError()){
351: $message = $this->__('%s was updated in your shopping cart.', Mage::helper('core')->htmlEscape($item->getProduct()->getName()));
352: $this->_getSession()->addSuccess($message);
353: }
354: $this->_goBack();
355: }
356: } catch (Mage_Core_Exception $e) {
357: if ($this->_getSession()->getUseNotice(true)) {
358: $this->_getSession()->addNotice($e->getMessage());
359: } else {
360: $messages = array_unique(explode("\n", $e->getMessage()));
361: foreach ($messages as $message) {
362: $this->_getSession()->addError($message);
363: }
364: }
365:
366: $url = $this->_getSession()->getRedirectUrl(true);
367: if ($url) {
368: $this->getResponse()->setRedirect($url);
369: } else {
370: $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
371: }
372: } catch (Exception $e) {
373: $this->_getSession()->addException($e, $this->__('Cannot update the item.'));
374: Mage::logException($e);
375: $this->_goBack();
376: }
377: $this->_redirect('*/*');
378: }
379:
380: 381: 382:
383: public function updatePostAction()
384: {
385: $updateAction = (string)$this->getRequest()->getParam('update_cart_action');
386:
387: switch ($updateAction) {
388: case 'empty_cart':
389: $this->_emptyShoppingCart();
390: break;
391: case 'update_qty':
392: $this->_updateShoppingCart();
393: break;
394: default:
395: $this->_updateShoppingCart();
396: }
397:
398: $this->_goBack();
399: }
400:
401: 402: 403:
404: protected function _updateShoppingCart()
405: {
406: try {
407: $cartData = $this->getRequest()->getParam('cart');
408: if (is_array($cartData)) {
409: $filter = new Zend_Filter_LocalizedToNormalized(
410: array('locale' => Mage::app()->getLocale()->getLocaleCode())
411: );
412: foreach ($cartData as $index => $data) {
413: if (isset($data['qty'])) {
414: $cartData[$index]['qty'] = $filter->filter(trim($data['qty']));
415: }
416: }
417: $cart = $this->_getCart();
418: if (! $cart->getCustomerSession()->getCustomer()->getId() && $cart->getQuote()->getCustomerId()) {
419: $cart->getQuote()->setCustomerId(null);
420: }
421:
422: $cartData = $cart->suggestItemsQty($cartData);
423: $cart->updateItems($cartData)
424: ->save();
425: }
426: $this->_getSession()->setCartWasUpdated(true);
427: } catch (Mage_Core_Exception $e) {
428: $this->_getSession()->addError(Mage::helper('core')->escapeHtml($e->getMessage()));
429: } catch (Exception $e) {
430: $this->_getSession()->addException($e, $this->__('Cannot update shopping cart.'));
431: Mage::logException($e);
432: }
433: }
434:
435: 436: 437:
438: protected function _emptyShoppingCart()
439: {
440: try {
441: $this->_getCart()->truncate()->save();
442: $this->_getSession()->setCartWasUpdated(true);
443: } catch (Mage_Core_Exception $exception) {
444: $this->_getSession()->addError($exception->getMessage());
445: } catch (Exception $exception) {
446: $this->_getSession()->addException($exception, $this->__('Cannot update shopping cart.'));
447: }
448: }
449:
450: 451: 452:
453: public function deleteAction()
454: {
455: $id = (int) $this->getRequest()->getParam('id');
456: if ($id) {
457: try {
458: $this->_getCart()->removeItem($id)
459: ->save();
460: } catch (Exception $e) {
461: $this->_getSession()->addError($this->__('Cannot remove the item.'));
462: Mage::logException($e);
463: }
464: }
465: $this->_redirectReferer(Mage::getUrl('*/*'));
466: }
467:
468: 469: 470:
471: public function estimatePostAction()
472: {
473: $country = (string) $this->getRequest()->getParam('country_id');
474: $postcode = (string) $this->getRequest()->getParam('estimate_postcode');
475: $city = (string) $this->getRequest()->getParam('estimate_city');
476: $regionId = (string) $this->getRequest()->getParam('region_id');
477: $region = (string) $this->getRequest()->getParam('region');
478:
479: $this->_getQuote()->getShippingAddress()
480: ->setCountryId($country)
481: ->setCity($city)
482: ->setPostcode($postcode)
483: ->setRegionId($regionId)
484: ->setRegion($region)
485: ->setCollectShippingRates(true);
486: $this->_getQuote()->save();
487: $this->_goBack();
488: }
489:
490: public function estimateUpdatePostAction()
491: {
492: $code = (string) $this->getRequest()->getParam('estimate_method');
493: if (!empty($code)) {
494: $this->_getQuote()->getShippingAddress()->setShippingMethod($code)->save();
495: }
496: $this->_goBack();
497: }
498:
499: 500: 501:
502: public function couponPostAction()
503: {
504: 505: 506:
507: if (!$this->_getCart()->getQuote()->getItemsCount()) {
508: $this->_goBack();
509: return;
510: }
511:
512: $couponCode = (string) $this->getRequest()->getParam('coupon_code');
513: if ($this->getRequest()->getParam('remove') == 1) {
514: $couponCode = '';
515: }
516: $oldCouponCode = $this->_getQuote()->getCouponCode();
517:
518: if (!strlen($couponCode) && !strlen($oldCouponCode)) {
519: $this->_goBack();
520: return;
521: }
522:
523: try {
524: $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
525: $this->_getQuote()->setCouponCode(strlen($couponCode) ? $couponCode : '')
526: ->collectTotals()
527: ->save();
528:
529: if (strlen($couponCode)) {
530: if ($couponCode == $this->_getQuote()->getCouponCode()) {
531: $this->_getSession()->addSuccess(
532: $this->__('Coupon code "%s" was applied.', Mage::helper('core')->htmlEscape($couponCode))
533: );
534: }
535: else {
536: $this->_getSession()->addError(
537: $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->htmlEscape($couponCode))
538: );
539: }
540: } else {
541: $this->_getSession()->addSuccess($this->__('Coupon code was canceled.'));
542: }
543:
544: } catch (Mage_Core_Exception $e) {
545: $this->_getSession()->addError($e->getMessage());
546: } catch (Exception $e) {
547: $this->_getSession()->addError($this->__('Cannot apply the coupon code.'));
548: Mage::logException($e);
549: }
550:
551: $this->_goBack();
552: }
553: }
554: