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_Catalog_Model_Resource_Eav_Mysql4_Product_Option_Collection
00036 extends Mage_Core_Model_Mysql4_Collection_Abstract
00037 {
00038 protected function _construct()
00039 {
00040 $this->_init('catalog/product_option');
00041 }
00042
00043 public function getOptions($store_id)
00044 {
00045 $this->getSelect()
00046 ->joinLeft(array('default_option_price'=>$this->getTable('catalog/product_option_price')),
00047 '`default_option_price`.option_id=`main_table`.option_id AND '.$this->getConnection()->quoteInto('`default_option_price`.store_id=?',0),
00048 array('default_price'=>'price','default_price_type'=>'price_type'))
00049 ->joinLeft(array('store_option_price'=>$this->getTable('catalog/product_option_price')),
00050 '`store_option_price`.option_id=`main_table`.option_id AND '.$this->getConnection()->quoteInto('`store_option_price`.store_id=?', $store_id),
00051 array('store_price'=>'price','store_price_type'=>'price_type',
00052 'price'=>new Zend_Db_Expr('IFNULL(`store_option_price`.price,`default_option_price`.price)'),
00053 'price_type'=>new Zend_Db_Expr('IFNULL(`store_option_price`.price_type,`default_option_price`.price_type)')))
00054 ->join(array('default_option_title'=>$this->getTable('catalog/product_option_title')),
00055 '`default_option_title`.option_id=`main_table`.option_id',
00056 array('default_title'=>'title'))
00057 ->joinLeft(array('store_option_title'=>$this->getTable('catalog/product_option_title')),
00058 '`store_option_title`.option_id=`main_table`.option_id AND '.$this->getConnection()->quoteInto('`store_option_title`.store_id=?', $store_id),
00059 array('store_title'=>'title',
00060 'title'=>new Zend_Db_Expr('IFNULL(`store_option_title`.title,`default_option_title`.title)')))
00061 ->where('`default_option_title`.store_id=?', 0);
00062
00063 return $this;
00064 }
00065
00066 public function addTitleToResult($store_id)
00067 {
00068 $this->getSelect()
00069 ->join(array('default_option_title'=>$this->getTable('catalog/product_option_title')),
00070 '`default_option_title`.option_id=`main_table`.option_id',
00071 array('default_title'=>'title'))
00072 ->joinLeft(array('store_option_title'=>$this->getTable('catalog/product_option_title')),
00073 '`store_option_title`.option_id=`main_table`.option_id AND '.$this->getConnection()->quoteInto('`store_option_title`.store_id=?', $store_id),
00074 array('store_title'=>'title',
00075 'title'=>new Zend_Db_Expr('IFNULL(`store_option_title`.title,`default_option_title`.title)')))
00076 ->where('`default_option_title`.store_id=?', 0);
00077
00078 return $this;
00079 }
00080
00081 public function addPriceToResult($store_id)
00082 {
00083 $this->getSelect()
00084 ->joinLeft(array('default_option_price'=>$this->getTable('catalog/product_option_price')),
00085 '`default_option_price`.option_id=`main_table`.option_id AND '.$this->getConnection()->quoteInto('`default_option_price`.store_id=?',0),
00086 array('default_price'=>'price','default_price_type'=>'price_type'))
00087 ->joinLeft(array('store_option_price'=>$this->getTable('catalog/product_option_price')),
00088 '`store_option_price`.option_id=`main_table`.option_id AND '.$this->getConnection()->quoteInto('`store_option_price`.store_id=?', $store_id),
00089 array('store_price'=>'price','store_price_type'=>'price_type',
00090 'price'=>new Zend_Db_Expr('IFNULL(`store_option_price`.price,`default_option_price`.price)'),
00091 'price_type'=>new Zend_Db_Expr('IFNULL(`store_option_price`.price_type,`default_option_price`.price_type)')));
00092 return $this;
00093 }
00094
00095 public function addValuesToResult($storeId = null)
00096 {
00097 if (null === $storeId) {
00098 $storeId = Mage::app()->getStore()->getId();
00099 }
00100 $optionIds = array();
00101 foreach ($this as $option) {
00102 $optionIds[] = $option->getId();
00103 }
00104 if (!empty($optionIds)) {
00105 $values = Mage::getModel('catalog/product_option_value')
00106 ->getCollection()
00107 ->addTitleToResult($storeId)
00108 ->addPriceToResult($storeId)
00109 ->addOptionToFilter($optionIds)
00110 ->setOrder('sort_order', 'asc')
00111 ->setOrder('title', 'asc');
00112
00113 foreach ($values as $value) {
00114 if($this->getItemById($value->getOptionId())) {
00115 $this->getItemById($value->getOptionId())->addValue($value);
00116 $value->setOption($this->getItemById($value->getOptionId()));
00117 }
00118 }
00119 }
00120
00121 return $this;
00122 }
00123
00124 public function addProductToFilter($product)
00125 {
00126 if (empty($product)) {
00127 $this->addFieldToFilter('product_id', '');
00128 } elseif (is_array($product)) {
00129 $this->addFieldToFilter('product_id', array('in' => $product));
00130 } elseif ($product instanceof Mage_Catalog_Model_Product) {
00131 $this->addFieldToFilter('product_id', $product->getId());
00132 } else {
00133 $this->addFieldToFilter('product_id', $product);
00134 }
00135
00136 return $this;
00137 }
00138
00139
00140
00141
00142
00143
00144 public function reset() {
00145 return $this->_reset();
00146 }
00147
00148 }