1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Sales
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Quote addresses collection
30: *
31: * @category Mage
32: * @package Mage_Sales
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35:
36: class Mage_Sales_Model_Entity_Quote_Item_Collection extends Mage_Eav_Model_Entity_Collection_Abstract
37: {
38: /**
39: * Collection quote instance
40: *
41: * @var Mage_Sales_Model_Quote
42: */
43: protected $_quote;
44:
45: protected function _construct()
46: {
47: $this->_init('sales/quote_item');
48: }
49:
50: public function getStoreId()
51: {
52: return $this->_quote->getStoreId();
53: }
54:
55: public function setQuote($quote)
56: {
57: $this->_quote = $quote;
58: $this->addAttributeToFilter('parent_id', $quote->getId());
59: return $this;
60: }
61:
62: protected function _afterLoad()
63: {
64: Varien_Profiler::start('TEST1: '.__METHOD__);
65: $productCollection = $this->_getProductCollection();
66: Varien_Profiler::stop('TEST1: '.__METHOD__);
67: $recollectQuote = false;
68: foreach ($this as $item) {
69: Varien_Profiler::start('TEST2: '.__METHOD__);
70: if ($productCollection) {
71: $product = $productCollection->getItemById($item->getProductId());
72: } else {
73: $product = false;
74: }
75: if ($this->_quote) {
76: $item->setQuote($this->_quote);
77: }
78: if (!$product) {
79: $item->isDeleted(true);
80: $recollectQuote = true;
81: continue;
82: }
83:
84: if ($item->getSuperProductId()) {
85: $superProduct = $productCollection->getItemById($item->getSuperProductId());
86: if (!$superProduct) {
87: $item->isDeleted(true);
88: $recollectQuote = true;
89: continue;
90: }
91: }
92: else {
93: $superProduct = null;
94: }
95:
96: $itemProduct = clone $product;
97: if ($superProduct) {
98: $itemProduct->setSuperProduct($superProduct);
99: $item->setSuperProduct($superProduct);
100: }
101:
102: $item->importCatalogProduct($itemProduct);
103: $item->checkData();
104: Varien_Profiler::stop('TEST2: '.__METHOD__);
105: }
106: if ($recollectQuote && $this->_quote) {
107: $this->_quote->collectTotals();
108: }
109: return $this;
110: }
111:
112: protected function _getProductCollection()
113: {
114: $productIds = array();
115: foreach ($this as $item) {
116: $productIds[$item->getProductId()] = $item->getProductId();
117: if ($item->getSuperProductId()) {
118: $productIds[$item->getSuperProductId()] = $item->getSuperProductId();
119: }
120: if ($item->getParentProductId()) {
121: $productIds[$item->getSuperProductId()] = $item->getParentProductId();
122: }
123: }
124:
125: if (empty($productIds)) {
126: return false;
127: }
128:
129: $collection = Mage::getModel('catalog/product')->getCollection()
130: ->setStoreId($this->getStoreId())
131: ->addIdFilter($productIds)
132: ->addAttributeToSelect('*')
133: ->addStoreFilter()
134: ->addUrlRewrite();
135:
136: if (Mage::app()->useCache('checkout_quote')) {
137: $collection->initCache(
138: $this->_getCacheInstance(),
139: $this->_cacheConf['prefix'].'_PRODUCTS',
140: $this->_getCacheTags()
141: );
142: }
143:
144: return $collection;
145: }
146: }
147: