/** * Assert that current visitor has exactly expected products in compare list * * @param array $expectedProductIds */ protected function _assertCompareListEquals(array $expectedProductIds) { $compareItems = new Mage_Catalog_Model_Resource_Product_Compare_Item_Collection(); $compareItems->useProductItem(true); // important $compareItems->setVisitorId(Mage::getSingleton('Mage_Log_Model_Visitor')->getId()); $actualProductIds = array(); foreach ($compareItems as $compareItem) { /** @var $compareItem Mage_Catalog_Model_Product_Compare_Item */ $actualProductIds[] = $compareItem->getProductId(); } $this->assertEquals($expectedProductIds, $actualProductIds, "Products in current visitor's compare list."); }
/** * Retrieve Product Compare items collection * * @return Mage_Catalog_Model_Resource_Product_Compare_Item_Collection */ public function getItems() { if (is_null($this->_items)) { Mage::helper('Mage_Catalog_Helper_Product_Compare')->setAllowUsedFlat(false); $this->_items = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Compare_Item_Collection')->useProductItem(true)->setStoreId(Mage::app()->getStore()->getId()); if (Mage::getSingleton('Mage_Customer_Model_Session')->isLoggedIn()) { $this->_items->setCustomerId(Mage::getSingleton('Mage_Customer_Model_Session')->getCustomerId()); } elseif ($this->_customerId) { $this->_items->setCustomerId($this->_customerId); } else { $this->_items->setVisitorId(Mage::getSingleton('Mage_Log_Model_Visitor')->getId()); } $this->_items->addAttributeToSelect(Mage::getSingleton('Mage_Catalog_Model_Config')->getProductAttributes())->loadComparableAttributes()->addMinimalPrice()->addTaxPercents()->setVisibility(Mage::getSingleton('Mage_Catalog_Model_Product_Visibility')->getVisibleInSiteIds()); } return $this->_items; }
/** * Retrieve compare list items collection * * @return Mage_Catalog_Model_Resource_Product_Compare_Item_Collection */ public function getItemCollection() { if (!$this->_itemCollection) { $this->_itemCollection = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Compare_Item_Collection')->useProductItem(true)->setStoreId(Mage::app()->getStore()->getId()); if (Mage::getSingleton('Mage_Customer_Model_Session')->isLoggedIn()) { $this->_itemCollection->setCustomerId(Mage::getSingleton('Mage_Customer_Model_Session')->getCustomerId()); } elseif ($this->_customerId) { $this->_itemCollection->setCustomerId($this->_customerId); } else { $this->_itemCollection->setVisitorId(Mage::getSingleton('Mage_Log_Model_Visitor')->getId()); } $this->_itemCollection->setVisibility(Mage::getSingleton('Mage_Catalog_Model_Product_Visibility')->getVisibleInSiteIds()); /* Price data is added to consider item stock status using price index */ $this->_itemCollection->addPriceData(); $this->_itemCollection->addAttributeToSelect('name')->addUrlRewrite()->load(); /* update compare items count */ $this->_getSession()->setCatalogCompareItemsCount(count($this->_itemCollection)); } return $this->_itemCollection; }