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_Observer
00035 {
00036
00037
00038
00039
00040
00041
00042 public function prepareProductSave($observer)
00043 {
00044 $request = $observer->getEvent()->getRequest();
00045 $product = $observer->getEvent()->getProduct();
00046
00047 if (($items = $request->getPost('bundle_options')) && !$product->getCompositeReadonly()) {
00048 $product->setBundleOptionsData($items);
00049 }
00050
00051 if (($selections = $request->getPost('bundle_selections')) && !$product->getCompositeReadonly()) {
00052 $product->setBundleSelectionsData($selections);
00053 }
00054
00055 if ($product->getPriceType() == '0' && !$product->getOptionsReadonly()) {
00056 $product->setCanSaveCustomOptions(true);
00057 if ($customOptions = $product->getProductOptions()) {
00058 foreach (array_keys($customOptions) as $key) {
00059 $customOptions[$key]['is_delete'] = 1;
00060 }
00061 $product->setProductOptions($customOptions);
00062 }
00063 }
00064
00065 $product->setCanSaveBundleSelections((bool)$request->getPost('affect_bundle_product_selections') && !$product->getCompositeReadonly());
00066
00067 return $this;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 public function appendUpsellProducts($observer)
00077 {
00078 $product = $observer->getEvent()->getProduct();
00079
00080 if ($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {
00081 return $this;
00082 }
00083
00084 $collection = $observer->getEvent()->getCollection();
00085 $limit = $observer->getEvent()->getLimit();
00086
00087 $bundles = Mage::getModel('catalog/product')->getResourceCollection()
00088 ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
00089 ->addStoreFilter()
00090 ->addMinimalPrice()
00091
00092 ->joinTable('bundle/option', 'parent_id=entity_id', array('option_id' => 'option_id'))
00093 ->joinTable('bundle/selection', 'option_id=option_id', array('product_id' => 'product_id'), '{{table}}.product_id='.$product->getId());
00094
00095 $ids = $collection->getAllIds();
00096 if (count($ids)) {
00097 $bundles->addIdFilter($ids, true);
00098 }
00099
00100 Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($bundles);
00101 Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($bundles);
00102
00103 $bundles->getSelect()->group('entity_id');
00104
00105 if (isset($limit['bundle'])) {
00106 $bundles->setPageSize($limit['bundle']);
00107 }
00108 $bundles->load();
00109
00110 foreach ($bundles->getItems() as $item) {
00111 $collection->addItem($item);
00112 }
00113
00114 return $this;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 public function appendBundleSelectionData($observer)
00124 {
00125 $orderItem = $observer->getEvent()->getOrderItem();
00126 $quoteItem = $observer->getEvent()->getItem();
00127
00128 if ($attributes = $quoteItem->getProduct()->getCustomOption('bundle_selection_attributes')) {
00129 $productOptions = $orderItem->getProductOptions();
00130 $productOptions['bundle_selection_attributes'] = $attributes->getValue();
00131 $orderItem->setProductOptions($productOptions);
00132 }
00133
00134 return $this;
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144 public function loadProductOptions($observer)
00145 {
00146 $collection = $observer->getEvent()->getCollection();
00147
00148 $hasBundle = false;
00149 foreach ($collection->getItems() as $item){
00150 if ($item->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
00151 $hasBundle = true;
00152 }
00153 }
00154
00155 if ($hasBundle) {
00156 Mage::getSingleton('bundle/price_index')
00157 ->addPriceIndexToCollection($collection);
00158 }
00159
00160 return $this;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 public function duplicateProduct($observer)
00170 {
00171 $product = $observer->getEvent()->getCurrentProduct();
00172
00173 if ($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
00174
00175 return $this;
00176 }
00177
00178 $newProduct = $observer->getEvent()->getNewProduct();
00179
00180 $product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
00181 $optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
00182 $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
00183 $product->getTypeInstance(true)->getOptionsIds($product),
00184 $product
00185 );
00186 $optionCollection->appendSelections($selectionCollection);
00187
00188 $optionRawData = array();
00189 $selectionRawData = array();
00190
00191 $i = 0;
00192 foreach ($optionCollection as $option) {
00193 $optionRawData[$i] = array(
00194 'required' => $option->getData('required'),
00195 'position' => $option->getData('position'),
00196 'type' => $option->getData('type'),
00197 'title' => $option->getData('title')?$option->getData('title'):$option->getData('default_title'),
00198 'delete' => ''
00199 );
00200 foreach ($option->getSelections() as $selection) {
00201 $selectionRawData[$i][] = array(
00202 'product_id' => $selection->getProductId(),
00203 'position' => $selection->getPosition(),
00204 'is_default' => $selection->getIsDefault(),
00205 'selection_price_type' => $selection->getSelectionPriceType(),
00206 'selection_price_value' => $selection->getSelectionPriceValue(),
00207 'selection_qty' => $selection->getSelectionQty(),
00208 'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
00209 'delete' => ''
00210 );
00211 }
00212 $i++;
00213 }
00214
00215 $newProduct->setBundleOptionsData($optionRawData);
00216 $newProduct->setBundleSelectionsData($selectionRawData);
00217 return $this;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226 public function setAttributeTabBlock($observer)
00227 {
00228 $product = $observer->getEvent()->getProduct();
00229 if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
00230 Mage::helper('adminhtml/catalog')
00231 ->setAttributeTabBlock('bundle/adminhtml_catalog_product_edit_tab_attributes');
00232 }
00233 return $this;
00234 }
00235
00236
00237
00238
00239
00240
00241
00242 public function catalogProductLoadAfter(Varien_Event_Observer $observer)
00243 {
00244 $product = $observer->getEvent()->getProduct();
00245 if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
00246 Mage::getSingleton('bundle/price_index')
00247 ->addPriceIndexToProduct($product);
00248 }
00249
00250 return $this;
00251 }
00252
00253
00254
00255
00256
00257
00258
00259 public function catalogIndexPlainReindexAfter(Varien_Event_Observer $observer)
00260 {
00261 $products = $observer->getEvent()->getProducts();
00262 Mage::getSingleton('bundle/price_index')->reindex($products);
00263
00264 return $this;
00265 }
00266 }