Exemple #1
0
 /**
  * Collects list of objects in product list
  *
  * @param Mage_Catalog_Block_Product_List_Related $object
  * @return Mage_Catalog_Model_Product[]
  */
 public function collect($object)
 {
     $result = array();
     $products = $object->getItems();
     foreach ($products as $product) {
         $result[] = $product;
     }
     return $result;
 }
 protected function _prepareData()
 {
     // Default back to core functionality
     if (!Mage::helper('predictions')->canOverrideRelatedProducts()) {
         return parent::_prepareData();
     }
     /* Retrieve related product ids here. */
     $predictionHelper = Mage::helper('predictions');
     $cookieId = array($predictionHelper->getCurrentUserUniqueId());
     $customer = Mage::getSingleton('customer/session');
     if ($customer->isLoggedIn()) {
         $cookieId[] = $customer->getId();
     }
     $recommendationCollection = Mage::getModel('predictions/recommendation')->getCollection()->addFieldToFilter('cookie_id', array('in' => $cookieId));
     // [todo] - Add a collection limit thorugh configuration
     // [todo] - Remove when refactored to single id
     //        if (Mage::getSingleton('customer/session')->isLoggedIn()) {
     //            $customerData = Mage::getSingleton('customer/session')->getCustomer();
     //            $recommendationCollection->addFieldToFilter('customer_id', array('eq' => $customerData->getId()));
     //        }
     $predictionioIds = $recommendationCollection->getColumnValues('product_id');
     // [review] - Review the logic for loading the product collection for this block
     $this->_itemCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('entity_id', array('in' => $predictionioIds))->addAttributeToSelect('required_options')->addStoreFilter();
     if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
         Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection, Mage::getSingleton('checkout/session')->getQuoteId());
         $this->_addProductAttributesAndPrices($this->_itemCollection);
     }
     Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);
     $this->_itemCollection->load();
     foreach ($this->_itemCollection as $product) {
         $product->setDoNotUseCategoryId(true);
     }
     return $this;
 }
Exemple #3
0
 public function testAll()
 {
     $product = new Mage_Catalog_Model_Product();
     $product->load(2);
     Mage::register('product', $product);
     $block = new Mage_Catalog_Block_Product_List_Related();
     $block->setLayout(new Mage_Core_Model_Layout());
     $block->setTemplate('product/list/related.phtml');
     $html = $block->toHtml();
     $this->assertNotEmpty($html);
     $this->assertContains('Simple Related Product', $html);
     /* name */
     $this->assertContains('product/1/', $html);
     /* part of url */
     $this->assertInstanceOf('Mage_Catalog_Model_Resource_Product_Link_Product_Collection', $block->getItems());
 }
Exemple #4
0
 protected function _prepareData()
 {
     parent::_prepareData();
     $_enabled = Mage::getStoreConfig('autorelated/general/enabled');
     if ($_enabled && count($this->getItems()) == 0) {
         $_products = Mage::getModel('autorelated/collection')->getRelatedProducts();
         if ($_products) {
             $this->_itemCollection = $_products;
         }
     }
     return $this;
 }
 /**
  * Retrives block html code
  * @return string
  */
 protected function _beforeToHtml()
 {
     $this->_prepareData();
     $this->disableRelated();
     return parent::_beforeToHtml();
 }
Exemple #6
0
 protected function _prepareData()
 {
     /* @var $product Mage_Catalog_Model_Product */
     $product = Mage::registry('product');
     // A bit of a hack, but return an empty collection if user selected 0 recommendations to show in config
     $limit = Mage::getStoreConfig('recommender/relatedproducts/numberofrelatedproducts');
     if ($limit < 1) {
         $this->_itemCollection = $product->getRelatedProductCollection();
         $this->_itemCollection->load();
         $this->_itemCollection->clear();
         return $this;
     }
     // Get manual links
     // Set link source to automated CommerceStack recommendations
     $linkModel = $product->getLinkInstance();
     $linkModel->useLinkSourceManual();
     parent::_prepareData();
     $unionLinkedItemCollection = $this->_itemCollection;
     $numRecsToGet = $limit;
     if (!is_null($unionLinkedItemCollection)) {
         $numRecsToGet = $limit - count($unionLinkedItemCollection);
     }
     if ($numRecsToGet > 0) {
         // Figure out if we should use a category filter
         $constrainCategory = Mage::getStoreConfig('recommender/relatedproducts/constraincategory');
         $currentCategory = Mage::registry('current_category');
         $productCategory = $currentCategory;
         if (is_null($currentCategory)) {
             // This could be a recently viewed or a search page. Try to get category collection and arbitrarily use first
             /* @var $currentProduct Mage_Catalog_Model_Product */
             $currentProduct = Mage::registry('current_product');
             if (is_object($currentProduct)) {
                 $currentCategory = $currentProduct->getCategoryCollection();
                 $productCategory = $currentCategory->getFirstItem();
                 $currentCategory = $productCategory;
             }
         }
         $useCategoryFilter = !is_null($currentCategory) && $constrainCategory;
         // Set link source to automated CommerceStack recommendations
         $linkModel = $product->getLinkInstance();
         $linkModel->useLinkSourceCommerceStack();
     }
     while ($numRecsToGet > 0) {
         $linkedItemCollection = $product->getRelatedProductCollection()->addAttributeToSelect('required_options')->setGroupBy()->setPositionOrder()->addStoreFilter();
         $linkedItemCollection->getSelect()->limit($numRecsToGet);
         if ($useCategoryFilter) {
             $linkedItemCollection->addCategoryFilter($currentCategory);
         }
         if (!is_null($unionLinkedItemCollection)) {
             $linkedItemCollection->addExcludeProductFilter($unionLinkedItemCollection->getAllIds());
         }
         Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($linkedItemCollection, Mage::getSingleton('checkout/session')->getQuoteId());
         $this->_addProductAttributesAndPrices($linkedItemCollection);
         Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($linkedItemCollection);
         $linkedItemCollection->load();
         // Add new source linked items to existing union of linked items
         if (is_null($unionLinkedItemCollection)) {
             $unionLinkedItemCollection = $linkedItemCollection;
         } else {
             foreach ($linkedItemCollection as $linkedProduct) {
                 $unionLinkedItemCollection->addItem($linkedProduct);
             }
         }
         if (!is_null($unionLinkedItemCollection)) {
             $numRecsToGet = $limit - count($unionLinkedItemCollection);
         }
         if (!$useCategoryFilter) {
             break;
         }
         // Go up a category level for next iteration
         $currentCategory = $currentCategory->getParentCategory();
         if (is_null($currentCategory->getId())) {
             break;
         }
     }
     // If we still don't have enough recommendations fill out the remaining with randoms.
     if ($numRecsToGet > 0) {
         $currentCategory = $productCategory;
     }
     while ($numRecsToGet > 0) {
         $randCollection = Mage::getResourceModel('catalog/product_collection');
         Mage::getModel('catalog/layer')->prepareProductCollection($randCollection);
         $randCollection->getSelect()->order('rand()');
         $randCollection->addStoreFilter();
         $randCollection->setPage(1, $numRecsToGet);
         $randCollection->addIdFilter(array_merge($unionLinkedItemCollection->getAllIds(), array($product->getId())), true);
         Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($randCollection);
         if ($useCategoryFilter) {
             $randCollection->addCategoryFilter($currentCategory);
         }
         foreach ($randCollection as $linkedProduct) {
             $unionLinkedItemCollection->addItem($linkedProduct);
         }
         if (!$useCategoryFilter) {
             break;
         }
         // We tried everything
         if (!is_null($unionLinkedItemCollection)) {
             $numRecsToGet = $limit - count($unionLinkedItemCollection);
         }
         // Go up a category level for next iteration
         $currentCategory = $currentCategory->getParentCategory();
         if (is_null($currentCategory->getId())) {
             break;
         }
     }
     $this->_itemCollection = $unionLinkedItemCollection;
     foreach ($this->_itemCollection as $product) {
         $product->setDoNotUseCategoryId(true);
     }
     return $this;
 }
Exemple #7
0
 /**
  * @param string $html
  * @param Mage_Catalog_Block_Product_List_Related $block
  * @return string
  */
 private function _processRelatedProducts($html, $block)
 {
     $helper = $this->_getHelper();
     /** @var $processor Mageplace_Callforprice_Model_Htmlprocessor_Interface */
     $processor = Mage::getModel('mageplace_callforprice/htmlprocessor_factory')->createProcessor();
     $processor->load($html);
     if ($helper->isGlobalEnabled()) {
         $processor->replace($helper->getCssSelector('upsell_product_price'), $helper->prepareReplacement());
         return $processor->getHtml();
     }
     $processor->process('related', array('products' => $block->getItems()));
     return $processor->getHtml();
 }