00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Sales_Model_Quote_Item extends Mage_Sales_Model_Quote_Item_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_eventPrefix = 'sales_quote_item';
00043
00044
00045
00046
00047
00048
00049
00050
00051 protected $_eventObject = 'item';
00052
00053
00054
00055
00056
00057
00058 protected $_quote;
00059
00060
00061
00062
00063
00064
00065 protected $_options = array();
00066
00067
00068
00069
00070
00071
00072 protected $_optionsByCode = array();
00073
00074
00075
00076
00077
00078
00079 protected $_notRepresentOptions = array('info_buyRequest');
00080
00081
00082
00083
00084
00085 protected function _construct()
00086 {
00087 $this->_init('sales/quote_item');
00088 }
00089
00090
00091
00092
00093
00094
00095 protected function _beforeSave()
00096 {
00097 parent::_beforeSave();
00098 $this->setIsVirtual($this->getProduct()->getIsVirtual());
00099 if ($this->getQuote()) {
00100 $this->setQuoteId($this->getQuote()->getId());
00101 }
00102 return $this;
00103 }
00104
00105
00106
00107
00108
00109
00110
00111 public function setQuote(Mage_Sales_Model_Quote $quote)
00112 {
00113 $this->_quote = $quote;
00114 $this->setQuoteId($quote->getId());
00115 return $this;
00116 }
00117
00118
00119
00120
00121
00122
00123 public function getQuote()
00124 {
00125 return $this->_quote;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134 protected function _prepareQty($qty)
00135 {
00136 $qty = Mage::app()->getLocale()->getNumber($qty);
00137 $qty = ($qty > 0) ? $qty : 1;
00138 return $qty;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147 public function addQty($qty)
00148 {
00149 $oldQty = $this->getQty();
00150 $qty = $this->_prepareQty($qty);
00151
00152
00153
00154
00155
00156 if (!$this->getParentItem() || !$this->getId()) {
00157 $this->setQtyToAdd($qty);
00158 $this->setQty($oldQty+$qty);
00159 }
00160 return $this;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 public function setQty($qty)
00170 {
00171 $qty = $this->_prepareQty($qty);
00172
00173
00174 $oldQty = $this->_getData('qty');
00175 $this->setData('qty', $qty);
00176
00177 Mage::dispatchEvent('sales_quote_item_qty_set_after', array('item'=>$this));
00178
00179 if ($this->getQuote() && $this->getQuote()->getIgnoreOldQty()) {
00180 return $this;
00181 }
00182 if ($this->getUseOldQty()) {
00183 $this->setData('qty', $oldQty);
00184 }
00185 return $this;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 public function getQtyOptions()
00198 {
00199 $productIds = array();
00200 $return = array();
00201 foreach ($this->getOptions() as $option) {
00202
00203 if (is_object($option->getProduct()) && $option->getProduct()->getId() != $this->getProduct()->getId()
00204 && !isset($productIds[$option->getProduct()->getId()])) {
00205 $productIds[$option->getProduct()->getId()] = $option->getProduct()->getId();
00206 }
00207 }
00208
00209 foreach ($productIds as $productId) {
00210 if ($option = $this->getOptionByCode('product_qty_' . $productId)) {
00211 $return[$productId] = $option;
00212 }
00213 }
00214
00215 return $return;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224 public function setProduct($product)
00225 {
00226 if ($this->getQuote()) {
00227 $product->setStoreId($this->getQuote()->getStoreId());
00228 }
00229 $this->setData('product', $product)
00230 ->setProductId($product->getId())
00231 ->setProductType($product->getTypeId())
00232 ->setSku($this->getProduct()->getSku())
00233 ->setName($product->getName())
00234 ->setWeight($this->getProduct()->getWeight())
00235 ->setTaxClassId($product->getTaxClassId())
00236 ->setCost($product->getCost())
00237 ->setIsQtyDecimal($product->getIsQtyDecimal());
00238
00239 Mage::dispatchEvent('sales_quote_item_set_product', array(
00240 'product' => $product,
00241 'quote_item'=>$this
00242 ));
00243
00244
00245
00246
00247
00248
00249 return $this;
00250 }
00251
00252
00253
00254
00255
00256
00257 public function getProduct()
00258 {
00259 $product = $this->_getData('product');
00260 if (($product === null) && $this->getProductId()) {
00261 $product = Mage::getModel('catalog/product')
00262 ->setStoreId($this->getQuote()->getStoreId())
00263 ->load($this->getProductId());
00264 $this->setProduct($product);
00265 }
00266
00267
00268
00269
00270 $product->setFinalPrice(null);
00271 $product->setCustomOptions($this->_optionsByCode);
00272 return $product;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281 public function representProduct($product)
00282 {
00283 $itemProduct = $this->getProduct();
00284 if ($itemProduct->getId() != $product->getId()) {
00285 return false;
00286 }
00287
00288 $itemOptions = $this->getOptions();
00289 $productOptions = $product->getCustomOptions();
00290
00291 foreach ($itemOptions as $option) {
00292 $code = $option->getCode();
00293 if (in_array($code, $this->_notRepresentOptions )) {
00294 continue;
00295 }
00296 if ( !isset($productOptions[$code])
00297 || ($productOptions[$code]->getValue() === null)
00298 || $productOptions[$code]->getValue() != $option->getValue()) {
00299 return false;
00300 }
00301 }
00302 return true;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 public function compare($item)
00312 {
00313 if ($this->getProductId() != $item->getProductId()) {
00314 return false;
00315 }
00316 foreach ($this->getOptions() as $option) {
00317 if ($itemOption = $item->getOptionByCode($option->getCode())) {
00318 $itemOptionValue = $itemOption->getValue();
00319 $optionValue = $option->getValue();
00320
00321
00322 if (is_string($itemOptionValue) && is_string($optionValue)) {
00323 $_itemOptionValue = @unserialize($itemOptionValue);
00324 $_optionValue = @unserialize($optionValue);
00325 if (is_array($_itemOptionValue) && is_array($_optionValue)) {
00326 $itemOptionValue = $_itemOptionValue;
00327 $optionValue = $_optionValue;
00328
00329 unset($itemOptionValue['qty'], $itemOptionValue['uenc'], $optionValue['qty'], $optionValue['uenc']);
00330 }
00331 }
00332
00333 if ($itemOptionValue != $optionValue) {
00334 return false;
00335 }
00336 }
00337 else {
00338 return false;
00339 }
00340 }
00341 return true;
00342 }
00343
00344
00345
00346
00347
00348
00349 public function getProductType()
00350 {
00351 if ($option = $this->getOptionByCode('product_type')) {
00352 return $option->getValue();
00353 }
00354 if ($product = $this->getProduct()) {
00355 return $product->getTypeId();
00356 }
00357 return $this->_getData('product_type');
00358 }
00359
00360
00361
00362
00363
00364
00365 public function getRealProductType()
00366 {
00367 return $this->_getData('product_type');
00368 }
00369
00370
00371
00372
00373
00374
00375
00376 public function toArray(array $arrAttributes=array())
00377 {
00378 $data = parent::toArray($arrAttributes);
00379
00380 if ($product = $this->getProduct()) {
00381 $data['product'] = $product->toArray();
00382 }
00383 return $data;
00384 }
00385
00386
00387
00388
00389
00390
00391
00392 public function setOptions($options)
00393 {
00394 foreach ($options as $option) {
00395 $this->addOption($option);
00396 }
00397 return $this;
00398 }
00399
00400
00401
00402
00403
00404
00405 public function getOptions()
00406 {
00407 return $this->_options;
00408 }
00409
00410
00411
00412
00413
00414
00415
00416 public function addOption($option)
00417 {
00418 if (is_array($option)) {
00419 $option = Mage::getModel('sales/quote_item_option')->setData($option)
00420 ->setItem($this);
00421 }
00422 elseif (($option instanceof Varien_Object) && !($option instanceof Mage_Sales_Model_Quote_Item_Option)) {
00423 $option = Mage::getModel('sales/quote_item_option')->setData($option->getData())
00424 ->setProduct($option->getProduct())
00425 ->setItem($this);
00426 }
00427 elseif($option instanceof Mage_Sales_Model_Quote_Item_Option) {
00428 $option->setItem($this);
00429 }
00430 else {
00431 Mage::throwException(Mage::helper('sales')->__('Invalid item option format'));
00432 }
00433
00434 if ($exOption = $this->getOptionByCode($option->getCode())) {
00435 $exOption->addData($option->getData());
00436 }
00437 else {
00438 $this->_addOptionCode($option);
00439 $this->_options[] = $option;
00440 }
00441 return $this;
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455 public function updateQtyOption(Varien_Object $option, $value)
00456 {
00457 $optionProduct = $option->getProduct();
00458
00459 $options = $this->getQtyOptions();
00460 if (isset($options[$optionProduct->getId()])) {
00461 $options[$optionProduct->getId()]->setValue($value);
00462 }
00463
00464 $this->getProduct()->getTypeInstance(true)
00465 ->updateQtyOption($this->getOptions(), $option, $value, $this->getProduct());
00466
00467 return $this;
00468 }
00469
00470
00471
00472
00473
00474
00475
00476 public function removeOption($code)
00477 {
00478 if ($option = $this->getOptionByCode($code)) {
00479 $option->isDeleted(true);
00480 }
00481 return $this;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490 protected function _addOptionCode($option)
00491 {
00492 if (!isset($this->_optionsByCode[$option->getCode()])) {
00493 $this->_optionsByCode[$option->getCode()] = $option;
00494 }
00495 else {
00496 Mage::throwException(Mage::helper('sales')->__('Item option with code %s already exist', $option->getCode()));
00497 }
00498 return $this;
00499 }
00500
00501
00502
00503
00504
00505
00506
00507 public function getOptionByCode($code)
00508 {
00509 if (isset($this->_optionsByCode[$code]) && !$this->_optionsByCode[$code]->isDeleted()) {
00510 return $this->_optionsByCode[$code];
00511 }
00512 return null;
00513 }
00514
00515
00516
00517
00518
00519
00520 protected function _afterSave()
00521 {
00522 foreach ($this->_options as $index => $option) {
00523 if ($option->isDeleted()) {
00524 $option->delete();
00525 unset($this->_options[$index]);
00526 unset($this->_optionsByCode[$option->getCode()]);
00527 }
00528 else {
00529 $option->save();
00530 }
00531 }
00532 return parent::_afterSave();
00533 }
00534
00535
00536
00537
00538
00539
00540 public function __clone()
00541 {
00542 parent::__clone();
00543 $options = $this->getOptions();
00544 $this->_quote = null;
00545 $this->_options = array();
00546 $this->_optionsByCode = array();
00547 foreach ($options as $option) {
00548 $this->addOption(clone $option);
00549 }
00550 return $this;
00551 }
00552 }