1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Helper_Product_Compare extends Mage_Core_Helper_Url
36: {
37: 38: 39: 40: 41:
42: protected $_itemCollection;
43:
44: 45: 46: 47: 48:
49: protected $_hasItems;
50:
51: 52: 53: 54: 55:
56: protected $_allowUsedFlat = true;
57:
58: 59: 60: 61: 62:
63: protected $_customerId = null;
64:
65: 66: 67: 68: 69:
70: protected function _getSession()
71: {
72: return Mage::getSingleton('catalog/session');
73: }
74:
75: 76: 77: 78: 79:
80: public function getListUrl()
81: {
82: $itemIds = array();
83: foreach ($this->getItemCollection() as $item) {
84: $itemIds[] = $item->getId();
85: }
86:
87: $params = array(
88: 'items'=>implode(',', $itemIds),
89: Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
90: );
91:
92: return $this->_getUrl('catalog/product_compare', $params);
93: }
94:
95: 96: 97: 98: 99: 100:
101: protected function _getUrlParams($product)
102: {
103: return array(
104: 'product' => $product->getId(),
105: Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
106: );
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function getAddUrl($product)
116: {
117: return $this->_getUrl('catalog/product_compare/add', $this->_getUrlParams($product));
118: }
119:
120: 121: 122: 123: 124: 125:
126: public function getAddToWishlistUrl($product)
127: {
128: $beforeCompareUrl = Mage::getSingleton('catalog/session')->getBeforeCompareUrl();
129:
130: $params = array(
131: 'product'=>$product->getId(),
132: Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl($beforeCompareUrl)
133: );
134:
135: return $this->_getUrl('wishlist/index/add', $params);
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function getAddToCartUrl($product)
145: {
146: $beforeCompareUrl = Mage::getSingleton('catalog/session')->getBeforeCompareUrl();
147: $params = array(
148: 'product'=>$product->getId(),
149: Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl($beforeCompareUrl)
150: );
151:
152: return $this->_getUrl('checkout/cart/add', $params);
153: }
154:
155: 156: 157: 158: 159: 160:
161: public function getRemoveUrl($item)
162: {
163: $params = array(
164: 'product'=>$item->getId(),
165: Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
166: );
167: return $this->_getUrl('catalog/product_compare/remove', $params);
168: }
169:
170: 171: 172: 173: 174:
175: public function getClearListUrl()
176: {
177: $params = array(
178: Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
179: );
180: return $this->_getUrl('catalog/product_compare/clear', $params);
181: }
182:
183: 184: 185: 186: 187:
188: public function getItemCollection()
189: {
190: if (!$this->_itemCollection) {
191: $this->_itemCollection = Mage::getResourceModel('catalog/product_compare_item_collection')
192: ->useProductItem(true)
193: ->setStoreId(Mage::app()->getStore()->getId());
194:
195: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
196: $this->_itemCollection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
197: } elseif ($this->_customerId) {
198: $this->_itemCollection->setCustomerId($this->_customerId);
199: } else {
200: $this->_itemCollection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
201: }
202:
203: Mage::getSingleton('catalog/product_visibility')
204: ->addVisibleInSiteFilterToCollection($this->_itemCollection);
205:
206:
207: $this->_itemCollection->addPriceData();
208:
209: $this->_itemCollection->addAttributeToSelect('name')
210: ->addUrlRewrite()
211: ->load();
212:
213:
214: $this->_getSession()->setCatalogCompareItemsCount(count($this->_itemCollection));
215: }
216:
217: return $this->_itemCollection;
218: }
219:
220: 221: 222: 223: 224: 225:
226: public function calculate($logout = false)
227: {
228:
229: if (!$this->_getSession()->hasCatalogCompareItemsCount() && !$this->_customerId) {
230: $count = 0;
231: } else {
232:
233: $collection = Mage::getResourceModel('catalog/product_compare_item_collection')
234: ->useProductItem(true);
235: if (!$logout && Mage::getSingleton('customer/session')->isLoggedIn()) {
236: $collection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId());
237: } elseif ($this->_customerId) {
238: $collection->setCustomerId($this->_customerId);
239: } else {
240: $collection->setVisitorId(Mage::getSingleton('log/visitor')->getId());
241: }
242:
243:
244: $collection->addPriceData();
245:
246: Mage::getSingleton('catalog/product_visibility')
247: ->addVisibleInSiteFilterToCollection($collection);
248:
249: $count = $collection->getSize();
250: }
251:
252: $this->_getSession()->setCatalogCompareItemsCount($count);
253:
254: return $this;
255: }
256:
257: 258: 259: 260: 261:
262: public function getItemCount()
263: {
264: if (!$this->_getSession()->hasCatalogCompareItemsCount()) {
265: $this->calculate();
266: }
267:
268: return $this->_getSession()->getCatalogCompareItemsCount();
269: }
270:
271: 272: 273: 274: 275:
276: public function hasItems()
277: {
278: return $this->getItemCount() > 0;
279: }
280:
281: 282: 283: 284: 285: 286:
287: public function setAllowUsedFlat($flag)
288: {
289: $this->_allowUsedFlat = (bool)$flag;
290: return $this;
291: }
292:
293: 294: 295: 296: 297:
298: public function getAllowUsedFlat()
299: {
300: return $this->_allowUsedFlat;
301: }
302:
303: 304: 305: 306: 307: 308:
309: public function setCustomerId($id)
310: {
311: $this->_customerId = $id;
312: return $this;
313: }
314: }
315: