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