/
Configurable.php
471 lines (432 loc) · 15.4 KB
/
Configurable.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?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.
*
* @category Mage
* @package Mage_Catalog
* @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Configurable product type implementation
*
* This type builds in product attributes and existing simple products
*
* @category Mage
* @package Mage_Catalog
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Catalog_Model_Product_Type_Configurable extends Mage_Catalog_Model_Product_Type_Abstract
{
const TYPE_CODE = 'configurable';
/**
* Attributes which used for configurable product
*
* @var array
*/
protected $_usedProductAttributeIds = null;
protected $_usedProductAttributes = null;
protected $_configurableAttributes = null;
protected $_usedProductIds = null;
protected $_usedProducts = null;
protected $_isComposite = true;
/**
* Retrieve product type attributes
*
* @return array
*/
public function getEditableAttributes()
{
if (is_null($this->_editableAttributes)) {
$this->_editableAttributes = parent::getEditableAttributes();
foreach ($this->_editableAttributes as $index => $attribute) {
if ($this->getUsedProductAttributeIds()
&& in_array($attribute->getAttributeId(), $this->getUsedProductAttributeIds())) {
unset($this->_editableAttributes[$index]);
}
}
}
return $this->_editableAttributes;
}
/**
* Checkin attribute availability for create superproduct
*
* @param Mage_Eav_Model_Entity_Attribute $attribute
* @return bool
*/
public function canUseAttribute(Mage_Eav_Model_Entity_Attribute $attribute)
{
$allow = $attribute->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
&& $attribute->getIsVisible()
&& $attribute->getIsConfigurable()
&& $attribute->usesSource();
return $allow;
}
/**
* Declare attribute identifiers used for asign subproducts
*
* @param array $ids
* @return Mage_Catalog_Model_Product_Type_Configurable
*/
public function setUsedProductAttributeIds($ids)
{
$this->_usedProductAttributes = array();
$this->_configurableAttributes= array();
foreach ($ids as $attributeId) {
$this->_usedProductAttributes[] = $this->getAttributeById($attributeId);
$this->_configurableAttributes[]= Mage::getModel('catalog/product_type_configurable_attribute')
->setProductAttribute($this->getAttributeById($attributeId));
}
$this->_usedProductAttributeIds = $ids;
return $this;
}
/**
* Retrieve identifiers of used product attributes
*
* @return array
*/
public function getUsedProductAttributeIds()
{
if (is_null($this->_usedProductAttributeIds)) {
$this->_usedProductAttributeIds = array();
foreach ($this->getUsedProductAttributes() as $attribute) {
$this->_usedProductAttributeIds[] = $attribute->getId();
}
}
return $this->_usedProductAttributeIds;
}
/**
* Retrieve used product attributes
*
* @return array
*/
public function getUsedProductAttributes()
{
if (is_null($this->_usedProductAttributes)) {
$this->_usedProductAttributes = array();
foreach ($this->getConfigurableAttributes() as $attribute) {
$this->_usedProductAttributes[$attribute->getProductAttribute()->getId()] = $attribute->getProductAttribute();
}
}
return $this->_usedProductAttributes;
}
/**
* Retrieve configurable attrbutes data
*
* @return array
*/
public function getConfigurableAttributes()
{
Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
if (is_null($this->_configurableAttributes)) {
$this->_configurableAttributes = $this->getConfigurableAttributeCollection()
->orderByPosition()
->load();
}
Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
return $this->_configurableAttributes;
}
public function getConfigurableAttributesAsArray()
{
$res = array();
foreach ($this->getConfigurableAttributes() as $attribute) {
$label = $attribute->getLabel() ? $attribute->getLabel() : $attribute->getProductAttribute()->getFrontend()->getLabel();
$res[] = array(
'id' => $attribute->getId(),
'label' => $label,
'position' => $attribute->getPosition(),
'values' => $attribute->getPrices() ? $attribute->getPrices() : array(),
'attribute_id' => $attribute->getProductAttribute()->getId(),
'attribute_code'=> $attribute->getProductAttribute()->getAttributeCode(),
'frontend_label'=> $attribute->getProductAttribute()->getFrontend()->getLabel(),
);
}
return $res;
}
public function getConfigurableAttributeCollection()
{
return Mage::getResourceModel('catalog/product_type_configurable_attribute_collection')
->setProductFilter($this->getProduct());
}
/**
* Retrieve subproducts identifiers
*
* @return array
*/
public function getUsedProductIds()
{
if (is_null($this->_usedProductIds)) {
$this->_usedProductIds = array();
foreach ($this->getUsedProducts() as $product) {
$this->_usedProductIds[] = $product->getId();
}
}
return $this->_usedProductIds;
}
/**
* Retrieve array of "subproducts"
*
* @return array
*/
public function getUsedProducts($requiredAttributeIds=null)
{
Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
if (is_null($this->_usedProducts)) {
if (is_null($requiredAttributeIds) && is_null($this->_configurableAttributes)) {
// If used products load before attributes, we will load attributes.
$this->getConfigurableAttributes();
// After attributes loading products loaded too.
return $this->_usedProducts;
}
$this->_usedProducts = array();
$collection = $this->getUsedProductCollection()
->addAttributeToSelect('*')
->addFilterByRequiredOptions();
if (is_array($requiredAttributeIds)) {
foreach ($requiredAttributeIds as $attributeId) {
$attribute = $this->getAttributeById($attributeId);
if (!is_null($attribute))
$collection->addAttributeToFilter($attribute->getAttributeCode(), array('notnull'=>1));
}
}
foreach ($collection as $product) {
$this->_usedProducts[] = $product;
}
}
Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
return $this->_usedProducts;
}
/**
* Retrieve related products collection
*
* @return unknown
*/
public function getUsedProductCollection()
{
$collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
->setProductFilter($this->getProduct());
if (!is_null($this->getStoreFilter())) {
$collection->addStoreFilter($this->getStoreFilter());
}
return $collection;
}
public function beforeSave()
{
parent::beforeSave();
$this->getProduct()->canAffectOptions(false);
if ($this->getProduct()->getCanSaveConfigurableAttributes()) {
$this->getProduct()->canAffectOptions(true);
if ($data = $this->getProduct()->getConfigurableAttributesData()) {
if (!empty($data)) {
foreach ($data as $attribute) {
if (!empty($attribute['values'])) {
$this->getProduct()->setTypeHasOptions(true);
$this->getProduct()->setTypeHasRequiredOptions(true);
break;
}
}
}
}
}
}
/**
* Save configurable product depended data
*
* @return Mage_Catalog_Model_Product_Type_Configurable
*/
public function save()
{
parent::save();
/**
* Save Attributes Information
*/
if ($data = $this->getProduct()->getConfigurableAttributesData()) {
foreach ($data as $attributeData) {
$id = isset($attributeData['id']) ? $attributeData['id'] : null;
$attribute = Mage::getModel('catalog/product_type_configurable_attribute')
->setData($attributeData)
->setId($id)
->setStoreId($this->getProduct()->getStoreId())
->setProductId($this->getProduct()->getId())
->save();
}
}
/**
* Save product relations
*/
$data = $this->getProduct()->getConfigurableProductsData();
if (is_array($data)) {
$productIds = array_keys($data);
Mage::getResourceModel('catalog/product_type_configurable')
->saveProducts($this->getProduct()->getId(), $productIds);
}
return $this;
}
/**
* Check is product available for sale
*
* @return bool
*/
public function isSalable()
{
$salable = $this->getProduct()->getIsSalable();
if (!is_null($salable) && !$salable) {
return $salable;
}
$salable = false;
foreach ($this->getUsedProducts() as $product) {
$salable = $salable || $product->isSalable();
}
return $salable;
}
/**
* Retrieve used product by attribute values
* $attrbutesInfo = array(
* $attributeId => $attributeValue
* )
* @param array $attrbutesInfo
* @return
*/
public function getProductByAttributes($attributesInfo)
{
foreach ($this->getUsedProducts() as $product) {
$checkRes = true;
foreach ($attributesInfo as $attributeId => $attributeValue) {
$code = $this->getAttributeById($attributeId)->getAttributeCode();
if ($product->getData($code) != $attributeValue) {
$checkRes = false;
}
}
if ($checkRes) {
return $product;
}
}
return null;
}
public function getSelectedAttributesInfo()
{
$attributes = array();
Varien_Profiler::start('CONFIGURABLE:'.__METHOD__);
if ($attributesOption = $this->getProduct()->getCustomOption('attributes')) {
$data = unserialize($attributesOption->getValue());
$usedAttributes = $this->getUsedProductAttributes();
foreach ($data as $attributeId => $attributeValue) {
if (isset($usedAttributes[$attributeId])) {
$attribute = $usedAttributes[$attributeId];
$label = $attribute->getFrontend()->getLabel();
if ($attribute->getSourceModel()) {
$value = $attribute->getSource()->getOptionText($attributeValue);
}
else {
$value = '';
}
$attributes[] = array('label'=>$label, 'value'=>$value);
}
}
}
Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__);
return $attributes;
}
/**
* Initialize product(s) for add to cart process
*
* @param Varien_Object $buyRequest
* @return unknown
*/
public function prepareForCart(Varien_Object $buyRequest)
{
if ($attributes = $buyRequest->getSuperAttribute()) {
$result = parent::prepareForCart($buyRequest);
if (is_array($result)) {
$product = $this->getProduct();
/**
* $attributes = array($attributeId=>$attributeValue)
*/
if ($subProduct = $this->getProductByAttributes($attributes)) {
$product->addCustomOption('attributes', serialize($attributes));
$product->addCustomOption('product_qty_'.$subProduct->getId(), 1, $subProduct);
$product->addCustomOption('simple_product', $subProduct->getId(), $subProduct);
return $result;
}
}
}
return Mage::helper('catalog')->__('Please specify the product option(s)');
}
/**
* Prepare additional options/information for order item which will be
* created from this product
*
* @return attay
*/
public function getOrderOptions()
{
$options = parent::getOrderOptions();
$options['attributes_info'] = $this->getSelectedAttributesInfo();
if ($simpleOption = $this->getProduct()->getCustomOption('simple_product')) {
$options['simple_name'] = $simpleOption->getProduct()->getName();
$options['simple_sku'] = $simpleOption->getProduct()->getSku();
}
return $options;
}
/**
* Check is virtual product
*
* @return bool
*/
public function isVirtual()
{
if ($productOption = $this->getProduct()->getCustomOption('simple_product')) {
if ($product = $productOption->getProduct()) {
/* @var $product Mage_Catalog_Model_Product */
return $product->getTypeInstance()->isVirtual();
}
}
return parent::isVirtual();
}
/**
* Return true if product has options
*
* @return bool
*/
public function hasOptions()
{
if ($this->getProduct()->getOptions()) {
return true;
}
$attributes = $this->getConfigurableAttributes();
if (count($attributes)) {
foreach ($attributes as $key => $attribute) {
/** @var Mage_Catalog_Model_Product_Type_Configurable_Attribute $attribute */
if ($attribute->getData('prices')) {
return true;
}
}
}
return false;
}
/**
* Return product weight based on simple product
* weight or configurable product weight
*
* @return decimal
*/
public function getWeight()
{
if ($this->getProduct()->hasCustomOptions() && ($simpleProductOption = $this->getProduct()->getCustomOption('simple_product'))) {
$simpleProduct = $simpleProductOption->getProduct();
return $simpleProduct->getWeight();
} else {
return $this->getProduct()->getData('weight');
}
}
}