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:
35: abstract class Mage_Wishlist_Controller_Abstract extends Mage_Core_Controller_Front_Action
36: {
37: 38: 39: 40:
41: protected $_localFilter = null;
42:
43: 44: 45: 46: 47: 48:
49: protected function _processLocalizedQty($qty)
50: {
51: if (!$this->_localFilter) {
52: $this->_localFilter = new Zend_Filter_LocalizedToNormalized(
53: array('locale' => Mage::app()->getLocale()->getLocaleCode())
54: );
55: }
56: $qty = $this->_localFilter->filter((float)$qty);
57: if ($qty < 0) {
58: $qty = null;
59: }
60: return $qty;
61: }
62:
63: 64: 65: 66: 67:
68: abstract protected function _getWishlist();
69:
70: 71: 72: 73:
74: public function allcartAction()
75: {
76: $wishlist = $this->_getWishlist();
77: if (!$wishlist) {
78: $this->_forward('noRoute');
79: return ;
80: }
81: $isOwner = $wishlist->isOwner(Mage::getSingleton('customer/session')->getCustomerId());
82:
83: $messages = array();
84: $addedItems = array();
85: $notSalable = array();
86: $hasOptions = array();
87:
88: $cart = Mage::getSingleton('checkout/cart');
89: $collection = $wishlist->getItemCollection()
90: ->setVisibilityFilter();
91:
92: $qtys = $this->getRequest()->getParam('qty');
93: foreach ($collection as $item) {
94:
95: try {
96: $disableAddToCart = $item->getProduct()->getDisableAddToCart();
97: $item->unsProduct();
98:
99:
100: if (isset($qtys[$item->getId()])) {
101: $qty = $this->_processLocalizedQty($qtys[$item->getId()]);
102: if ($qty) {
103: $item->setQty($qty);
104: }
105: }
106: $item->getProduct()->setDisableAddToCart($disableAddToCart);
107:
108: if ($item->addToCart($cart, $isOwner)) {
109: $addedItems[] = $item->getProduct();
110: }
111:
112: } catch (Mage_Core_Exception $e) {
113: if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
114: $notSalable[] = $item;
115: } else if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) {
116: $hasOptions[] = $item;
117: } else {
118: $messages[] = $this->__('%s for "%s".', trim($e->getMessage(), '.'), $item->getProduct()->getName());
119: }
120: } catch (Exception $e) {
121: Mage::logException($e);
122: $messages[] = Mage::helper('wishlist')->__('Cannot add the item to shopping cart.');
123: }
124: }
125:
126: if ($isOwner) {
127: $indexUrl = Mage::helper('wishlist')->getListUrl($wishlist->getId());
128: } else {
129: $indexUrl = Mage::getUrl('wishlist/shared', array('code' => $wishlist->getSharingCode()));
130: }
131: if (Mage::helper('checkout/cart')->getShouldRedirectToCart()) {
132: $redirectUrl = Mage::helper('checkout/cart')->getCartUrl();
133: } else if ($this->_getRefererUrl()) {
134: $redirectUrl = $this->_getRefererUrl();
135: } else {
136: $redirectUrl = $indexUrl;
137: }
138:
139: if ($notSalable) {
140: $products = array();
141: foreach ($notSalable as $item) {
142: $products[] = '"' . $item->getProduct()->getName() . '"';
143: }
144: $messages[] = Mage::helper('wishlist')->__('Unable to add the following product(s) to shopping cart: %s.', join(', ', $products));
145: }
146:
147: if ($hasOptions) {
148: $products = array();
149: foreach ($hasOptions as $item) {
150: $products[] = '"' . $item->getProduct()->getName() . '"';
151: }
152: $messages[] = Mage::helper('wishlist')->__('Product(s) %s have required options. Each of them can be added to cart separately only.', join(', ', $products));
153: }
154:
155: if ($messages) {
156: $isMessageSole = (count($messages) == 1);
157: if ($isMessageSole && count($hasOptions) == 1) {
158: $item = $hasOptions[0];
159: if ($isOwner) {
160: $item->delete();
161: }
162: $redirectUrl = $item->getProductUrl();
163: } else {
164: $wishlistSession = Mage::getSingleton('wishlist/session');
165: foreach ($messages as $message) {
166: $wishlistSession->addError($message);
167: }
168: $redirectUrl = $indexUrl;
169: }
170: }
171:
172: if ($addedItems) {
173:
174: try {
175: $wishlist->save();
176: }
177: catch (Exception $e) {
178: Mage::getSingleton('wishlist/session')->addError($this->__('Cannot update wishlist'));
179: $redirectUrl = $indexUrl;
180: }
181:
182: $products = array();
183: foreach ($addedItems as $product) {
184: $products[] = '"' . $product->getName() . '"';
185: }
186:
187: Mage::getSingleton('checkout/session')->addSuccess(
188: Mage::helper('wishlist')->__('%d product(s) have been added to shopping cart: %s.', count($addedItems), join(', ', $products))
189: );
190: }
191:
192: $cart->save()->getQuote()->collectTotals();
193:
194: Mage::helper('wishlist')->calculate();
195:
196: $this->_redirectUrl($redirectUrl);
197: }
198: }
199: