PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Catalog/Model/Product/Compare/List.php

https://bitbucket.org/jit_bec/shopifine
PHP | 131 lines | 51 code | 11 blank | 69 comment | 4 complexity | a7ab49b465233022ceea70ba302ca857 MD5 | raw file
Possible License(s): LGPL-3.0
  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_Catalog
  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. * Product Compare List Model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Compare_List extends Varien_Object
  34. {
  35. /**
  36. * Add product to Compare List
  37. *
  38. * @param int|Mage_Catalog_Model_Product $product
  39. * @return Mage_Catalog_Model_Product_Compare_List
  40. */
  41. public function addProduct($product)
  42. {
  43. /* @var $item Mage_Catalog_Model_Product_Compare_Item */
  44. $item = Mage::getModel('catalog/product_compare_item');
  45. $this->_addVisitorToItem($item);
  46. $item->loadByProduct($product);
  47. if (!$item->getId()) {
  48. $item->addProductData($product);
  49. $item->save();
  50. }
  51. return $this;
  52. }
  53. /**
  54. * Add products to compare list
  55. *
  56. * @param array $productIds
  57. * @return Mage_Catalog_Model_Product_Compare_List
  58. */
  59. public function addProducts($productIds)
  60. {
  61. if (is_array($productIds)) {
  62. foreach ($productIds as $productId) {
  63. $this->addProduct($productId);
  64. }
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Retrieve Compare Items Collection
  70. *
  71. * @return product_compare_item_collection
  72. */
  73. public function getItemCollection()
  74. {
  75. return Mage::getResourceModel('catalog/product_compare_item_collection');
  76. }
  77. /**
  78. * Remove product from compare list
  79. *
  80. * @param int|Mage_Catalog_Model_Product $product
  81. * @return Mage_Catalog_Model_Product_Compare_List
  82. */
  83. public function removeProduct($product)
  84. {
  85. /* @var $item Mage_Catalog_Model_Product_Compare_Item */
  86. $item = Mage::getModel('catalog/product_compare_item');
  87. $this->_addVisitorToItem($item);
  88. $item->loadByProduct($product);
  89. if ($item->getId()) {
  90. $item->delete();
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Add visitor and customer data to compare item
  96. *
  97. * @param Mage_Catalog_Model_Product_Compare_Item $item
  98. * @return Mage_Catalog_Model_Product_Compare_List
  99. */
  100. protected function _addVisitorToItem($item)
  101. {
  102. $item->addVisitorId(Mage::getSingleton('log/visitor')->getId());
  103. if (Mage::getSingleton('customer/session')->isLoggedIn()) {
  104. $item->addCustomerData(Mage::getSingleton('customer/session')->getCustomer());
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Check has compare items by visitor/customer
  110. *
  111. * @param int $customerId
  112. * @param int $visitorId
  113. * @return bool
  114. */
  115. public function hasItems($customerId, $visitorId)
  116. {
  117. return Mage::getResourceSingleton('catalog/product_compare_item')
  118. ->getCount($customerId, $visitorId);
  119. }
  120. }