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
00031
00032
00033
00034 class Mage_Checkout_Model_Cart extends Varien_Object
00035 {
00036 protected $_summaryQty = null;
00037 protected $_productIds = null;
00038
00039 protected function _getResource()
00040 {
00041 return Mage::getResourceSingleton('checkout/cart');
00042 }
00043
00044
00045
00046
00047
00048
00049 public function getCheckoutSession()
00050 {
00051 return Mage::getSingleton('checkout/session');
00052 }
00053
00054
00055
00056
00057
00058
00059 public function getCustomerSession()
00060 {
00061 return Mage::getSingleton('customer/session');
00062 }
00063
00064 public function getItems()
00065 {
00066 if (!$this->getQuote()->getId()) {
00067 return array();
00068 }
00069 return $this->getQuote()->getItemsCollection();
00070 }
00071
00072
00073
00074
00075
00076
00077 public function getQuoteProductIds()
00078 {
00079 $products = $this->getData('product_ids');
00080 if (is_null($products)) {
00081 $products = array();
00082 foreach ($this->getQuote()->getAllItems() as $item) {
00083 $products[$item->getProductId()] = $item->getProductId();
00084 }
00085 $this->setData('product_ids', $products);
00086 }
00087 return $products;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 public function getQuote()
00097 {
00098 return $this->getCheckoutSession()->getQuote();
00099 }
00100
00101 public function init()
00102 {
00103 $this->getQuote()->setCheckoutMethod('');
00104
00105
00106
00107
00108 if ($this->getCheckoutSession()->getCheckoutState() !== Mage_Checkout_Model_Session::CHECKOUT_STATE_BEGIN) {
00109 $this->getQuote()
00110 ->removeAllAddresses()
00111 ->removePayment();
00112 $this->getCheckoutSession()->resetCheckout();
00113 }
00114
00115 if (!$this->getQuote()->hasItems()) {
00116 $this->getQuote()->getShippingAddress()
00117 ->setCollectShippingRates(false)
00118 ->removeAllShippingRates();
00119 }
00120
00121 return $this;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131 public function addOrderItem($orderItem, $qtyFlag=null)
00132 {
00133
00134 if (is_null($orderItem->getParentItem())) {
00135 $product = Mage::getModel('catalog/product')
00136 ->setStoreId(Mage::app()->getStore()->getId())
00137 ->load($orderItem->getProductId());
00138 if (!$product->getId()) {
00139 return $this;
00140 }
00141
00142 $info = $orderItem->getProductOptionByCode('info_buyRequest');
00143 $info = new Varien_Object($info);
00144 if (is_null($qtyFlag)) {
00145 $info->setQty($orderItem->getQtyOrdered());
00146 } else {
00147 $info->setQty(1);
00148 }
00149
00150 $this->addProduct($product, $info);
00151 }
00152 return $this;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161 protected function _getProduct($productInfo)
00162 {
00163 if ($productInfo instanceof Mage_Catalog_Model_Product) {
00164 $product = $productInfo;
00165 }
00166 elseif (is_int($productInfo)) {
00167 $product = Mage::getModel('catalog/product')
00168 ->setStoreId(Mage::app()->getStore()->getId())
00169 ->load($productInfo);
00170 }
00171 else {
00172
00173 }
00174 return $product;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183 protected function _getProductRequest($requestInfo)
00184 {
00185 if ($requestInfo instanceof Varien_Object) {
00186 $request = $requestInfo;
00187 }
00188 elseif (is_numeric($requestInfo)) {
00189 $request = new Varien_Object();
00190 $request->setQty($requestInfo);
00191 }
00192 else {
00193 $request = new Varien_Object($requestInfo);
00194 }
00195
00196 if (!$request->hasQty()) {
00197 $request->setQty(1);
00198 }
00199 return $request;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209 public function addProduct($product, $info=null)
00210 {
00211 $product = $this->_getProduct($product);
00212 $request = $this->_getProductRequest($info);
00213
00214
00215 if ($product->getId()) {
00216
00217 $result = $this->getQuote()->addProduct($product, $request);
00218
00219
00220
00221
00222 if (is_string($result)) {
00223
00224 $this->getCheckoutSession()->setRedirectUrl($product->getProductUrl());
00225 if ($this->getCheckoutSession()->getUseNotice() === null) {
00226 $this->getCheckoutSession()->setUseNotice(true);
00227 }
00228 Mage::throwException($result);
00229 }
00230 }
00231 else {
00232 Mage::throwException(Mage::helper('checkout')->__('Product does not exist'));
00233 }
00234
00235 Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item'=>$result, 'product'=>$product));
00236 $this->getCheckoutSession()->setLastAddedProductId($product->getId());
00237 return $this;
00238 }
00239
00240
00241
00242
00243
00244
00245
00246 public function addProductsByIds($productIds)
00247 {
00248 $allAvailable = true;
00249 $allAdded = true;
00250
00251 if (!empty($productIds)) {
00252 foreach ($productIds as $productId) {
00253 $productId = (int) $productId;
00254 if (!$productId) {
00255 continue;
00256 }
00257 $product = Mage::getModel('catalog/product')
00258 ->setStoreId(Mage::app()->getStore()->getId())
00259 ->load($productId);
00260 if ($product->getId() && $product->isVisibleInCatalog()) {
00261 try {
00262 $this->getQuote()->addProduct($product);
00263 }
00264 catch (Exception $e){
00265 $allAdded = false;
00266 }
00267 }
00268 else {
00269 $allAvailable = false;
00270 }
00271 }
00272
00273 if (!$allAvailable) {
00274 $this->getCheckoutSession()->addError(
00275 Mage::helper('checkout')->__('Some of the products you requested are unavailable')
00276 );
00277 }
00278 if (!$allAdded) {
00279 $this->getCheckoutSession()->addError(
00280 Mage::helper('checkout')->__('Some of the products you requested are not available in the desired quantity')
00281 );
00282 }
00283 }
00284 return $this;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293 public function updateItems($data)
00294 {
00295 Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
00296
00297 foreach ($data as $itemId => $itemInfo) {
00298 $item = $this->getQuote()->getItemById($itemId);
00299 if (!$item) {
00300 continue;
00301 }
00302
00303 if (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) && $itemInfo['qty']=='0')) {
00304 $this->removeItem($itemId);
00305 continue;
00306 }
00307
00308 $qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false;
00309 if ($qty > 0) {
00310 $item->setQty($qty);
00311 }
00312 }
00313
00314 Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
00315 return $this;
00316 }
00317
00318
00319
00320
00321
00322
00323
00324 public function removeItem($itemId)
00325 {
00326 $this->getQuote()->removeItem($itemId);
00327 return $this;
00328 }
00329
00330
00331
00332
00333
00334
00335 public function save()
00336 {
00337 $this->getQuote()->getBillingAddress();
00338 $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
00339 $this->getQuote()->collectTotals();
00340 $this->getQuote()->save();
00341 $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
00342 return $this;
00343 }
00344
00345 public function truncate()
00346 {
00347 foreach ($this->getQuote()->getItemsCollection() as $item) {
00348 $item->isDeleted(true);
00349 }
00350 }
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 public function getProductIds()
00461 {
00462 $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
00463 if (null === $this->_productIds) {
00464 $this->_productIds = array();
00465 if ($this->getSummaryQty()>0) {
00466 foreach ($this->getQuote()->getAllItems() as $item) {
00467 $this->_productIds[] = $item->getProductId();
00468 }
00469 }
00470 $this->_productIds = array_unique($this->_productIds);
00471 }
00472 return $this->_productIds;
00473 }
00474
00475
00476
00477
00478
00479
00480 public function getSummaryQty()
00481 {
00482 $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
00483
00484
00485
00486
00487 if (!$quoteId && Mage::getSingleton('customer/session')->isLoggedIn()) {
00488 $quote = Mage::getSingleton('checkout/session')->getQuote();
00489 $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
00490 }
00491
00492 if ($quoteId && $this->_summaryQty === null) {
00493 if (Mage::getStoreConfig('checkout/cart_link/use_qty')) {
00494 $this->_summaryQty = $this->getItemsQty();
00495 }
00496 else {
00497 $this->_summaryQty = $this->getItemsCount();
00498 }
00499 }
00500 return $this->_summaryQty;
00501 }
00502
00503
00504
00505
00506
00507
00508 public function getItemsCount()
00509 {
00510 return $this->getQuote()->getItemsCount()*1;
00511 }
00512
00513
00514
00515
00516
00517
00518 public function getItemsQty()
00519 {
00520 return $this->getQuote()->getItemsQty()*1;
00521 }
00522 }