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 class Mage_Checkout_Model_Mysql4_Cart extends Mage_Core_Model_Mysql4_Abstract
00029 {
00030 protected function _construct()
00031 {
00032 $this->_init('sales/quote', 'entity_id');
00033 }
00034
00035 public function getItemsQty($cart)
00036 {
00037 return $this->fetchItemsSummaryQty($cart->getQuote()->getId());
00038 }
00039
00040 public function fetchItemsSummaryQty($quoteId)
00041 {
00042 $entityType = Mage::getSingleton('eav/config')->getEntityType('quote_item');
00043 $attribute = Mage::getSingleton('eav/config')->getAttribute($entityType->getEntityTypeId(), 'qty');
00044
00045 $qtyAttributeTable = $this->getMainTable().'_'.$attribute->getBackendType();
00046 $read = $this->_getReadAdapter();
00047 $select = $read->select()
00048 ->from(array('qty'=>$qtyAttributeTable), 'sum(qty.value)')
00049 ->join(array('e'=>$this->getMainTable()), 'e.entity_id=qty.entity_id', array())
00050 ->where('e.parent_id=?', $quoteId)
00051 ->where('qty.entity_type_id=?', $entityType->getEntityTypeId())
00052 ->where('qty.attribute_id=?', $attribute->getAttributeId());
00053 $qty = $read->fetchOne($select);
00054 return $qty;
00055 }
00056
00057 public function fetchItemsSummary($quoteId)
00058 {
00059 $read = $this->_getReadAdapter();
00060 $select = $read->select()
00061 ->from(array('q'=>$this->getTable('sales/quote')), array('items_qty', 'items_count'))
00062 ->where('q.entity_id=?', $quoteId);
00063
00064 $result = $read->fetchRow($select);
00065 return $result ? $result : array('items_qty'=>0, 'items_count'=>0);
00066 }
00067
00068 public function fetchItems($quoteId)
00069 {
00070 $read = $this->_getReadAdapter();
00071 $select = $read->select()
00072 ->from(array('qi'=>$this->getTable('sales/quote_item')), array('id'=>'item_id', 'product_id', 'super_product_id', 'qty', 'created_at'))
00073 ->where('qi.quote_id=?', $quoteId);
00074
00075 return $read->fetchAll($select);
00076 }
00077
00078
00079
00080
00081
00082
00083
00084 public function addExcludeProductFilter($collection, $quoteId) {
00085 $collection->getSelect()->where(new Zend_Db_Expr(sprintf(
00086 'e.entity_id NOT IN (SELECT product_id FROM %s WHERE quote_id=%d)',
00087 $this->getTable('sales/quote_item'), $quoteId
00088 )));
00089 }
00090 }