Mage_Checkout_Model_Cart Class Reference

Inheritance diagram for Mage_Checkout_Model_Cart:

Varien_Object

List of all members.

Public Member Functions

 getCheckoutSession ()
 getCustomerSession ()
 getItems ()
 getQuoteProductIds ()
 getQuote ()
 init ()
 addOrderItem ($orderItem, $qtyFlag=null)
 addProduct ($product, $info=null)
 addProductsByIds ($productIds)
 updateItems ($data)
 removeItem ($itemId)
 save ()
 truncate ()
 getProductIds ()
 getSummaryQty ()
 getItemsCount ()
 getItemsQty ()

Protected Member Functions

 _getResource ()
 _getProduct ($productInfo)
 _getProductRequest ($requestInfo)

Protected Attributes

 $_summaryQty = null
 $_productIds = null


Detailed Description

Definition at line 34 of file Cart.php.


Member Function Documentation

_getProduct ( productInfo  )  [protected]

Get product for product information

Parameters:
mixed $productInfo
Returns:
Mage_Catalog_Model_Product

Definition at line 161 of file Cart.php.

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     }

_getProductRequest ( requestInfo  )  [protected]

Get request for product add to cart procedure

Parameters:
mixed $requestInfo
Returns:
Varien_Object

Definition at line 183 of file Cart.php.

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     }

_getResource (  )  [protected]

Definition at line 39 of file Cart.php.

00040     {
00041         return Mage::getResourceSingleton('checkout/cart');
00042     }

addOrderItem ( orderItem,
qtyFlag = null 
)

Convert order item to quote item

Parameters:
Mage_Sales_Model_Order_Item $orderItem
mixed $qtyFlag if is null set product qty like in order
Returns:
Mage_Checkout_Model_Cart

Definition at line 131 of file Cart.php.

00132     {
00133         /* @var $orderItem Mage_Sales_Model_Order_Item */
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     }

addProduct ( product,
info = null 
)

Add product to shopping cart (quote)

Parameters:
int $productId
int $qty
Returns:
Mage_Checkout_Model_Cart

String we can get if prepare process has error

Definition at line 209 of file Cart.php.

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              * String we can get if prepare process has error
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     }

addProductsByIds ( productIds  ) 

Adding products to cart by ids

Parameters:
array $productIds
Returns:
Mage_Checkout_Model_Cart

Definition at line 246 of file Cart.php.

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     }

getCheckoutSession (  ) 

Retrieve checkout session model

Returns:
Mage_Checkout_Model_Session

Definition at line 49 of file Cart.php.

00050     {
00051         return Mage::getSingleton('checkout/session');
00052     }

getCustomerSession (  ) 

Retrieve custome session model

Returns:
Mage_Customer_Model_Customer

Definition at line 59 of file Cart.php.

00060     {
00061         return Mage::getSingleton('customer/session');
00062     }

getItems (  ) 

Definition at line 64 of file Cart.php.

00065     {
00066         if (!$this->getQuote()->getId()) {
00067             return array();
00068         }
00069         return $this->getQuote()->getItemsCollection();
00070     }

getItemsCount (  ) 

Get shopping cart items count

Returns:
int

Definition at line 508 of file Cart.php.

00509     {
00510         return $this->getQuote()->getItemsCount()*1;
00511     }

getItemsQty (  ) 

Get shopping cart summary qty

Returns:
decimal

Definition at line 518 of file Cart.php.

00519     {
00520         return $this->getQuote()->getItemsQty()*1;
00521     }

getProductIds (  ) 

Retrieve cart information for sidebar

Returns:
Varien_Object

Definition at line 460 of file Cart.php.

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     }

getQuote (  ) 

Retrieve current quote object

Returns:
Mage_Sales_Model_Quote

Definition at line 96 of file Cart.php.

00097     {
00098         return $this->getCheckoutSession()->getQuote();
00099     }

getQuoteProductIds (  ) 

Retrieve array of cart product ids

Returns:
array

Definition at line 77 of file Cart.php.

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     }

getSummaryQty (  ) 

Get shopping cart items summary (inchlude config settings)

Returns:
decimal

Definition at line 480 of file Cart.php.

00481     {
00482         $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
00483 
00484         //If there is no quote id in session trying to load quote
00485         //and get new quote id. This is done for cases when quote was created
00486         //not by customer (from backend for example).
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     }

init (  ) 

If user try do checkout, reset shipiing and payment data

Definition at line 101 of file Cart.php.

00102     {
00103         $this->getQuote()->setCheckoutMethod('');
00104 
00105         /**
00106          * If user try do checkout, reset shipiing and payment data
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     }

removeItem ( itemId  ) 

Remove item from cart

Parameters:
int $itemId
Returns:
Mage_Checkout_Model_Cart

Definition at line 324 of file Cart.php.

00325     {
00326         $this->getQuote()->removeItem($itemId);
00327         return $this;
00328     }

save (  ) 

Save cart

Returns:
Mage_Checkout_Model_Cart

Definition at line 335 of file Cart.php.

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     }

truncate (  ) 

Definition at line 345 of file Cart.php.

00346     {
00347         foreach ($this->getQuote()->getItemsCollection() as $item) {
00348             $item->isDeleted(true);
00349         }
00350     }

updateItems ( data  ) 

Update cart items information

Parameters:
array $data
Returns:
Mage_Checkout_Model_Cart

Definition at line 293 of file Cart.php.

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     }


Member Data Documentation

$_productIds = null [protected]

Definition at line 37 of file Cart.php.

$_summaryQty = null [protected]

Definition at line 36 of file Cart.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:51 2009 for Magento by  doxygen 1.5.8