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_Mysql4_Quote_Item_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_quote;
00043
00044
00045
00046
00047
00048
00049 protected $_productIds = array();
00050
00051
00052
00053
00054
00055 protected function _construct()
00056 {
00057 $this->_init('sales/quote_item');
00058 }
00059
00060
00061
00062
00063
00064
00065 public function getStoreId()
00066 {
00067 return $this->_quote->getStoreId();
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 public function setQuote($quote)
00077 {
00078 $this->_quote = $quote;
00079 $this->addFieldToFilter('quote_id', $quote->getId());
00080 return $this;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 public function resetJoinQuotes($quotesTableName, $productId = null)
00093 {
00094 $this->getSelect()
00095 ->reset()
00096 ->from(array('qi' => $this->getResource()->getMainTable()), array('item_id', 'qty', 'quote_id'));
00097 $this->getSelect()
00098 ->joinInner(array('q' => $quotesTableName), 'qi.quote_id=q.entity_id', array('store_id', 'items_qty', 'items_count'));
00099 if ($productId) {
00100 $this->getSelect()->where('qi.product_id=?', $productId);
00101 }
00102 return $this;
00103 }
00104
00105
00106
00107
00108
00109
00110 protected function _afterLoad()
00111 {
00112 parent::_afterLoad();
00113
00114
00115
00116 foreach ($this as $item) {
00117 if ($item->getParentItemId()) {
00118 $item->setParentItem($this->getItemById($item->getParentItemId()));
00119 }
00120 if ($this->_quote) {
00121 $item->setQuote($this->_quote);
00122 }
00123 }
00124
00125
00126
00127
00128 $this->_assignOptions()
00129 ->_assignProducts();
00130 return $this;
00131 }
00132
00133
00134
00135
00136
00137
00138 protected function _assignOptions()
00139 {
00140 $itemIds = array_keys($this->_items);
00141 $optionCollection = Mage::getModel('sales/quote_item_option')->getCollection()
00142 ->addItemFilter($itemIds);
00143 foreach ($this as $item) {
00144 $item->setOptions($optionCollection->getOptionsByItem($item));
00145 }
00146 $productIds = $optionCollection->getProductIds();
00147 $this->_productIds = array_merge($this->_productIds, $productIds);
00148 return $this;
00149 }
00150
00151
00152
00153
00154
00155
00156 protected function _assignProducts()
00157 {
00158 Varien_Profiler::start('QUOTE:'.__METHOD__);
00159 $productIds = array();
00160 foreach ($this as $item) {
00161 $productIds[] = $item->getProductId();
00162 }
00163 $this->_productIds = array_merge($this->_productIds, $productIds);
00164
00165 $productCollection = Mage::getModel('catalog/product')->getCollection()
00166 ->setStoreId($this->getStoreId())
00167 ->addIdFilter($this->_productIds)
00168 ->addAttributeToSelect(Mage::getSingleton('sales/quote_config')->getProductAttributes())
00169 ->addOptionsToResult()
00170 ->addStoreFilter()
00171 ->addUrlRewrite();
00172
00173 Mage::dispatchEvent('sales_quote_item_collection_products_after_load', array('product_collection'=>$productCollection));
00174
00175 $recollectQuote = false;
00176 foreach ($this as $item) {
00177
00178 if ($product = $productCollection->getItemById($item->getProductId())) {
00179 $product->setCustomOptions(array());
00180
00181 foreach ($item->getOptions() as $option) {
00182
00183
00184
00185 $product->getTypeInstance(true)->assignProductToOption(
00186 $productCollection->getItemById($option->getProductId()),
00187 $option,
00188 $product
00189 );
00190 }
00191 $item->setProduct($product);
00192 }
00193 else {
00194 $item->isDeleted(true);
00195 $recollectQuote = true;
00196 }
00197 $item->checkData();
00198 }
00199
00200 if ($recollectQuote && $this->_quote) {
00201 $this->_quote->collectTotals();
00202 }
00203 Varien_Profiler::stop('QUOTE:'.__METHOD__);
00204 return $this;
00205 }
00206 }