/
Item.php
256 lines (223 loc) · 7.28 KB
/
Item.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Wishlist
* @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Wishlist item model
*
* @category Mage
* @package Mage_Wishlist
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Wishlist_Model_Item extends Mage_Core_Model_Abstract
{
const EXCEPTION_CODE_NOT_SALABLE = 901;
const EXCEPTION_CODE_HAS_REQUIRED_OPTIONS = 902;
const EXCEPTION_CODE_IS_GROUPED_PRODUCT = 903;
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'wishlist_item';
/**
* Parameter name in event
*
* In observe method you can use $observer->getEvent()->getItem() in this case
*
* @var string
*/
protected $_eventObject = 'item';
/**
* Initialize resource model
*
*/
protected function _construct()
{
$this->_init('wishlist/item');
}
/**
* Retrieve resource instance wrapper
*
* @return Mage_Wishlist_Model_Mysql4_Item
*/
protected function _getResource()
{
return parent::_getResource();
}
/**
* Validate wish list item data
*
* @throws Mage_Core_Exception
* @return bool
*/
public function validate()
{
if (!$this->getWishlistId()) {
Mage::throwException(Mage::helper('wishlist')->__('Can\'t specify wishlist'));
}
if (!$this->getProductId()) {
Mage::throwException(Mage::helper('wishlist')->__('Can\'t specify product'));
}
return true;
}
/**
* Check required data
*
* @return Mage_Wishlist_Model_Item
*/
protected function _beforeSave()
{
parent::_beforeSave();
// validate required item data
$this->validate();
// set current store id if it is not defined
if (is_null($this->getStoreId())) {
$this->setStoreId(Mage::app()->getStore()->getId());
}
// set current date if added at data is not defined
if (is_null($this->getAddedAt())) {
$this->setAddedAt(Mage::getSingleton('core/date')->gmtDate());
}
return $this;
}
/**
* Retrieve wishlist item data as array
*
* @deprecated since 1.4.0.0
* @return array
*/
public function getDataForSave()
{
$data = array();
$data['product_id'] = $this->getProductId();
$data['wishlist_id'] = $this->getWishlistId();
$data['added_at'] = $this->getAddedAt() ? $this->getAddedAt() : Mage::getSingleton('core/date')->gmtDate();
$data['description'] = $this->getDescription();
$data['store_id'] = $this->getStoreId() ? $this->getStoreId() : Mage::app()->getStore()->getId();
return $data;
}
/**
* Load item by product, wishlist and shared stores
*
* @param int $wishlistId
* @param int $productId
* @param array $sharedStores
* @return Mage_Wishlist_Model_Item
*/
public function loadByProductWishlist($wishlistId, $productId, $sharedStores)
{
$this->_getResource()->loadByProductWishlist($this, $wishlistId, $productId, $sharedStores);
$this->_afterLoad();
$this->setOrigData();
return $this;
}
/**
* Retrieve item product instance
*
* @throws Mage_Core_Exception
* @return Mage_Catalog_Model_Product
*/
public function getProduct()
{
$product = $this->_getData('product');
if (is_null($product)) {
if (!$this->getProductId()) {
Mage::throwException(Mage::helper('wishlist')->__('Can\'t specify product'));
}
$product = Mage::getModel('catalog/product')
->load($this->getProductId());
$this->setData('product', $product);
}
return $product;
}
/**
* Add or Move item product to shopping cart
*
* Return true if product was successful added or exception with code
* Return false for disabled or unvisible products
*
* @throws Mage_Core_Exception
* @param Mage_Checkout_Model_Cart $cart
* @param bool $delete delete the item after successful add to cart
* @return bool
*/
public function addToCart(Mage_Checkout_Model_Cart $cart, $delete = false)
{
$product = $this->getProduct();
if (Mage_Catalog_Model_Product_Type::TYPE_GROUPED == $product->getTypeId()) {
throw new Mage_Core_Exception(null, self::EXCEPTION_CODE_IS_GROUPED_PRODUCT);
}
$product->setQty(1);
$storeId = $this->getStoreId();
if ($product->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_ENABLED) {
return false;
}
if (!$product->isVisibleInSiteVisibility()) {
if ($product->getStoreId() == $storeId) {
return false;
}
$urlData = Mage::getResourceSingleton('catalog/url')
->getRewriteByProductStore(array($product->getId() => $storeId));
if (!isset($urlData[$product->getId()])) {
return false;
}
$product->setUrlDataObject(new Varien_Object($urlData));
$visibility = $product->getUrlDataObject()->getVisibility();
if (!in_array($visibility, $product->getVisibleInSiteVisibilities())) {
return false;
}
}
if (!$product->isSalable()) {
throw new Mage_Core_Exception(null, self::EXCEPTION_CODE_NOT_SALABLE);
}
if ($product->getTypeInstance(true)->hasRequiredOptions($product)) {
throw new Mage_Core_Exception(null, self::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS);
}
$cart->addProduct($product);
if (!$product->isVisibleInSiteVisibility()) {
$cart->getQuote()->getItemByProduct($product)->setStoreId($storeId);
}
if ($delete) {
$this->delete();
}
return true;
}
/**
* Retrieve Product View Page URL
*
* If product has required options add special key to URL
*
* @return string
*/
public function getProductUrl()
{
$product = $this->getProduct();
$query = array();
if ($product->getTypeInstance(true)->hasRequiredOptions($product)) {
$query['options'] = 'cart';
}
return $product->getUrlModel()->getUrl($product, array('_query' => $query));
}
}