*/ class Mage_Sales_Model_Quote_Item extends Mage_Sales_Model_Quote_Item_Abstract { /** * Prefix of model events names * * @var string */ protected $_eventPrefix = 'sales_quote_item'; /** * Parameter name in event * * In observe method you can use $observer->getEvent()->getObject() in this case * * @var string */ protected $_eventObject = 'item'; /** * Quote model object * * @var Mage_Sales_Model_Quote */ protected $_quote; /** * Item options array * * @var array */ protected $_options = array(); /** * Item options by code cache * * @var array */ protected $_optionsByCode = array(); /** * Not Represent options * * @var array */ protected $_notRepresentOptions = array('info_buyRequest'); /** * Flag stating that options were successfully saved * */ protected $_flagOptionsSaved = null; /** * Array of errors associated with this quote item * * @var Mage_Sales_Model_Status_List */ protected $_errorInfos = null; /** * Initialize resource model * */ protected function _construct() { $this->_init('sales/quote_item'); $this->_errorInfos = Mage::getModel('sales/status_list'); } /** * Init mapping array of short fields to * its full names * * @return Varien_Object */ protected function _initOldFieldsMap() { $this->_oldFieldsMap = Mage::helper('sales')->getOldFieldMap('quote_item'); return $this; } /** * Quote Item Before Save prepare data process * * @return Mage_Sales_Model_Quote_Item */ protected function _beforeSave() { parent::_beforeSave(); $this->setIsVirtual($this->getProduct()->getIsVirtual()); if ($this->getQuote()) { $this->setQuoteId($this->getQuote()->getId()); } return $this; } /** * Declare quote model object * * @param Mage_Sales_Model_Quote $quote * @return Mage_Sales_Model_Quote_Item */ public function setQuote(Mage_Sales_Model_Quote $quote) { $this->_quote = $quote; $this->setQuoteId($quote->getId()); return $this; } /** * Retrieve quote model object * * @return Mage_Sales_Model_Quote */ public function getQuote() { return $this->_quote; } /** * Prepare quantity * * @param float|int $qty * @return int|float */ protected function _prepareQty($qty) { $qty = Mage::app()->getLocale()->getNumber($qty); $qty = ($qty > 0) ? $qty : 1; return $qty; } /** * Get Magento App instance * * @return Mage_Core_Model_App */ protected function _getApp() { return Mage::app(); } /** * Adding quantity to quote item * * @param float $qty * @return Mage_Sales_Model_Quote_Item */ public function addQty($qty) { $oldQty = $this->getQty(); $qty = $this->_prepareQty($qty); /** * We can't modify quontity of existing items which have parent * This qty declared just once duering add process and is not editable */ if (!$this->getParentItem() || !$this->getId()) { $this->setQtyToAdd($qty); $this->setQty($oldQty + $qty); } return $this; } /** * Declare quote item quantity * * @param float $qty * @return Mage_Sales_Model_Quote_Item */ public function setQty($qty) { $qty = $this->_prepareQty($qty); $oldQty = $this->_getData('qty'); $this->setData('qty', $qty); Mage::dispatchEvent('sales_quote_item_qty_set_after', array('item' => $this)); if ($this->getQuote() && $this->getQuote()->getIgnoreOldQty()) { return $this; } if ($this->getUseOldQty()) { $this->setData('qty', $oldQty); } return $this; } /** * Retrieve option product with Qty * * Return array * 'qty' => the qty * 'product' => the product model * * @return array */ public function getQtyOptions() { $qtyOptions = $this->getData('qty_options'); if (is_null($qtyOptions)) { $productIds = array(); $qtyOptions = array(); foreach ($this->getOptions() as $option) { /** @var $option Mage_Sales_Model_Quote_Item_Option */ if (is_object($option->getProduct()) && $option->getProduct()->getId() != $this->getProduct()->getId() ) { $productIds[$option->getProduct()->getId()] = $option->getProduct()->getId(); } } foreach ($productIds as $productId) { $option = $this->getOptionByCode('product_qty_' . $productId); if ($option) { $qtyOptions[$productId] = $option; } } $this->setData('qty_options', $qtyOptions); } return $qtyOptions; } /** * Set option product with Qty * * @param $qtyOptions * @return Mage_Sales_Model_Quote_Item */ public function setQtyOptions($qtyOptions) { return $this->setData('qty_options', $qtyOptions); } /** * Setup product for quote item * * @param Mage_Catalog_Model_Product $product * @return Mage_Sales_Model_Quote_Item */ public function setProduct($product) { if ($this->getQuote()) { $product->setStoreId($this->getQuote()->getStoreId()); $product->setCustomerGroupId($this->getQuote()->getCustomerGroupId()); } $this->setData('product', $product) ->setProductId($product->getId()) ->setProductType($product->getTypeId()) ->setSku($this->getProduct()->getSku()) ->setName($product->getName()) ->setWeight($this->getProduct()->getWeight()) ->setTaxClassId($product->getTaxClassId()) ->setBaseCost($product->getCost()) ->setIsRecurring($product->getIsRecurring()); if ($product->getStockItem()) { $this->setIsQtyDecimal($product->getStockItem()->getIsQtyDecimal()); } Mage::dispatchEvent('sales_quote_item_set_product', array( 'product' => $product, 'quote_item' => $this )); // if ($options = $product->getCustomOptions()) { // foreach ($options as $option) { // $this->addOption($option); // } // } return $this; } /** * Check product representation in item * * @param Mage_Catalog_Model_Product $product * @return bool */ public function representProduct($product) { $itemProduct = $this->getProduct(); if (!$product || $itemProduct->getId() != $product->getId()) { return false; } /** * Check maybe product is planned to be a child of some quote item - in this case we limit search * only within same parent item */ $stickWithinParent = $product->getStickWithinParent(); if ($stickWithinParent) { if ($this->getParentItem() !== $stickWithinParent) { return false; } } // Check options $itemOptions = $this->getOptionsByCode(); $productOptions = $product->getCustomOptions(); if (!$this->compareOptions($itemOptions, $productOptions)) { return false; } if (!$this->compareOptions($productOptions, $itemOptions)) { return false; } return true; } /** * Check if two options array are identical * First options array is prerogative * Second options array checked against first one * * @param array $options1 * @param array $options2 * @return bool */ public function compareOptions($options1, $options2) { foreach ($options1 as $option) { $code = $option->getCode(); if (in_array($code, $this->_notRepresentOptions)) { continue; } if (!isset($options2[$code]) || ($options2[$code]->getValue() === null) || $options2[$code]->getValue() != $option->getValue() ) { return false; } } return true; } /** * Compare item * * @param Mage_Sales_Model_Quote_Item $item * @return bool */ public function compare($item) { if ($this->getProductId() != $item->getProductId()) { return false; } foreach ($this->getOptions() as $option) { if (in_array($option->getCode(), $this->_notRepresentOptions) && !$item->getProduct()->hasCustomOptions() ) { continue; } if ($itemOption = $item->getOptionByCode($option->getCode())) { $itemOptionValue = $itemOption->getValue(); $optionValue = $option->getValue(); // dispose of some options params, that can cramp comparing of arrays if (is_string($itemOptionValue) && is_string($optionValue)) { $_itemOptionValue = @unserialize($itemOptionValue); $_optionValue = @unserialize($optionValue); if (is_array($_itemOptionValue) && is_array($_optionValue)) { $itemOptionValue = $_itemOptionValue; $optionValue = $_optionValue; // looks like it does not break bundle selection qty unset($itemOptionValue['qty'], $itemOptionValue['uenc']); unset($optionValue['qty'], $optionValue['uenc']); } } if ($itemOptionValue != $optionValue) { return false; } } else { return false; } } return true; } /** * Get item product type * * @return string */ public function getProductType() { if ($option = $this->getOptionByCode('product_type')) { return $option->getValue(); } if ($product = $this->getProduct()) { return $product->getTypeId(); } return $this->_getData('product_type'); } /** * Return real product type of item * * @return unknown */ public function getRealProductType() { return $this->_getData('product_type'); } /** * Convert Quote Item to array * * @param array $arrAttributes * @return array */ public function toArray(array $arrAttributes = array()) { $data = parent::toArray($arrAttributes); if ($product = $this->getProduct()) { $data['product'] = $product->toArray(); } return $data; } /** * Initialize quote item options * * @param array $options * @return Mage_Sales_Model_Quote_Item */ public function setOptions($options) { foreach ($options as $option) { $this->addOption($option); } return $this; } /** * Get all item options * * @return array */ public function getOptions() { return $this->_options; } /** * Get all item options as array with codes in array key * * @return array */ public function getOptionsByCode() { return $this->_optionsByCode; } /** * Add option to item * * @param Mage_Sales_Model_Quote_Item_Option|Varien_Object $option * @return Mage_Sales_Model_Quote_Item */ public function addOption($option) { if (is_array($option)) { $option = Mage::getModel('sales/quote_item_option')->setData($option) ->setItem($this); } elseif (($option instanceof Varien_Object) && !($option instanceof Mage_Sales_Model_Quote_Item_Option)) { $option = Mage::getModel('sales/quote_item_option')->setData($option->getData()) ->setProduct($option->getProduct()) ->setItem($this); } elseif ($option instanceof Mage_Sales_Model_Quote_Item_Option) { $option->setItem($this); } else { Mage::throwException(Mage::helper('sales')->__('Invalid item option format.')); } if ($exOption = $this->getOptionByCode($option->getCode())) { $exOption->addData($option->getData()); } else { $this->_addOptionCode($option); $this->_options[] = $option; } return $this; } /** * 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. * * @param Varien_Object $option * @param int|float|null $value * @return Mage_Sales_Model_Quote_Item */ public function updateQtyOption(Varien_Object $option, $value) { $optionProduct = $option->getProduct(); $options = $this->getQtyOptions(); if (isset($options[$optionProduct->getId()])) { $options[$optionProduct->getId()]->setValue($value); } $this->getProduct()->getTypeInstance(true) ->updateQtyOption($this->getOptions(), $option, $value, $this->getProduct()); return $this; } /** *Remove option from item options * * @param string $code * @return Mage_Sales_Model_Quote_Item */ public function removeOption($code) { $option = $this->getOptionByCode($code); if ($option) { $option->isDeleted(true); } return $this; } /** * Register option code * * @param Mage_Sales_Model_Quote_Item_Option $option * @return Mage_Sales_Model_Quote_Item */ protected function _addOptionCode($option) { if (!isset($this->_optionsByCode[$option->getCode()])) { $this->_optionsByCode[$option->getCode()] = $option; } else { Mage::throwException(Mage::helper('sales')->__('An item option with code %s already exists.', $option->getCode())); } return $this; } /** * Get item option by code * * @param string $code * @return Mage_Sales_Model_Quote_Item_Option || null */ public function getOptionByCode($code) { if (isset($this->_optionsByCode[$code]) && !$this->_optionsByCode[$code]->isDeleted()) { return $this->_optionsByCode[$code]; } return null; } /** * Checks that item model has data changes. * Call save item options if model isn't need to save in DB * * @return boolean */ protected function _hasModelChanged() { if (!$this->hasDataChanges()) { return false; } return $this->_getResource()->hasDataChanged($this); } /** * Save item options * * @return Mage_Sales_Model_Quote_Item */ protected function _saveItemOptions() { foreach ($this->_options as $index => $option) { if ($option->isDeleted()) { $option->delete(); unset($this->_options[$index]); unset($this->_optionsByCode[$option->getCode()]); } else { $option->save(); } } $this->_flagOptionsSaved = true; // Report to watchers that options were saved return $this; } /** * Save model plus its options * Ensures saving options in case when resource model was not changed */ public function save() { $hasDataChanges = $this->hasDataChanges(); $this->_flagOptionsSaved = false; parent::save(); if ($hasDataChanges && !$this->_flagOptionsSaved) { $this->_saveItemOptions(); } } /** * Save item options after item saved * * @return Mage_Sales_Model_Quote_Item */ protected function _afterSave() { $this->_saveItemOptions(); return parent::_afterSave(); } /** * Clone quote item * * @return Mage_Sales_Model_Quote_Item */ public function __clone() { parent::__clone(); $options = $this->getOptions(); $this->_quote = null; $this->_options = array(); $this->_optionsByCode = array(); foreach ($options as $option) { $this->addOption(clone $option); } return $this; } /** * Returns formatted buy request - object, holding request received from * product view page with keys and options for configured product * * @return Varien_Object */ public function getBuyRequest() { $option = $this->getOptionByCode('info_buyRequest'); $buyRequest = new Varien_Object($option ? unserialize($option->getValue()) : null); // Overwrite standard buy request qty, because item qty could have changed since adding to quote $buyRequest->setOriginalQty($buyRequest->getQty()) ->setQty($this->getQty() * 1); return $buyRequest; } /** * Sets flag, whether this quote item has some error associated with it. * * @param bool $flag * @return Mage_Sales_Model_Quote_Item */ protected function _setHasError($flag) { return $this->setData('has_error', $flag); } /** * Sets flag, whether this quote item has some error associated with it. * When TRUE - also adds 'unknown' error information to list of quote item errors. * When FALSE - clears whole list of quote item errors. * It's recommended to use addErrorInfo() instead - to be able to remove error statuses later. * * @param bool $flag * @return Mage_Sales_Model_Quote_Item * @see addErrorInfo() */ public function setHasError($flag) { if ($flag) { $this->addErrorInfo(); } else { $this->_clearErrorInfo(); } return $this; } /** * Clears list of errors, associated with this quote item. * Also automatically removes error-flag from oneself. * * @return Mage_Sales_Model_Quote_Item */ protected function _clearErrorInfo() { $this->_errorInfos->clear(); $this->_setHasError(false); return $this; } /** * Adds error information to the quote item. * Automatically sets error flag. * * @param string|null $origin Usually a name of module, that embeds error * @param int|null $code Error code, unique for origin, that sets it * @param string|null $message Error message * @param Varien_Object|null $additionalData Any additional data, that caller would like to store * @return Mage_Sales_Model_Quote_Item */ public function addErrorInfo($origin = null, $code = null, $message = null, $additionalData = null) { $this->_errorInfos->addItem($origin, $code, $message, $additionalData); if ($message !== null) { $this->setMessage($message); } $this->_setHasError(true); return $this; } /** * Retrieves all error infos, associated with this item * * @return array */ public function getErrorInfos() { return $this->_errorInfos->getItems(); } /** * Removes error infos, that have parameters equal to passed in $params. * $params can have following keys (if not set - then any item is good for this key): * 'origin', 'code', 'message' * * @param array $params * @return Mage_Sales_Model_Quote_Item */ public function removeErrorInfosByParams($params) { $removedItems = $this->_errorInfos->removeItemsByParams($params); foreach ($removedItems as $item) { if ($item['message'] !== null) { $this->removeMessageByText($item['message']); } } if (!$this->_errorInfos->getItems()) { $this->_setHasError(false); } return $this; } }