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) 2012 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: /**
28: * Cart crosssell list
29: *
30: * @category Mage
31: * @package Mage_Checkout
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Checkout_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
35: {
36: /**
37: * Items quantity will be capped to this value
38: *
39: * @var int
40: */
41: protected $_maxItemCount = 4;
42:
43: /**
44: * Get crosssell items
45: *
46: * @return array
47: */
48: public function getItems()
49: {
50: $items = $this->getData('items');
51: if (is_null($items)) {
52: $items = array();
53: $ninProductIds = $this->_getCartProductIds();
54: if ($ninProductIds) {
55: $lastAdded = (int) $this->_getLastAddedProductId();
56: if ($lastAdded) {
57: $collection = $this->_getCollection()
58: ->addProductFilter($lastAdded);
59: if (!empty($ninProductIds)) {
60: $collection->addExcludeProductFilter($ninProductIds);
61: }
62: $collection->setPositionOrder()->load();
63:
64: foreach ($collection as $item) {
65: $ninProductIds[] = $item->getId();
66: $items[] = $item;
67: }
68: }
69:
70: if (count($items) < $this->_maxItemCount) {
71: $filterProductIds = array_merge($this->_getCartProductIds(), $this->_getCartProductIdsRel());
72: $collection = $this->_getCollection()
73: ->addProductFilter($filterProductIds)
74: ->addExcludeProductFilter($ninProductIds)
75: ->setPageSize($this->_maxItemCount-count($items))
76: ->setGroupBy()
77: ->setPositionOrder()
78: ->load();
79: foreach ($collection as $item) {
80: $items[] = $item;
81: }
82: }
83:
84: }
85:
86: $this->setData('items', $items);
87: }
88: return $items;
89: }
90:
91: /**
92: * Count items
93: *
94: * @return int
95: */
96: public function getItemCount()
97: {
98: return count($this->getItems());
99: }
100:
101: /**
102: * Get ids of products that are in cart
103: *
104: * @return array
105: */
106: protected function _getCartProductIds()
107: {
108: $ids = $this->getData('_cart_product_ids');
109: if (is_null($ids)) {
110: $ids = array();
111: foreach ($this->getQuote()->getAllItems() as $item) {
112: if ($product = $item->getProduct()) {
113: $ids[] = $product->getId();
114: }
115: }
116: $this->setData('_cart_product_ids', $ids);
117: }
118: return $ids;
119: }
120:
121: /**
122: * Retrieve Array of product ids which have special relation with products in Cart
123: * For example simple product as part of Grouped product
124: *
125: * @return array
126: */
127: protected function _getCartProductIdsRel()
128: {
129: $productIds = array();
130: foreach ($this->getQuote()->getAllItems() as $quoteItem) {
131: $productTypeOpt = $quoteItem->getOptionByCode('product_type');
132: if ($productTypeOpt instanceof Mage_Sales_Model_Quote_Item_Option
133: && $productTypeOpt->getValue() == Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE
134: && $productTypeOpt->getProductId()
135: ) {
136: $productIds[] = $productTypeOpt->getProductId();
137: }
138: }
139:
140: return $productIds;
141: }
142:
143: /**
144: * Get last product ID that was added to cart and remove this information from session
145: *
146: * @return int
147: */
148: protected function _getLastAddedProductId()
149: {
150: return Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
151: }
152:
153: /**
154: * Get quote instance
155: *
156: * @return Mage_Sales_Model_Quote
157: */
158: public function getQuote()
159: {
160: return Mage::getSingleton('checkout/session')->getQuote();
161: }
162:
163: /**
164: * Get crosssell products collection
165: *
166: * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection
167: */
168: protected function _getCollection()
169: {
170: $collection = Mage::getModel('catalog/product_link')->useCrossSellLinks()
171: ->getProductCollection()
172: ->setStoreId(Mage::app()->getStore()->getId())
173: ->addStoreFilter()
174: ->setPageSize($this->_maxItemCount);
175: $this->_addProductAttributesAndPrices($collection);
176:
177: Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
178: Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
179: Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
180:
181: return $collection;
182: }
183: }
184: