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_Checkout_Model_Cart extends Varien_Object implements Mage_Checkout_Model_Cart_Interface
35: {
36: 37: 38: 39: 40:
41: protected $_summaryQty;
42:
43: 44: 45: 46: 47:
48: protected $_productIds;
49:
50: 51: 52: 53: 54:
55: protected function _getResource()
56: {
57: return Mage::getResourceSingleton('checkout/cart');
58: }
59:
60: 61: 62: 63: 64:
65: public function getCheckoutSession()
66: {
67: return Mage::getSingleton('checkout/session');
68: }
69:
70: 71: 72: 73: 74:
75: public function getCustomerSession()
76: {
77: return Mage::getSingleton('customer/session');
78: }
79:
80: 81: 82: 83: 84:
85: public function getItems()
86: {
87: if (!$this->getQuote()->getId()) {
88: return array();
89: }
90: return $this->getQuote()->getItemsCollection();
91: }
92:
93: 94: 95: 96: 97:
98: public function getQuoteProductIds()
99: {
100: $products = $this->getData('product_ids');
101: if (is_null($products)) {
102: $products = array();
103: foreach ($this->getQuote()->getAllItems() as $item) {
104: $products[$item->getProductId()] = $item->getProductId();
105: }
106: $this->setData('product_ids', $products);
107: }
108: return $products;
109: }
110:
111: 112: 113: 114: 115:
116: public function getQuote()
117: {
118: if (!$this->hasData('quote')) {
119: $this->setData('quote', $this->getCheckoutSession()->getQuote());
120: }
121: return $this->_getData('quote');
122: }
123:
124: 125: 126: 127: 128: 129:
130: public function setQuote(Mage_Sales_Model_Quote $quote)
131: {
132: $this->setData('quote', $quote);
133: return $this;
134: }
135:
136: 137: 138: 139: 140:
141: public function init()
142: {
143: $this->getQuote()->setCheckoutMethod('');
144:
145: 146: 147:
148: if ($this->getCheckoutSession()->getCheckoutState() !== Mage_Checkout_Model_Session::CHECKOUT_STATE_BEGIN) {
149: $this->getQuote()
150: ->removeAllAddresses()
151: ->removePayment();
152: $this->getCheckoutSession()->resetCheckout();
153: }
154:
155: if (!$this->getQuote()->hasItems()) {
156: $this->getQuote()->getShippingAddress()
157: ->setCollectShippingRates(false)
158: ->removeAllShippingRates();
159: }
160:
161: return $this;
162: }
163:
164: 165: 166: 167: 168: 169: 170:
171: public function addOrderItem($orderItem, $qtyFlag=null)
172: {
173:
174: if (is_null($orderItem->getParentItem())) {
175: $product = Mage::getModel('catalog/product')
176: ->setStoreId(Mage::app()->getStore()->getId())
177: ->load($orderItem->getProductId());
178: if (!$product->getId()) {
179: return $this;
180: }
181:
182: $info = $orderItem->getProductOptionByCode('info_buyRequest');
183: $info = new Varien_Object($info);
184: if (is_null($qtyFlag)) {
185: $info->setQty($orderItem->getQtyOrdered());
186: } else {
187: $info->setQty(1);
188: }
189:
190: $this->addProduct($product, $info);
191: }
192: return $this;
193: }
194:
195: 196: 197: 198: 199: 200:
201: protected function _getProduct($productInfo)
202: {
203: $product = null;
204: if ($productInfo instanceof Mage_Catalog_Model_Product) {
205: $product = $productInfo;
206: } elseif (is_int($productInfo) || is_string($productInfo)) {
207: $product = Mage::getModel('catalog/product')
208: ->setStoreId(Mage::app()->getStore()->getId())
209: ->load($productInfo);
210: }
211: $currentWebsiteId = Mage::app()->getStore()->getWebsiteId();
212: if (!$product
213: || !$product->getId()
214: || !is_array($product->getWebsiteIds())
215: || !in_array($currentWebsiteId, $product->getWebsiteIds())
216: ) {
217: Mage::throwException(Mage::helper('checkout')->__('The product could not be found.'));
218: }
219: return $product;
220: }
221:
222: 223: 224: 225: 226: 227:
228: protected function _getProductRequest($requestInfo)
229: {
230: if ($requestInfo instanceof Varien_Object) {
231: $request = $requestInfo;
232: } elseif (is_numeric($requestInfo)) {
233: $request = new Varien_Object(array('qty' => $requestInfo));
234: } else {
235: $request = new Varien_Object($requestInfo);
236: }
237:
238: if (!$request->hasQty()) {
239: $request->setQty(1);
240: }
241:
242: return $request;
243: }
244:
245: 246: 247: 248: 249: 250: 251:
252: public function addProduct($productInfo, $requestInfo=null)
253: {
254: $product = $this->_getProduct($productInfo);
255: $request = $this->_getProductRequest($requestInfo);
256:
257: $productId = $product->getId();
258:
259: if ($product->getStockItem()) {
260: $minimumQty = $product->getStockItem()->getMinSaleQty();
261:
262: if ($minimumQty && $minimumQty > 0 && $request->getQty() < $minimumQty
263: && !$this->getQuote()->hasProductId($productId)
264: ){
265: $request->setQty($minimumQty);
266: }
267: }
268:
269: if ($productId) {
270: try {
271: $result = $this->getQuote()->addProduct($product, $request);
272: } catch (Mage_Core_Exception $e) {
273: $this->getCheckoutSession()->setUseNotice(false);
274: $result = $e->getMessage();
275: }
276: 277: 278:
279: if (is_string($result)) {
280: $redirectUrl = ($product->hasOptionsValidationFail())
281: ? $product->getUrlModel()->getUrl(
282: $product,
283: array('_query' => array('startcustomization' => 1))
284: )
285: : $product->getProductUrl();
286: $this->getCheckoutSession()->setRedirectUrl($redirectUrl);
287: if ($this->getCheckoutSession()->getUseNotice() === null) {
288: $this->getCheckoutSession()->setUseNotice(true);
289: }
290: Mage::throwException($result);
291: }
292: } else {
293: Mage::throwException(Mage::helper('checkout')->__('The product does not exist.'));
294: }
295:
296: Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));
297: $this->getCheckoutSession()->setLastAddedProductId($productId);
298: return $this;
299: }
300:
301: 302: 303: 304: 305: 306:
307: public function addProductsByIds($productIds)
308: {
309: $allAvailable = true;
310: $allAdded = true;
311:
312: if (!empty($productIds)) {
313: foreach ($productIds as $productId) {
314: $productId = (int) $productId;
315: if (!$productId) {
316: continue;
317: }
318: $product = $this->_getProduct($productId);
319: if ($product->getId() && $product->isVisibleInCatalog()) {
320: try {
321: $this->getQuote()->addProduct($product);
322: } catch (Exception $e){
323: $allAdded = false;
324: }
325: } else {
326: $allAvailable = false;
327: }
328: }
329:
330: if (!$allAvailable) {
331: $this->getCheckoutSession()->addError(
332: Mage::helper('checkout')->__('Some of the requested products are unavailable.')
333: );
334: }
335: if (!$allAdded) {
336: $this->getCheckoutSession()->addError(
337: Mage::helper('checkout')->__('Some of the requested products are not available in the desired quantity.')
338: );
339: }
340: }
341: return $this;
342: }
343:
344: 345: 346: 347: 348: 349: 350: 351: 352: 353:
354: public function suggestItemsQty($data)
355: {
356: foreach ($data as $itemId => $itemInfo) {
357: if (!isset($itemInfo['qty'])) {
358: continue;
359: }
360: $qty = (float) $itemInfo['qty'];
361: if ($qty <= 0) {
362: continue;
363: }
364:
365: $quoteItem = $this->getQuote()->getItemById($itemId);
366: if (!$quoteItem) {
367: continue;
368: }
369:
370: $product = $quoteItem->getProduct();
371: if (!$product) {
372: continue;
373: }
374:
375:
376: $stockItem = $product->getStockItem();
377: if (!$stockItem) {
378: continue;
379: }
380:
381: $data[$itemId]['before_suggest_qty'] = $qty;
382: $data[$itemId]['qty'] = $stockItem->suggestQty($qty);
383: }
384:
385: return $data;
386: }
387:
388: 389: 390: 391: 392: 393:
394: public function updateItems($data)
395: {
396: Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
397:
398:
399: $messageFactory = Mage::getSingleton('core/message');
400: $session = $this->getCheckoutSession();
401: $qtyRecalculatedFlag = false;
402: foreach ($data as $itemId => $itemInfo) {
403: $item = $this->getQuote()->getItemById($itemId);
404: if (!$item) {
405: continue;
406: }
407:
408: if (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) && $itemInfo['qty']=='0')) {
409: $this->removeItem($itemId);
410: continue;
411: }
412:
413: $qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false;
414: if ($qty > 0) {
415: $item->setQty($qty);
416:
417: $itemInQuote = $this->getQuote()->getItemById($item->getId());
418:
419: if (!$itemInQuote && $item->getHasError()) {
420: Mage::throwException($item->getMessage());
421: }
422:
423: if (isset($itemInfo['before_suggest_qty']) && ($itemInfo['before_suggest_qty'] != $qty)) {
424: $qtyRecalculatedFlag = true;
425: $message = $messageFactory->notice(Mage::helper('checkout')->__('Quantity was recalculated from %d to %d', $itemInfo['before_suggest_qty'], $qty));
426: $session->addQuoteItemMessage($item->getId(), $message);
427: }
428: }
429: }
430:
431: if ($qtyRecalculatedFlag) {
432: $session->addNotice(
433: Mage::helper('checkout')->__('Some products quantities were recalculated because of quantity increment mismatch')
434: );
435: }
436:
437: Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
438: return $this;
439: }
440:
441: 442: 443: 444: 445: 446:
447: public function removeItem($itemId)
448: {
449: $this->getQuote()->removeItem($itemId);
450: return $this;
451: }
452:
453: 454: 455: 456: 457:
458: public function save()
459: {
460: Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));
461:
462: $this->getQuote()->getBillingAddress();
463: $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
464: $this->getQuote()->collectTotals();
465: $this->getQuote()->save();
466: $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
467: 468: 469:
470: Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
471: return $this;
472: }
473:
474: 475: 476:
477: public function saveQuote()
478: {
479: $this->save();
480: }
481:
482: 483: 484: 485: 486:
487: public function truncate()
488: {
489: $this->getQuote()->removeAllItems();
490: return $this;
491: }
492:
493: public function getProductIds()
494: {
495: $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
496: if (null === $this->_productIds) {
497: $this->_productIds = array();
498: if ($this->getSummaryQty()>0) {
499: foreach ($this->getQuote()->getAllItems() as $item) {
500: $this->_productIds[] = $item->getProductId();
501: }
502: }
503: $this->_productIds = array_unique($this->_productIds);
504: }
505: return $this->_productIds;
506: }
507:
508: 509: 510: 511: 512:
513: public function getSummaryQty()
514: {
515: $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
516:
517:
518:
519:
520: if (!$quoteId && Mage::getSingleton('customer/session')->isLoggedIn()) {
521: $quote = Mage::getSingleton('checkout/session')->getQuote();
522: $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
523: }
524:
525: if ($quoteId && $this->_summaryQty === null) {
526: if (Mage::getStoreConfig('checkout/cart_link/use_qty')) {
527: $this->_summaryQty = $this->getItemsQty();
528: } else {
529: $this->_summaryQty = $this->getItemsCount();
530: }
531: }
532: return $this->_summaryQty;
533: }
534:
535: 536: 537: 538: 539:
540: public function getItemsCount()
541: {
542: return $this->getQuote()->getItemsCount()*1;
543: }
544:
545: 546: 547: 548: 549:
550: public function getItemsQty()
551: {
552: return $this->getQuote()->getItemsQty()*1;
553: }
554:
555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566:
567: public function updateItem($itemId, $requestInfo = null, $updatingParams = null)
568: {
569: try {
570: $item = $this->getQuote()->getItemById($itemId);
571: if (!$item) {
572: Mage::throwException(Mage::helper('checkout')->__('Quote item does not exist.'));
573: }
574: $productId = $item->getProduct()->getId();
575: $product = $this->_getProduct($productId);
576: $request = $this->_getProductRequest($requestInfo);
577:
578: if ($product->getStockItem()) {
579: $minimumQty = $product->getStockItem()->getMinSaleQty();
580:
581: if ($minimumQty && ($minimumQty > 0)
582: && ($request->getQty() < $minimumQty)
583: && !$this->getQuote()->hasProductId($productId)
584: ) {
585: $request->setQty($minimumQty);
586: }
587: }
588:
589: $result = $this->getQuote()->updateItem($itemId, $request, $updatingParams);
590: } catch (Mage_Core_Exception $e) {
591: $this->getCheckoutSession()->setUseNotice(false);
592: $result = $e->getMessage();
593: }
594:
595: 596: 597:
598: if (is_string($result)) {
599: if ($this->getCheckoutSession()->getUseNotice() === null) {
600: $this->getCheckoutSession()->setUseNotice(true);
601: }
602: Mage::throwException($result);
603: }
604:
605: Mage::dispatchEvent('checkout_cart_product_update_after', array(
606: 'quote_item' => $result,
607: 'product' => $product
608: ));
609: $this->getCheckoutSession()->setLastAddedProductId($productId);
610: return $result;
611: }
612: }
613: