PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Checkout/Model/Cart.php

https://github.com/gryzz/crystal_magento
PHP | 435 lines | 260 code | 39 blank | 136 comment | 45 complexity | ab9be0beefa79564c11e2e65bf175c1f MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Checkout
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Shoping cart model
  28. *
  29. * @category Mage
  30. * @package Mage_Checkout
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Checkout_Model_Cart extends Varien_Object
  34. {
  35. protected $_summaryQty = null;
  36. protected $_productIds = null;
  37. /**
  38. * Get shopping cart resource model
  39. */
  40. protected function _getResource()
  41. {
  42. return Mage::getResourceSingleton('checkout/cart');
  43. }
  44. /**
  45. * Retrieve checkout session model
  46. *
  47. * @return Mage_Checkout_Model_Session
  48. */
  49. public function getCheckoutSession()
  50. {
  51. return Mage::getSingleton('checkout/session');
  52. }
  53. /**
  54. * Retrieve custome session model
  55. *
  56. * @return Mage_Customer_Model_Customer
  57. */
  58. public function getCustomerSession()
  59. {
  60. return Mage::getSingleton('customer/session');
  61. }
  62. public function getItems()
  63. {
  64. if (!$this->getQuote()->getId()) {
  65. return array();
  66. }
  67. return $this->getQuote()->getItemsCollection();
  68. }
  69. /**
  70. * Retrieve array of cart product ids
  71. *
  72. * @return array
  73. */
  74. public function getQuoteProductIds()
  75. {
  76. $products = $this->getData('product_ids');
  77. if (is_null($products)) {
  78. $products = array();
  79. foreach ($this->getQuote()->getAllItems() as $item) {
  80. $products[$item->getProductId()] = $item->getProductId();
  81. }
  82. $this->setData('product_ids', $products);
  83. }
  84. return $products;
  85. }
  86. /**
  87. * Get quote object associated with cart. By default it is current customer session quote
  88. *
  89. * @return Mage_Sales_Model_Quote
  90. */
  91. public function getQuote()
  92. {
  93. if (!$this->hasData('quote')) {
  94. $this->setData('quote', $this->getCheckoutSession()->getQuote());
  95. }
  96. return $this->_getData('quote');
  97. }
  98. /**
  99. * Initialize cart quote state to be able use it on cart page
  100. */
  101. public function init()
  102. {
  103. $this->getQuote()->setCheckoutMethod('');
  104. /**
  105. * If user try do checkout, reset shipiing and payment data
  106. */
  107. if ($this->getCheckoutSession()->getCheckoutState() !== Mage_Checkout_Model_Session::CHECKOUT_STATE_BEGIN) {
  108. $this->getQuote()
  109. ->removeAllAddresses()
  110. ->removePayment();
  111. $this->getCheckoutSession()->resetCheckout();
  112. }
  113. if (!$this->getQuote()->hasItems()) {
  114. $this->getQuote()->getShippingAddress()
  115. ->setCollectShippingRates(false)
  116. ->removeAllShippingRates();
  117. }
  118. return $this;
  119. }
  120. /**
  121. * Convert order item to quote item
  122. *
  123. * @param Mage_Sales_Model_Order_Item $orderItem
  124. * @param mixed $qtyFlag if is null set product qty like in order
  125. * @return Mage_Checkout_Model_Cart
  126. */
  127. public function addOrderItem($orderItem, $qtyFlag=null)
  128. {
  129. /* @var $orderItem Mage_Sales_Model_Order_Item */
  130. if (is_null($orderItem->getParentItem())) {
  131. $product = Mage::getModel('catalog/product')
  132. ->setStoreId(Mage::app()->getStore()->getId())
  133. ->load($orderItem->getProductId());
  134. if (!$product->getId()) {
  135. return $this;
  136. }
  137. $info = $orderItem->getProductOptionByCode('info_buyRequest');
  138. $info = new Varien_Object($info);
  139. if (is_null($qtyFlag)) {
  140. $info->setQty($orderItem->getQtyOrdered());
  141. } else {
  142. $info->setQty(1);
  143. }
  144. $this->addProduct($product, $info);
  145. }
  146. return $this;
  147. }
  148. /**
  149. * Get product object based on requested product information
  150. *
  151. * @param mixed $productInfo
  152. * @return Mage_Catalog_Model_Product
  153. */
  154. protected function _getProduct($productInfo)
  155. {
  156. if ($productInfo instanceof Mage_Catalog_Model_Product) {
  157. $product = $productInfo;
  158. } elseif (is_int($productInfo)) {
  159. $product = Mage::getModel('catalog/product')
  160. ->setStoreId(Mage::app()->getStore()->getId())
  161. ->load($productInfo);
  162. } else {
  163. Mage::throwException(Mage::helper('checkout')->__('The product could not be found.'));
  164. }
  165. return $product;
  166. }
  167. /**
  168. * Get request for product add to cart procedure
  169. *
  170. * @param mixed $requestInfo
  171. * @return Varien_Object
  172. */
  173. protected function _getProductRequest($requestInfo)
  174. {
  175. if ($requestInfo instanceof Varien_Object) {
  176. $request = $requestInfo;
  177. } elseif (is_numeric($requestInfo)) {
  178. $request = new Varien_Object();
  179. $request->setQty($requestInfo);
  180. } else {
  181. $request = new Varien_Object($requestInfo);
  182. }
  183. if (!$request->hasQty()) {
  184. $request->setQty(1);
  185. }
  186. return $request;
  187. }
  188. /**
  189. * Add product to shopping cart (quote)
  190. *
  191. * @param int $productId
  192. * @param int $qty
  193. * @return Mage_Checkout_Model_Cart
  194. */
  195. public function addProduct($product, $info=null)
  196. {
  197. $product = $this->_getProduct($product);
  198. $request = $this->_getProductRequest($info);
  199. //Check if current product already exists in cart
  200. $productId = $product->getId();
  201. $items = $this->getQuote()->getAllItems();
  202. $quoteProduct = null;
  203. foreach ($items as $item) {
  204. if ($item->getProductId() == $productId) {
  205. $quoteProduct = $item;
  206. break;
  207. }
  208. }
  209. if ($product->getStockItem()) {
  210. $minimumQty = $product->getStockItem()->getMinSaleQty();
  211. //If product was not found in cart and there is set minimal qty for it
  212. if($minimumQty && $minimumQty > 0 && $request->getQty() < $minimumQty && $quoteProduct === null){
  213. $request->setQty($minimumQty);
  214. }
  215. }
  216. if ($product->getId()) {
  217. $result = $this->getQuote()->addProduct($product, $request);
  218. /**
  219. * String we can get if prepare process has error
  220. */
  221. if (is_string($result)) {
  222. $this->getCheckoutSession()->setRedirectUrl($product->getProductUrl());
  223. if ($this->getCheckoutSession()->getUseNotice() === null) {
  224. $this->getCheckoutSession()->setUseNotice(true);
  225. }
  226. Mage::throwException($result);
  227. }
  228. }
  229. else {
  230. Mage::throwException(Mage::helper('checkout')->__('The product does not exist.'));
  231. }
  232. Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item'=>$result, 'product'=>$product));
  233. $this->getCheckoutSession()->setLastAddedProductId($product->getId());
  234. return $this;
  235. }
  236. /**
  237. * Adding products to cart by ids
  238. *
  239. * @param array $productIds
  240. * @return Mage_Checkout_Model_Cart
  241. */
  242. public function addProductsByIds($productIds)
  243. {
  244. $allAvailable = true;
  245. $allAdded = true;
  246. if (!empty($productIds)) {
  247. foreach ($productIds as $productId) {
  248. $productId = (int) $productId;
  249. if (!$productId) {
  250. continue;
  251. }
  252. $product = Mage::getModel('catalog/product')
  253. ->setStoreId(Mage::app()->getStore()->getId())
  254. ->load($productId);
  255. if ($product->getId() && $product->isVisibleInCatalog()) {
  256. try {
  257. $this->getQuote()->addProduct($product);
  258. } catch (Exception $e){
  259. $allAdded = false;
  260. }
  261. } else {
  262. $allAvailable = false;
  263. }
  264. }
  265. if (!$allAvailable) {
  266. $this->getCheckoutSession()->addError(
  267. Mage::helper('checkout')->__('Some of the requested products are unavailable.')
  268. );
  269. }
  270. if (!$allAdded) {
  271. $this->getCheckoutSession()->addError(
  272. Mage::helper('checkout')->__('Some of the requested products are not available in the desired quantity.')
  273. );
  274. }
  275. }
  276. return $this;
  277. }
  278. /**
  279. * Update cart items information
  280. *
  281. * @param array $data
  282. * @return Mage_Checkout_Model_Cart
  283. */
  284. public function updateItems($data)
  285. {
  286. Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
  287. foreach ($data as $itemId => $itemInfo) {
  288. $item = $this->getQuote()->getItemById($itemId);
  289. if (!$item) {
  290. continue;
  291. }
  292. if (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) && $itemInfo['qty']=='0')) {
  293. $this->removeItem($itemId);
  294. continue;
  295. }
  296. $qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false;
  297. if ($qty > 0) {
  298. $item->setQty($qty);
  299. }
  300. }
  301. Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
  302. return $this;
  303. }
  304. /**
  305. * Remove item from cart
  306. *
  307. * @param int $itemId
  308. * @return Mage_Checkout_Model_Cart
  309. */
  310. public function removeItem($itemId)
  311. {
  312. $this->getQuote()->removeItem($itemId);
  313. return $this;
  314. }
  315. /**
  316. * Save cart
  317. *
  318. * @return Mage_Checkout_Model_Cart
  319. */
  320. public function save()
  321. {
  322. $this->getQuote()->getBillingAddress();
  323. $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
  324. $this->getQuote()->collectTotals();
  325. $this->getQuote()->save();
  326. $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
  327. /**
  328. * Cart save usually called after chenges with cart items.
  329. */
  330. Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
  331. return $this;
  332. }
  333. public function truncate()
  334. {
  335. foreach ($this->getQuote()->getItemsCollection() as $item) {
  336. $item->isDeleted(true);
  337. }
  338. }
  339. public function getProductIds()
  340. {
  341. $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
  342. if (null === $this->_productIds) {
  343. $this->_productIds = array();
  344. if ($this->getSummaryQty()>0) {
  345. foreach ($this->getQuote()->getAllItems() as $item) {
  346. $this->_productIds[] = $item->getProductId();
  347. }
  348. }
  349. $this->_productIds = array_unique($this->_productIds);
  350. }
  351. return $this->_productIds;
  352. }
  353. /**
  354. * Get shopping cart items summary (inchlude config settings)
  355. *
  356. * @return decimal
  357. */
  358. public function getSummaryQty()
  359. {
  360. $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
  361. //If there is no quote id in session trying to load quote
  362. //and get new quote id. This is done for cases when quote was created
  363. //not by customer (from backend for example).
  364. if (!$quoteId && Mage::getSingleton('customer/session')->isLoggedIn()) {
  365. $quote = Mage::getSingleton('checkout/session')->getQuote();
  366. $quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
  367. }
  368. if ($quoteId && $this->_summaryQty === null) {
  369. if (Mage::getStoreConfig('checkout/cart_link/use_qty')) {
  370. $this->_summaryQty = $this->getItemsQty();
  371. }
  372. else {
  373. $this->_summaryQty = $this->getItemsCount();
  374. }
  375. }
  376. return $this->_summaryQty;
  377. }
  378. /**
  379. * Get shopping cart items count
  380. *
  381. * @return int
  382. */
  383. public function getItemsCount()
  384. {
  385. return $this->getQuote()->getItemsCount()*1;
  386. }
  387. /**
  388. * Get shopping cart summary qty
  389. *
  390. * @return decimal
  391. */
  392. public function getItemsQty()
  393. {
  394. return $this->getQuote()->getItemsQty()*1;
  395. }
  396. }