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_Catalog_Model_Product_Type_Grouped extends Mage_Catalog_Model_Product_Type_Abstract
00035 {
00036 const TYPE_CODE = 'grouped';
00037
00038
00039
00040
00041
00042
00043 protected $_keyAssociatedProducts = '_cache_instance_associated_products';
00044
00045
00046
00047
00048
00049
00050 protected $_keyAssociatedProductIds = '_cache_instance_associated_product_ids';
00051
00052
00053
00054
00055
00056
00057 protected $_keyStatusFilters = '_cache_instance_status_filters';
00058
00059
00060
00061
00062
00063
00064 protected $_isComposite = true;
00065
00066
00067
00068
00069
00070
00071 public function getRelationInfo()
00072 {
00073 $info = new Varien_Object();
00074 $info->setTable('catalog/product_link')
00075 ->setParentFieldName('product_id')
00076 ->setChildFieldName('linked_product_id')
00077 ->setWhere('link_type_id=' . Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED);
00078 return $info;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 public function getChildrenIds($parentId, $required = true)
00092 {
00093 return Mage::getResourceSingleton('catalog/product_link')
00094 ->getChildrenIds($parentId,
00095 Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED);
00096 }
00097
00098
00099
00100
00101
00102
00103
00104 public function getParentIdsByChild($childId)
00105 {
00106 return Mage::getResourceSingleton('catalog/product_link')
00107 ->getParentIdsByChild($childId,
00108 Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED);
00109 }
00110
00111
00112
00113
00114
00115
00116
00117 public function getAssociatedProducts($product = null)
00118 {
00119 if (!$this->getProduct($product)->hasData($this->_keyAssociatedProducts)) {
00120 $associatedProducts = array();
00121
00122 if (!Mage::app()->getStore()->isAdmin()) {
00123 $this->setSaleableStatus($product);
00124 }
00125
00126 $collection = $this->getAssociatedProductCollection($product)
00127 ->addAttributeToSelect('*')
00128 ->addFilterByRequiredOptions()
00129 ->setPositionOrder()
00130 ->addStoreFilter($this->getStoreFilter($product))
00131 ->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)));
00132
00133 foreach ($collection as $product) {
00134 $associatedProducts[] = $product;
00135 }
00136
00137 $this->getProduct($product)->setData($this->_keyAssociatedProducts, $associatedProducts);
00138 }
00139 return $this->getProduct($product)->getData($this->_keyAssociatedProducts);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 public function addStatusFilter($status, $product = null)
00150 {
00151 $statusFilters = $this->getProduct($product)->getData($this->_keyStatusFilters);
00152 if (!is_array($statusFilters)) {
00153 $statusFilters = array();
00154 }
00155
00156 $statusFilters[] = $status;
00157 $this->getProduct($product)->setData($this->_keyStatusFilters, $statusFilters);
00158
00159 return $this;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 public function setSaleableStatus($product = null)
00169 {
00170 $this->getProduct($product)->setData($this->_keyStatusFilters,
00171 Mage::getSingleton('catalog/product_status')->getSaleableStatusIds());
00172 return $this;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181 public function getStatusFilters($product = null)
00182 {
00183 if (!$this->getProduct($product)->hasData($this->_keyStatusFilters)) {
00184 return array(
00185 Mage_Catalog_Model_Product_Status::STATUS_ENABLED,
00186 Mage_Catalog_Model_Product_Status::STATUS_DISABLED
00187 );
00188 }
00189 return $this->getProduct($product)->getData($this->_keyStatusFilters);
00190 }
00191
00192
00193
00194
00195
00196
00197
00198 public function getAssociatedProductIds($product = null)
00199 {
00200 if (!$this->getProduct($product)->hasData($this->_keyAssociatedProductIds)) {
00201 $associatedProductIds = array();
00202 foreach ($this->getAssociatedProducts($product) as $product) {
00203 $associatedProductIds[] = $product->getId();
00204 }
00205 $this->getProduct($product)->setData($this->_keyAssociatedProductIds, $associatedProductIds);
00206 }
00207 return $this->getProduct($product)->getData($this->_keyAssociatedProductIds);
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 public function getAssociatedProductCollection($product = null)
00217 {
00218 $collection = $this->getProduct($product)->getLinkInstance()->useGroupedLinks()
00219 ->getProductCollection()
00220 ->setFlag('require_stock_items', true)
00221 ->setIsStrongMode();
00222 $collection->setProduct($this->getProduct($product));
00223 return $collection;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232 public function isSalable($product = null)
00233 {
00234 $salable = parent::isSalable($product);
00235 if (!is_null($salable)) {
00236 return $salable;
00237 }
00238
00239 $salable = false;
00240 foreach ($this->getAssociatedProducts($product) as $associatedProduct) {
00241 $salable = $salable || $associatedProduct->isSalable();
00242 }
00243 return $salable;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 public function save($product = null)
00253 {
00254 parent::save($product);
00255 $this->getProduct($product)->getLinkInstance()->saveGroupedLinks($this->getProduct($product));
00256 return $this;
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266 public function prepareForCart(Varien_Object $buyRequest, $product = null)
00267 {
00268 $productsInfo = $buyRequest->getSuperGroup();
00269 if (!empty($productsInfo) && is_array($productsInfo)) {
00270 $products = array();
00271 $associatedProducts = $this->getAssociatedProducts($product);
00272 if ($associatedProducts) {
00273 foreach ($associatedProducts as $subProduct) {
00274 if(isset($productsInfo[$subProduct->getId()])) {
00275 $qty = $productsInfo[$subProduct->getId()];
00276 if (!empty($qty)) {
00277
00278 $_result = $subProduct->getTypeInstance(true)
00279 ->prepareForCart($buyRequest, $subProduct);
00280 if (is_string($_result) && !is_array($_result)) {
00281 return $_result;
00282 }
00283
00284 if (!isset($_result[0])) {
00285 return Mage::helper('checkout')->__('Can not add item to shopping cart');
00286 }
00287
00288 $_result[0]->setCartQty($qty);
00289 $_result[0]->addCustomOption('product_type', self::TYPE_CODE, $this->getProduct($product));
00290 $_result[0]->addCustomOption('info_buyRequest',
00291 serialize(array(
00292 'super_product_config' => array(
00293 'product_type' => self::TYPE_CODE,
00294 'product_id' => $this->getProduct($product)->getId()
00295 )
00296 ))
00297 );
00298 $products[] = $_result[0];
00299 }
00300 }
00301 }
00302 }
00303 if (count($products)) {
00304 return $products;
00305 }
00306 }
00307 return Mage::helper('catalog')->__('Please specify the product(s) quantity');
00308 }
00309 }