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_Wishlist_Helper_Data extends Mage_Core_Helper_Abstract
36: {
37: 38: 39:
40: const XML_PATH_WISHLIST_LINK_USE_QTY = 'wishlist/wishlist_link/use_qty';
41:
42: 43: 44:
45: const XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK = 'cataloginventory/options/show_out_of_stock';
46:
47: 48: 49: 50: 51:
52: protected $_currentCustomer = null;
53:
54: 55: 56: 57: 58:
59: protected $_wishlist = null;
60:
61: 62: 63: 64: 65:
66: protected $_productCollection = null;
67:
68: 69: 70: 71: 72:
73: protected $_wishlistItemCollection = null;
74:
75: 76: 77: 78: 79:
80: protected function _getCustomerSession()
81: {
82: return Mage::getSingleton('customer/session');
83: }
84:
85: 86: 87: 88: 89:
90: protected function _isCustomerLogIn()
91: {
92: return $this->_getCustomerSession()->isLoggedIn();
93: }
94:
95: 96: 97: 98: 99:
100: protected function _getCurrentCustomer()
101: {
102: return $this->getCustomer();
103: }
104:
105: 106: 107: 108: 109:
110: public function setCustomer(Mage_Customer_Model_Customer $customer)
111: {
112: $this->_currentCustomer = $customer;
113: }
114:
115: 116: 117: 118: 119:
120: public function getCustomer()
121: {
122: if (!$this->_currentCustomer && $this->_getCustomerSession()->isLoggedIn()) {
123: $this->_currentCustomer = $this->_getCustomerSession()->getCustomer();
124: }
125: return $this->_currentCustomer;
126: }
127:
128: 129: 130: 131: 132:
133: public function getWishlist()
134: {
135: if (is_null($this->_wishlist)) {
136: if (Mage::registry('shared_wishlist')) {
137: $this->_wishlist = Mage::registry('shared_wishlist');
138: }
139: elseif (Mage::registry('wishlist')) {
140: $this->_wishlist = Mage::registry('wishlist');
141: }
142: else {
143: $this->_wishlist = Mage::getModel('wishlist/wishlist');
144: if ($this->getCustomer()) {
145: $this->_wishlist->loadByCustomer($this->getCustomer());
146: }
147: }
148: }
149: return $this->_wishlist;
150: }
151:
152: 153: 154: 155: 156: 157: 158:
159: public function hasItems()
160: {
161: return $this->getWishlist()->getItemsCount() > 0;
162: }
163:
164: 165: 166: 167: 168: 169:
170: public function getItemCount()
171: {
172: $storedDisplayType = $this->_getCustomerSession()->getWishlistDisplayType();
173: $currentDisplayType = Mage::getStoreConfig(self::XML_PATH_WISHLIST_LINK_USE_QTY);
174:
175: $storedDisplayOutOfStockProducts = $this->_getCustomerSession()->getDisplayOutOfStockProducts();
176: $currentDisplayOutOfStockProducts = Mage::getStoreConfig(self::XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK);
177: if (!$this->_getCustomerSession()->hasWishlistItemCount()
178: || ($currentDisplayType != $storedDisplayType)
179: || $this->_getCustomerSession()->hasDisplayOutOfStockProducts()
180: || ($currentDisplayOutOfStockProducts != $storedDisplayOutOfStockProducts)) {
181: $this->calculate();
182: }
183:
184: return $this->_getCustomerSession()->getWishlistItemCount();
185: }
186:
187: 188: 189: 190: 191: 192: 193: 194: 195: 196:
197: public function getItemCollection()
198: {
199: return $this->getProductCollection();
200: }
201:
202: 203: 204: 205: 206:
207: protected function _createWishlistItemCollection()
208: {
209: return $this->getWishlist()->getItemCollection();
210: }
211:
212: 213: 214: 215: 216:
217: public function getWishlistItemCollection()
218: {
219: if (is_null($this->_wishlistItemCollection)) {
220: $this->_wishlistItemCollection = $this->_createWishlistItemCollection();
221: }
222: return $this->_wishlistItemCollection;
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232:
233: public function getProductCollection()
234: {
235: if (is_null($this->_productCollection)) {
236: $this->_productCollection = $this->getWishlist()
237: ->getProductCollection();
238:
239: Mage::getSingleton('catalog/product_visibility')
240: ->addVisibleInSiteFilterToCollection($this->_productCollection);
241: }
242: return $this->_productCollection;
243: }
244:
245: 246: 247: 248: 249: 250:
251: protected function _getUrlStore($item)
252: {
253: $storeId = null;
254: $product = null;
255: if ($item instanceof Mage_Wishlist_Model_Item) {
256: $product = $item->getProduct();
257: } elseif ($item instanceof Mage_Catalog_Model_Product) {
258: $product = $item;
259: }
260: if ($product) {
261: if ($product->isVisibleInSiteVisibility()) {
262: $storeId = $product->getStoreId();
263: }
264: else if ($product->hasUrlDataObject()) {
265: $storeId = $product->getUrlDataObject()->getStoreId();
266: }
267: }
268: return Mage::app()->getStore($storeId);
269: }
270:
271: 272: 273: 274: 275: 276:
277: public function getRemoveUrl($item)
278: {
279: return $this->_getUrl('wishlist/index/remove',
280: array('item' => $item->getWishlistItemId())
281: );
282: }
283:
284: 285: 286: 287: 288: 289:
290: public function getConfigureUrl($item)
291: {
292: return $this->_getUrl('wishlist/index/configure', array(
293: 'item' => $item->getWishlistItemId()
294: ));
295: }
296:
297: 298: 299: 300: 301: 302: 303:
304: public function getAddUrl($item)
305: {
306: return $this->getAddUrlWithParams($item);
307: }
308:
309: 310: 311: 312: 313: 314: 315:
316: public function getMoveFromCartUrl($itemId)
317: {
318: return $this->_getUrl('wishlist/index/fromcart', array('item' => $itemId));
319: }
320:
321: 322: 323: 324: 325: 326: 327:
328: public function getUpdateUrl($item)
329: {
330: $itemId = null;
331: if ($item instanceof Mage_Catalog_Model_Product) {
332: $itemId = $item->getWishlistItemId();
333: }
334: if ($item instanceof Mage_Wishlist_Model_Item) {
335: $itemId = $item->getId();
336: }
337:
338: if ($itemId) {
339: return $this->_getUrl('wishlist/index/updateItemOptions', array('id' => $itemId));
340: }
341:
342: return false;
343: }
344:
345: 346: 347: 348: 349: 350: 351: 352:
353: public function getAddUrlWithParams($item, array $params = array())
354: {
355: $productId = null;
356: if ($item instanceof Mage_Catalog_Model_Product) {
357: $productId = $item->getEntityId();
358: }
359: if ($item instanceof Mage_Wishlist_Model_Item) {
360: $productId = $item->getProductId();
361: }
362:
363: if ($productId) {
364: $params['product'] = $productId;
365: return $this->_getUrlStore($item)->getUrl('wishlist/index/add', $params);
366: }
367:
368: return false;
369: }
370:
371: 372: 373: 374: 375: 376:
377: public function getAddToCartUrl($item)
378: {
379: $urlParamName = Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED;
380: $continueUrl = Mage::helper('core')->urlEncode(
381: Mage::getUrl('*/*/*', array(
382: '_current' => true,
383: '_use_rewrite' => true,
384: '_store_to_url' => true,
385: ))
386: );
387:
388: $urlParamName = Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED;
389: $params = array(
390: 'item' => is_string($item) ? $item : $item->getWishlistItemId(),
391: $urlParamName => $continueUrl
392: );
393: return $this->_getUrlStore($item)->getUrl('wishlist/index/cart', $params);
394: }
395:
396: 397: 398: 399: 400: 401:
402: public function getSharedAddToCartUrl($item)
403: {
404: $continueUrl = Mage::helper('core')->urlEncode(Mage::getUrl('*/*/*', array(
405: '_current' => true,
406: '_use_rewrite' => true,
407: '_store_to_url' => true,
408: )));
409:
410: $urlParamName = Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED;
411: $params = array(
412: 'item' => is_string($item) ? $item : $item->getWishlistItemId(),
413: $urlParamName => $continueUrl
414: );
415: return $this->_getUrlStore($item)->getUrl('wishlist/shared/cart', $params);
416: }
417:
418: 419: 420: 421: 422: 423: 424:
425: public function getAddToCartUrlBase64($item)
426: {
427: return $this->getAddToCartUrl($item);
428: }
429:
430: 431: 432: 433: 434: 435:
436: public function getListUrl($wishlistId = null)
437: {
438: $params = array();
439: if ($wishlistId) {
440: $params['wishlist_id'] = $wishlistId;
441: }
442: return $this->_getUrl('wishlist', $params);
443: }
444:
445: 446: 447: 448: 449:
450: public function isAllow()
451: {
452: if ($this->isModuleOutputEnabled() && Mage::getStoreConfig('wishlist/general/active')) {
453: return true;
454: }
455: return false;
456: }
457:
458: 459: 460: 461: 462:
463: public function isAllowInCart()
464: {
465: return $this->isAllow() && $this->getCustomer();
466: }
467:
468: 469: 470: 471: 472:
473: public function getCustomerName()
474: {
475: $customer = $this->_getCurrentCustomer();
476: if ($customer) {
477: return $customer->getName();
478: }
479: }
480:
481: 482: 483: 484: 485: 486:
487: public function ($wishlistId = null)
488: {
489: $customer = $this->_getCurrentCustomer();
490: if ($customer) {
491: $key = $customer->getId() . ',' . $customer->getEmail();
492: $params = array(
493: 'data' => Mage::helper('core')->urlEncode($key),
494: '_secure' => false,
495: );
496: }
497: if ($wishlistId) {
498: $params['wishlist_id'] = $wishlistId;
499: }
500: return $this->_getUrl(
501: 'rss/index/wishlist',
502: $params
503: );
504: }
505:
506: 507: 508: 509: 510:
511: public function ()
512: {
513: return Mage::getStoreConfigFlag('rss/wishlist/active');
514: }
515:
516: 517: 518: 519: 520:
521: public function ()
522: {
523: return $this->__('Please, enter your comments...');
524: }
525:
526: 527: 528: 529: 530:
531: public function getDefaultWishlistName()
532: {
533: return $this->__('Wishlist');
534: }
535:
536: 537: 538: 539: 540: 541: 542:
543: public function calculate()
544: {
545: $session = $this->_getCustomerSession();
546: $count = 0;
547: if ($this->getCustomer()) {
548: $collection = $this->getWishlistItemCollection()->setInStockFilter(true);
549: if (Mage::getStoreConfig(self::XML_PATH_WISHLIST_LINK_USE_QTY)) {
550: $count = $collection->getItemsQty();
551: } else {
552: $count = $collection->getSize();
553: }
554: $session->setWishlistDisplayType(Mage::getStoreConfig(self::XML_PATH_WISHLIST_LINK_USE_QTY));
555: $session->setDisplayOutOfStockProducts(
556: Mage::getStoreConfig(self::XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK)
557: );
558: }
559: $session->setWishlistItemCount($count);
560: Mage::dispatchEvent('wishlist_items_renewed');
561: return $this;
562: }
563:
564: 565: 566: 567: 568:
569: public function isDisplayQty()
570: {
571: return Mage::getStoreConfig(self::XML_PATH_WISHLIST_LINK_USE_QTY);
572: }
573: }
574: