Mage_Sales_Model_Quote_Item Class Reference

Inheritance diagram for Mage_Sales_Model_Quote_Item:

Mage_Sales_Model_Quote_Item_Abstract Mage_Core_Model_Abstract Varien_Object

List of all members.

Public Member Functions

 setQuote (Mage_Sales_Model_Quote $quote)
 getQuote ()
 addQty ($qty)
 setQty ($qty)
 getQtyOptions ()
 setProduct ($product)
 getProduct ()
 representProduct ($product)
 compare ($item)
 getProductType ()
 getRealProductType ()
 toArray (array $arrAttributes=array())
 setOptions ($options)
 getOptions ()
 addOption ($option)
 updateQtyOption (Varien_Object $option, $value)
 removeOption ($code)
 getOptionByCode ($code)
 __clone ()

Protected Member Functions

 _construct ()
 _beforeSave ()
 _prepareQty ($qty)
 _addOptionCode ($option)
 _afterSave ()

Protected Attributes

 $_eventPrefix = 'sales_quote_item'
 $_eventObject = 'item'
 $_quote
 $_options = array()
 $_optionsByCode = array()
 $_notRepresentOptions = array('info_buyRequest')


Detailed Description

Definition at line 35 of file Item.php.


Member Function Documentation

__clone (  ) 

Clone quote item

Returns:
Mage_Sales_Model_Quote_Item

Reimplemented from Mage_Sales_Model_Quote_Item_Abstract.

Definition at line 540 of file Item.php.

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     }

_addOptionCode ( option  )  [protected]

Register option code

Parameters:
Mage_Sales_Model_Quote_Item_Option $option
Returns:
Mage_Sales_Model_Quote_Item

Definition at line 490 of file Item.php.

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     }

_afterSave (  )  [protected]

Save item options

Returns:
Mage_Sales_Model_Quote_Item

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 520 of file Item.php.

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     }

_beforeSave (  )  [protected]

Quote Item Before Save prepare data process

Returns:
Mage_Sales_Model_Quote_Item

Reimplemented from Mage_Sales_Model_Quote_Item_Abstract.

Definition at line 95 of file Item.php.

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     }

_construct (  )  [protected]

Initialize resource model

Reimplemented from Varien_Object.

Definition at line 85 of file Item.php.

00086     {
00087         $this->_init('sales/quote_item');
00088     }

_prepareQty ( qty  )  [protected]

Prepare quantity

Parameters:
float|int $qty
Returns:
int|float

Definition at line 134 of file Item.php.

00135     {
00136         $qty = Mage::app()->getLocale()->getNumber($qty);
00137         $qty = ($qty > 0) ? $qty : 1;
00138         return $qty;
00139     }

addOption ( option  ) 

Add option to item

Parameters:
Mage_Sales_Model_Quote_Item_Option $option
Returns:
Mage_Sales_Model_Quote_Item

Definition at line 416 of file Item.php.

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     }

addQty ( qty  ) 

Adding quantity to quote item

Parameters:
float $qty
Returns:
Mage_Sales_Model_Quote_Item

We can't modify quontity of existing items which have parent This qty declared just once duering add process and is not editable

Definition at line 147 of file Item.php.

00148     {
00149         $oldQty = $this->getQty();
00150         $qty = $this->_prepareQty($qty);
00151 
00152         /**
00153          * We can't modify quontity of existing items which have parent
00154          * This qty declared just once duering add process and is not editable
00155          */
00156         if (!$this->getParentItem() || !$this->getId()) {
00157             $this->setQtyToAdd($qty);
00158             $this->setQty($oldQty+$qty);
00159         }
00160         return $this;
00161     }

compare ( item  ) 

Compare item

Parameters:
Mage_Sales_Model_Quote_Item $item
Returns:
bool

Definition at line 311 of file Item.php.

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                 // dispose of some options params, that can cramp comparing of arrays
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                         // looks like it does not break bundle selection qty
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     }

getOptionByCode ( code  ) 

Get item option by code

Parameters:
string $code
Returns:
Mage_Sales_Model_Quote_Item_Option || null

Definition at line 507 of file Item.php.

00508     {
00509         if (isset($this->_optionsByCode[$code]) && !$this->_optionsByCode[$code]->isDeleted()) {
00510             return $this->_optionsByCode[$code];
00511         }
00512         return null;
00513     }

getOptions (  ) 

Get all item options

Returns:
array

Definition at line 405 of file Item.php.

00406     {
00407         return $this->_options;
00408     }

getProduct (  ) 

Retrieve product model object associated with item

Returns:
Mage_Catalog_Model_Product

Reset product final price because it related to custom options

Definition at line 257 of file Item.php.

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          * Reset product final price because it related to custom options
00269          */
00270         $product->setFinalPrice(null);
00271         $product->setCustomOptions($this->_optionsByCode);
00272         return $product;
00273     }

getProductType (  ) 

Get item product type

Returns:
string

Definition at line 349 of file Item.php.

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     }

getQtyOptions (  ) 

Retrieve option product with Qty

Return array 'qty' => the qty 'product' => the product model

Returns:
array

Definition at line 197 of file Item.php.

00198     {
00199         $productIds = array();
00200         $return     = array();
00201         foreach ($this->getOptions() as $option) {
00202             /* @var $option Mage_Sales_Model_Quote_Item_Option */
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     }

getQuote (  ) 

Retrieve quote model object

Returns:
Mage_Sales_Model_Quote

Reimplemented from Mage_Sales_Model_Quote_Item_Abstract.

Definition at line 123 of file Item.php.

00124     {
00125         return $this->_quote;
00126     }

getRealProductType (  ) 

Return real product type of item

Returns:
unknown

Definition at line 365 of file Item.php.

00366     {
00367         return $this->_getData('product_type');
00368     }

removeOption ( code  ) 

Remove option from item options

Parameters:
string $code
Returns:
Mage_Sales_Model_Quote_Item

Definition at line 476 of file Item.php.

00477     {
00478         if ($option = $this->getOptionByCode($code)) {
00479             $option->isDeleted(true);
00480         }
00481         return $this;
00482     }

representProduct ( product  ) 

Check product representation in item

Parameters:
Mage_Catalog_Model_Product $product
Returns:
bool

Definition at line 281 of file Item.php.

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     }

setOptions ( options  ) 

Initialize quote item options

Parameters:
array $options
Returns:
Mage_Sales_Model_Quote_Item

Definition at line 392 of file Item.php.

00393     {
00394         foreach ($options as $option) {
00395             $this->addOption($option);
00396         }
00397         return $this;
00398     }

setProduct ( product  ) 

Setup product for quote item

Parameters:
Mage_Catalog_Model_Product $product
Returns:
Mage_Sales_Model_Quote_Item

Definition at line 224 of file Item.php.

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 //        if ($options = $product->getCustomOptions()) {
00245 //            foreach ($options as $option) {
00246 //                $this->addOption($option);
00247 //            }
00248 //        }
00249         return $this;
00250     }

setQty ( qty  ) 

Declare quote item quantity

Parameters:
float $qty
Returns:
Mage_Sales_Model_Quote_Item

Definition at line 169 of file Item.php.

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     }

setQuote ( Mage_Sales_Model_Quote quote  ) 

Declare quote model object

Parameters:
Mage_Sales_Model_Quote $quote
Returns:
Mage_Sales_Model_Quote_Item

Definition at line 111 of file Item.php.

00112     {
00113         $this->_quote = $quote;
00114         $this->setQuoteId($quote->getId());
00115         return $this;
00116     }

toArray ( array arrAttributes = array()  ) 

Convert Quote Item to array

Parameters:
array $arrAttributes
Returns:
array

Reimplemented from Varien_Object.

Definition at line 376 of file Item.php.

00377     {
00378         $data = parent::toArray($arrAttributes);
00379 
00380         if ($product = $this->getProduct()) {
00381             $data['product'] = $product->toArray();
00382         }
00383         return $data;
00384     }

updateQtyOption ( Varien_Object option,
value 
)

Can specify specific actions for ability to change given quote options values Exemple: cataloginventory decimal qty validation may change qty to int, so need to change quote item qty option value.

Parameters:
array $options
Varien_Object $option
mixed $value
Returns:
object Mage_Catalog_Model_Product_Type_Abstract

Definition at line 455 of file Item.php.

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     }


Member Data Documentation

$_eventObject = 'item' [protected]

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 51 of file Item.php.

$_eventPrefix = 'sales_quote_item' [protected]

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 42 of file Item.php.

$_notRepresentOptions = array('info_buyRequest') [protected]

Definition at line 79 of file Item.php.

$_options = array() [protected]

Definition at line 65 of file Item.php.

$_optionsByCode = array() [protected]

Definition at line 72 of file Item.php.

$_quote [protected]

Definition at line 58 of file Item.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:49 2009 for Magento by  doxygen 1.5.8