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 class Mage_Bundle_Model_Mysql4_Selection extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036 protected function _construct()
00037 {
00038 $this->_init('bundle/selection', 'selection_id');
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 public function getPriceFromIndex($productId, $qty, $storeId, $groupId) {
00081 $select = clone $this->_getReadAdapter()->select();
00082 $select->reset();
00083
00084 $attrPriceId = Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'price');
00085 $attrTierPriceId = Mage::getSingleton('eav/entity_attribute')->getIdByCode('catalog_product', 'tier_price');
00086
00087 $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
00088
00089 $select->from(array("price_index" => $this->getTable('catalogindex/price')), array('price' => 'SUM(value)'))
00090 ->where('entity_id in (?)', $productId)
00091 ->where('website_id = ?', $websiteId)
00092 ->where('customer_group_id = ?', $groupId)
00093 ->where('attribute_id in (?)', array($attrPriceId, $attrTierPriceId))
00094 ->where('qty <= ?', $qty)
00095 ->group('entity_id');
00096
00097 $price = $this->_getReadAdapter()->fetchCol($select);
00098 if (!empty($price)) {
00099 return array_shift($price);
00100 } else {
00101 return 0;
00102 }
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 public function getChildrenIds($parentId, $required = true)
00116 {
00117 $childrenIds = array();
00118 $notRequired = array();
00119 $select = $this->_getReadAdapter()->select()
00120 ->from(
00121 array('tbl_selection' => $this->getMainTable()),
00122 array('product_id', 'parent_product_id', 'option_id'))
00123 ->join(
00124 array('e' => $this->getTable('catalog/product')),
00125 'e.entity_id=tbl_selection.product_id AND e.required_options=0',
00126 array()
00127 )
00128 ->join(
00129 array('tbl_option' => $this->getTable('bundle/option')),
00130 '`tbl_option`.`option_id` = `tbl_selection`.`option_id`',
00131 array('required')
00132 )
00133 ->where('`tbl_selection`.`parent_product_id`=?', $parentId);
00134 foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
00135 if ($row['required']) {
00136 $childrenIds[$row['option_id']][$row['product_id']] = $row['product_id'];
00137 }
00138 else {
00139 $notRequired[$row['option_id']][$row['product_id']] = $row['product_id'];
00140 }
00141 }
00142
00143 if (!$required) {
00144 $childrenIds = array_merge($childrenIds, $notRequired);
00145 }
00146 else {
00147 if (!$childrenIds) {
00148 foreach ($notRequired as $groupedChildrenIds) {
00149 foreach ($groupedChildrenIds as $childId) {
00150 $childrenIds[0][$childId] = $childId;
00151 }
00152 }
00153 }
00154 }
00155
00156 return $childrenIds;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 public function getParentIdsByChild($childId)
00166 {
00167 $parentIds = array();
00168
00169 $select = $this->_getReadAdapter()->select()
00170 ->from(
00171 array('tbl_selection' => $this->getMainTable()),
00172 array('product_id', 'parent_product_id', 'option_id'))
00173 ->join(
00174 array('tbl_option' => $this->getTable('bundle/option')),
00175 '`tbl_option`.`option_id` = `tbl_selection`.`option_id`',
00176 array('required')
00177 )
00178 ->where('`tbl_selection`.`product_id` IN(?)', $childId);
00179 foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
00180 $parentIds[] = $row['parent_product_id'];
00181 }
00182
00183 return $parentIds;
00184 }
00185 }