/** * Add bundle price range index to Product collection * * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @return Mage_Bundle_Model_Price_Index */ public function addPriceIndexToCollection($collection) { $productObjects = array(); $productIds = array(); foreach ($collection->getItems() as $product) { /* @var $product Mage_Catalog_Model_Product */ if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) { $productIds[] = $product->getEntityId(); $productObjects[$product->getEntityId()] = $product; } } $websiteId = Mage::app()->getStore($collection->getStoreId())->getWebsiteId(); $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId(); $addOptionsToResult = false; $prices = $this->_getResource()->loadPriceIndex($productIds, $websiteId, $groupId); foreach ($productIds as $productId) { if (isset($prices[$productId])) { $productObjects[$productId]->setData('_price_index', true)->setData('_price_index_min_price', $prices[$productId]['min_price'])->setData('_price_index_max_price', $prices[$productId]['max_price']); } else { $addOptionsToResult = true; } } if ($addOptionsToResult) { $collection->addOptionsToResult(); } return $this; }
/** * Append review summary to product collection * * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @return Mage_Review_Model_Review */ public function appendSummary($collection) { $entityIds = array(); foreach ($collection->getItems() as $_itemId => $_item) { $entityIds[] = $_item->getEntityId(); } if (sizeof($entityIds) == 0) { return $this; } $summaryData = Mage::getResourceModel('review/review_summary_collection')->addEntityFilter($entityIds)->addStoreFilter(Mage::app()->getStore()->getId())->load(); foreach ($collection->getItems() as $_item) { foreach ($summaryData as $_summary) { if ($_summary->getEntityPkValue() == $_item->getEntityId()) { $_item->setRatingSummary($_summary); } } } return $this; }
/** * Retrieve specific data from products, that were intrested to patricular customer. * Writes Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection * to $_touchedProductsCollection * * @return array * ( * 'catalog_product_view' => array(), * 'catalog_product_comare_add' => array(), * 'checkout_cart_add_product' => array(), * 'wishlist_add_product' => array(), * 'shopping_cart_product' => array(), * 'wishlist_product' => array() * ) */ public function getTouchedProducts() { $result = array(); $ids = array(); foreach (Mage::getModel('checkout/cart')->getQuoteProductIds() as $productId) { $ids[$productId] = $productId; $result['shopping_cart_product'][$productId] = array('weight' => 1); } if (Mage::getSingleton('customer/session')->isLoggedIn()) { $result['wishlist_product'] = array(); foreach (Mage::getModel('wishlist/wishlist')->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer())->getProductCollection()->getItems() as $item) { $ids[$item->getProductId()] = $item->getProductId(); $result['wishlist_product'][$item->getProductId()] = array('weight' => 1); } } // get recently viewed products, added to shopping cart, to whishlist, to compare. $lastViewed = false; $breakTheViewsInRow = array(); foreach ($this->getResource()->getRecentlyIntrestedProductIds() as $item) { $ids[$item['product_id']] = $item['product_id']; $eventName = $item['event_name']; unset($item['event_name']); if (!isset($result[$eventName][$item['product_id']])) { $result[$eventName][$item['product_id']] = array('logged_at' => $item['logged_at'], 'product_id' => $item['product_id'], 'weight' => 0); } $result[$eventName][$item['product_id']]['weight']++; // calculating how many last views in a row of particular product we have if (!isset($result[$eventName][$item['product_id']]['view_in_row'])) { $result[$eventName][$item['product_id']]['view_in_row'] = 1; } elseif (false === $lastViewed || $lastViewed == $item['product_id'] && !isset($breakTheViewsInRow[$item['product_id']])) { $result[$eventName][$item['product_id']]['view_in_row']++; } else { $breakTheViewsInRow[$item['product_id']] = true; } $lastViewed = $item['product_id']; } // fill event recieved array with category_id, attribute_set_id, price $groups = $this->_getProductGroups(); $this->_touchedProductsCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', array('in' => $ids))->addFinalPrice(); // comment this line for out of stock items foreach ($this->_touchedProductsCollection->getItems() as $item) { foreach ($groups as $group) { if (isset($result[$group]) && isset($result[$group][$item->getEntityId()])) { $result[$group][$item->getEntityId()]['product_id'] = $item->getEntityId(); $price = $item->getFinalPrice(); if (!$price) { $price = $item->getCalculatedFinalPrice(); } if (!$price) { $price = $item->getPrice(); } $result[$group][$item->getEntityId()]['price'] = $price; if ($item->getAttributeSetId()) { $result[$group][$item->getEntityId()]['attribute_set_id'] = $item->getAttributeSetId(); } /* $categoryIds = $item->getCategoryIds(); $category = Mage::registry('current_category'); if (!$category || !in_array($category->getId(), $categoryIds)) { $category = $item->getCategoryCollection() ->setOrder('level', 'desc') ->getFirstItem(); } if ($category) { $result[$group][$item->getEntityId()]['category_id'] = array($category->getId()); } */ $result[$group][$item->getEntityId()]['category_id'] = $item->getCategoryIds(); $weightFactor = Mage::getStoreConfig("smartsuggest/event_importance/{$group}"); $result[$group][$item->getEntityId()]['weight'] *= is_numeric($weightFactor) ? $weightFactor : 1; } } } $result = array_merge(array_fill_keys($groups, array()), $result); return $result; }