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: 31: 32: 33:
34: class Mage_XmlConnect_CartController extends Mage_XmlConnect_Controller_Action
35: {
36: 37: 38: 39: 40:
41: public function indexAction()
42: {
43: try {
44: $messages = array();
45: $cart = $this->_getCart();
46: if ($cart->getQuote()->getItemsCount()) {
47: $cart->init();
48: $cart->save();
49:
50: if (!$this->_getQuote()->validateMinimumAmount()) {
51: $warning = Mage::getStoreConfig('sales/minimum_order/description');
52: $messages[parent::MESSAGE_STATUS_WARNING][] = $warning;
53: }
54: }
55:
56: foreach ($cart->getQuote()->getMessages() as $message) {
57: if ($message) {
58: $messages[$message->getType()][] = $message->getText();
59: }
60: }
61:
62: 63: 64: 65:
66: $this->_getSession()->setCartWasUpdated(true);
67: $this->loadLayout(false)->getLayout()->getBlock('xmlconnect.cart')->setMessages($messages);
68: $this->renderLayout();
69: } catch (Mage_Core_Exception $e) {
70: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
71: } catch (Exception $e) {
72: Mage::logException($e);
73: $this->_message($this->__('Can\'t load cart.'), self::MESSAGE_STATUS_ERROR);
74: }
75: }
76:
77: 78: 79: 80: 81:
82: public function updateAction()
83: {
84: try {
85: $cartData = $this->getRequest()->getParam('cart');
86: if (is_array($cartData)) {
87: $filter = new Zend_Filter_LocalizedToNormalized(
88: array('locale' => Mage::app()->getLocale()->getLocaleCode())
89: );
90: foreach ($cartData as $index => $data) {
91: if (isset($data['qty'])) {
92: $cartData[$index]['qty'] = $filter->filter($data['qty']);
93: }
94: }
95: $cart = $this->_getCart();
96: if (!$cart->getCustomerSession()->getCustomer()->getId() && $cart->getQuote()->getCustomerId()) {
97: $cart->getQuote()->setCustomerId(null);
98: }
99: $cart->updateItems($cartData)->save();
100: }
101: $this->_getSession()->setCartWasUpdated(true);
102: $this->_message($this->__('Cart has been updated.'), parent::MESSAGE_STATUS_SUCCESS);
103: } catch (Mage_Core_Exception $e) {
104: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
105: } catch (Exception $e) {
106: Mage::logException($e);
107: $this->_message($this->__('Can\'t update cart.'), self::MESSAGE_STATUS_ERROR);
108: }
109: }
110:
111: 112: 113: 114: 115: 116:
117: protected function _getProductRequest($requestInfo)
118: {
119: if ($requestInfo instanceof Varien_Object) {
120: $request = $requestInfo;
121: } elseif (is_numeric($requestInfo)) {
122: $request = new Varien_Object();
123: $request->setQty($requestInfo);
124: } else {
125: $request = new Varien_Object($requestInfo);
126: }
127:
128: if (!$request->hasQty()) {
129: $request->setQty(1);
130: }
131: return $request;
132: }
133:
134: 135: 136: 137: 138:
139: public function addAction()
140: {
141: $cart = $this->_getCart();
142: $params = $this->getRequest()->getParams();
143: try {
144: if (isset($params['qty'])) {
145: $filter = new Zend_Filter_LocalizedToNormalized(
146: array('locale' => Mage::app()->getLocale()->getLocaleCode())
147: );
148: $params['qty'] = $filter->filter($params['qty']);
149: }
150:
151: $product = null;
152: $productId = (int) $this->getRequest()->getParam('product');
153: if ($productId) {
154: $_product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())
155: ->load($productId);
156: if ($_product->getId()) {
157: $product = $_product;
158: }
159: }
160: $related = $this->getRequest()->getParam('related_product');
161:
162: 163: 164:
165: if (!$product) {
166: $this->_message($this->__('Product is unavailable.'), parent::MESSAGE_STATUS_ERROR);
167: return;
168: }
169:
170: if ($product->isConfigurable()) {
171:
172: $request = $this->_getProductRequest($params);
173: 174: 175: 176:
177: $qty = isset($params['qty']) ? $params['qty'] : 0;
178: $requestedQty = ($qty > 1) ? $qty : 1;
179: $subProduct = $product->getTypeInstance(true)
180: ->getProductByAttributes($request->getSuperAttribute(), $product);
181:
182: if (!empty($subProduct)
183: && $requestedQty < ($requiredQty = $subProduct->getStockItem()->getMinSaleQty())
184: ) {
185: $requestedQty = $requiredQty;
186: }
187:
188: $params['qty'] = $requestedQty;
189: }
190:
191: $cart->addProduct($product, $params);
192: if (!empty($related)) {
193: $cart->addProductsByIds(explode(',', $related));
194: }
195:
196: $cart->save();
197: $this->_getSession()->setCartWasUpdated(true);
198:
199: if (isset($params['whishlist_id'])) {
200: $wishlist = $this->_getWishlist();
201: $id = (int) $params['whishlist_id'];
202: $item = Mage::getModel('wishlist/item')->load($id);
203:
204: if ($item->getWishlistId() == $wishlist->getId()) {
205: try {
206: $item->delete();
207: $wishlist->save();
208: } catch (Mage_Core_Exception $e) {
209: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
210: } catch(Exception $e) {
211: $this->_message(
212: $this->__('An error occurred while removing item from wishlist.'),
213: self::MESSAGE_STATUS_ERROR
214: );
215: }
216: } else {
217: $wishlistMessage = $this->__('Specified item does not exist in wishlist.');
218: }
219: Mage::helper('wishlist')->calculate();
220: }
221:
222: 223: 224:
225: Mage::dispatchEvent('checkout_cart_add_product_complete',
226: array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
227: );
228:
229: if (!$this->_getSession()->getNoCartRedirect(true)) {
230: if (isset($wishlistMessage)) {
231: $this->_message($wishlistMessage, self::MESSAGE_STATUS_ERROR);
232: } else {
233: $productName = Mage::helper('core')->escapeHtml($product->getName());
234: $message = $this->__('%s has been added to your cart.', $productName);
235: if ($cart->getQuote()->getHasError()) {
236: $message .= $this->__(' But cart has some errors.');
237: }
238: $this->_message($message, parent::MESSAGE_STATUS_SUCCESS);
239: }
240: }
241: } catch (Mage_Core_Exception $e) {
242: if ($this->_getSession()->getUseNotice(true)) {
243: $this->_message($e->getMessage(), parent::MESSAGE_STATUS_ERROR);
244: } else {
245: $messageText = implode("\n", array_unique(explode("\n", $e->getMessage())));
246: $this->_message($messageText, parent::MESSAGE_STATUS_ERROR);
247: }
248: } catch (Exception $e) {
249: Mage::logException($e);
250: $this->_message($this->__('Can\'t add item to shopping cart.'), self::MESSAGE_STATUS_ERROR);
251: }
252: }
253:
254: 255: 256: 257: 258:
259: public function deleteAction()
260: {
261: $id = (int) $this->getRequest()->getParam('item_id');
262: if ($id) {
263: try {
264: $this->_getCart()->removeItem($id)->save();
265: $this->_message($this->__('Item has been deleted from cart.'), parent::MESSAGE_STATUS_SUCCESS);
266: } catch (Mage_Core_Exception $e) {
267: $this->_message($e->getMessage(), parent::MESSAGE_STATUS_ERROR);
268: } catch (Exception $e) {
269: Mage::logException($e);
270: $this->_message($this->__('Can\'t remove the item.'), self::MESSAGE_STATUS_ERROR);
271: }
272: }
273: }
274:
275: 276: 277: 278: 279:
280: public function couponAction()
281: {
282: 283: 284:
285: if (!$this->_getQuote()->getItemsCount()) {
286: $this->_message($this->__('Shopping cart is empty.'), self::MESSAGE_STATUS_ERROR);
287: return;
288: }
289:
290: $couponCode = (string) $this->getRequest()->getParam('coupon_code');
291: if ($this->getRequest()->getParam('remove') == 1) {
292: $couponCode = '';
293: }
294: $oldCouponCode = $this->_getQuote()->getCouponCode();
295:
296: if (!strlen($couponCode) && !strlen($oldCouponCode)) {
297: $this->_message($this->__('Coupon code is empty.'), self::MESSAGE_STATUS_ERROR);
298: return;
299: }
300:
301: try {
302: $this->_getQuote()->getShippingAddress()->setCollectShippingRates(true);
303: $this->_getQuote()->setCouponCode(strlen($couponCode) ? $couponCode : '')->collectTotals()->save();
304:
305: if ($couponCode) {
306: if ($couponCode == $this->_getQuote()->getCouponCode()) {
307: $this->_message(
308: $this->__('Coupon code %s was applied.', strip_tags($couponCode)),
309: parent::MESSAGE_STATUS_SUCCESS
310: );
311: } else {
312: $this->_message(
313: $this->__('Coupon code %s is not valid.', strip_tags($couponCode)),
314: self::MESSAGE_STATUS_ERROR
315: );
316: }
317: } else {
318: $this->_message($this->__('Coupon code was canceled.'), parent::MESSAGE_STATUS_SUCCESS);
319: }
320:
321: } catch (Mage_Core_Exception $e) {
322: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
323: } catch (Exception $e) {
324: Mage::logException($e);
325: $this->_message($this->__('Can\'t apply the coupon code.'), self::MESSAGE_STATUS_ERROR);
326: }
327: }
328:
329: 330: 331: 332: 333:
334: public function addGiftcardAction()
335: {
336: 337: 338:
339: if (!$this->_getQuote()->getItemsCount()) {
340: $this->_message($this->__('Shopping cart is empty.'), self::MESSAGE_STATUS_ERROR);
341: return;
342: }
343:
344: $data = $this->getRequest()->getPost();
345: if (!empty($data['giftcard_code'])) {
346: $code = $data['giftcard_code'];
347: try {
348: Mage::getModel('enterprise_giftcardaccount/giftcardaccount')->loadByCode($code)->addToCart();
349: $this->_message(
350: $this->__('Gift Card "%s" was added.', Mage::helper('core')->escapeHtml($code)),
351: self::MESSAGE_STATUS_SUCCESS
352: );
353: return;
354: } catch (Mage_Core_Exception $e) {
355: Mage::dispatchEvent('enterprise_giftcardaccount_add', array('status' => 'fail', 'code' => $code));
356: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
357: } catch (Exception $e) {
358: $this->_message($this->__('Cannot apply gift card.'), self::MESSAGE_STATUS_ERROR);
359: Mage::logException($e);
360: }
361: } else {
362: $this->_message($this->__('Gift Card code is empty.'), self::MESSAGE_STATUS_ERROR);
363: return;
364: }
365: }
366:
367: 368: 369: 370: 371:
372: public function removeGiftcardAction()
373: {
374: $code = $this->getRequest()->getParam('giftcard_code');
375: if ($code) {
376: try {
377: Mage::getModel('enterprise_giftcardaccount/giftcardaccount')->loadByCode($code)->removeFromCart();
378: $this->_message(
379: $this->__('Gift Card "%s" was removed.', Mage::helper('core')->escapeHtml($code)),
380: self::MESSAGE_STATUS_SUCCESS
381: );
382: } catch (Mage_Core_Exception $e) {
383: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
384: } catch (Exception $e) {
385: $this->_message($this->__('Cannot remove gift card.'), self::MESSAGE_STATUS_ERROR);
386: Mage::logException($e);
387: }
388: } else {
389: $this->_message($this->__('Gift Card code is empty.'), self::MESSAGE_STATUS_ERROR);
390: return;
391: }
392: }
393:
394: 395: 396: 397: 398:
399: public function removeStoreCreditAction()
400: {
401: if (!Mage::helper('enterprise_customerbalance')->isEnabled()) {
402: $this->_message($this->__('Customer balance is disabled for current store'), self::MESSAGE_STATUS_ERROR);
403: return;
404: }
405:
406: $quote = $this->_getQuote();
407:
408: if ($quote->getUseCustomerBalance()) {
409: $this->_message(
410: $this->__('The store credit payment has been removed from shopping cart.'),
411: self::MESSAGE_STATUS_SUCCESS
412: );
413: $quote->setUseCustomerBalance(false)->collectTotals()->save();
414: return;
415: } else {
416: $this->_message(
417: $this->__('Store Credit payment is not being used in your shopping cart.'),
418: self::MESSAGE_STATUS_ERROR
419: );
420: return;
421: }
422: }
423:
424: 425: 426: 427: 428:
429: public function infoAction()
430: {
431: try {
432: $this->_getQuote()->collectTotals()->save();
433: $this->loadLayout(false);
434: $this->renderLayout();
435: } catch (Mage_Core_Exception $e) {
436: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
437: } catch (Exception $e) {
438: Mage::logException($e);
439: $this->_message($this->__('Can\'t load cart info.'), self::MESSAGE_STATUS_ERROR);
440: }
441: }
442:
443: 444: 445: 446: 447:
448: protected function _getCart()
449: {
450: return Mage::getSingleton('checkout/cart');
451: }
452:
453: 454: 455: 456: 457:
458: protected function _getSession()
459: {
460: return Mage::getSingleton('checkout/session');
461: }
462:
463: 464: 465: 466: 467:
468: protected function _getQuote()
469: {
470: return $this->_getCart()->getQuote();
471: }
472:
473: 474: 475: 476: 477:
478: protected function _getWishlist()
479: {
480: try {
481: $wishlist = Mage::getModel('wishlist/wishlist')
482: ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer(), true);
483: Mage::register('wishlist', $wishlist);
484: } catch (Mage_Core_Exception $e) {
485: $this->_message($e->getMessage(), self::MESSAGE_STATUS_ERROR);
486: return false;
487: } catch (Exception $e) {
488: $this->_message($this->__('Can\'t create wishlist.'), self::MESSAGE_STATUS_ERROR);
489: return false;
490: }
491: return $wishlist;
492: }
493: }
494: