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_Block_Cart_Crosssell extends Mage_Catalog_Block_Product_Abstract
00035 {
00036
00037
00038
00039
00040
00041 protected $_maxItemCount = 4;
00042
00043
00044
00045
00046
00047
00048 public function getItems()
00049 {
00050 $items = $this->getData('items');
00051 if (is_null($items)) {
00052 $items = array();
00053 if ($ninProductIds = $this->_getCartProductIds()) {
00054 if ($lastAdded = (int) $this->_getLastAddedProductId()) {
00055 $collection = $this->_getCollection()
00056 ->addProductFilter($lastAdded);
00057 if (!empty($ninProductIds)) {
00058 $collection->addExcludeProductFilter($ninProductIds);
00059 }
00060 $collection->load();
00061
00062 foreach ($collection as $item) {
00063 $ninProductIds[] = $item->getId();
00064 $items[] = $item;
00065 }
00066 }
00067
00068 if (count($items)<$this->_maxItemCount) {
00069 $collection = $this->_getCollection()
00070 ->addProductFilter($this->_getCartProductIds())
00071 ->addExcludeProductFilter($ninProductIds)
00072 ->setPageSize($this->_maxItemCount-count($items))
00073 ->setGroupBy()
00074 ->setRandomOrder()
00075 ->load();
00076 foreach ($collection as $item) {
00077 $items[] = $item;
00078 }
00079 }
00080 }
00081
00082 $this->setData('items', $items);
00083 }
00084 return $items;
00085 }
00086
00087
00088
00089
00090
00091
00092 public function getItemCount()
00093 {
00094 return count($this->getItems());
00095 }
00096
00097
00098
00099
00100
00101
00102 protected function _getCartProductIds()
00103 {
00104 $ids = $this->getData('_cart_product_ids');
00105 if (is_null($ids)) {
00106 $ids = array();
00107 foreach ($this->getQuote()->getAllItems() as $item) {
00108 if ($product = $item->getProduct()) {
00109 $ids[] = $product->getId();
00110 }
00111 }
00112 $this->setData('_cart_product_ids', $ids);
00113 }
00114 return $ids;
00115 }
00116
00117
00118
00119
00120
00121
00122 protected function _getLastAddedProductId()
00123 {
00124 return Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
00125 }
00126
00127
00128
00129
00130
00131
00132 public function getQuote()
00133 {
00134 return Mage::getSingleton('checkout/session')->getQuote();
00135 }
00136
00137
00138
00139
00140
00141
00142 protected function _getCollection()
00143 {
00144 $collection = Mage::getModel('catalog/product_link')->useCrossSellLinks()
00145 ->getProductCollection()
00146 ->setStoreId(Mage::app()->getStore()->getId())
00147 ->addStoreFilter()
00148 ->setPageSize($this->_maxItemCount);
00149 $this->_addProductAttributesAndPrices($collection);
00150
00151 Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
00152 Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
00153 Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
00154
00155 return $collection;
00156 }
00157 }