I have a Magento site where I need to remove some products from certain customer groups due to them having a zero price on the products.
I have tried catalog_product_collection_load_after
event and have successfully managed to remove the products that I want. This however causes a gap on the catagory pages where the products would be. Would there be any way to go about remove products from a collection either before the pagination is set on the collection or to "remake" the collection once you've removed the products.
The code I've tried so far:
/* Remove all products from the category list where the (group) price is zero */
public function removeZeroPriceProducts($observer) {
if (! Mage::app()->getStore()->isAdmin())
{
$collection = $observer->getEvent()->getCollection();
//Loop through all products in the collection
foreach ($collection as $product)
{
$origProduct = $product;
//If it is a configurable we need to get the childrens prices.
// Since if the customer isn't allowed to see the product all prices on all children will be zero we only need to get any of the children
if($this->getTypeId() == "configurable") {
$product = $product->getTypeInstance()->getUsedProductCollection()->addAttributeToSelect('price')->addPriceData()->setPageSize(1)->getFirstItem();
}
//Check if the price is zero. If so remove the product from the collection. Note that this --will-- cause a gap in the end of the page before the pagination.
if($origProduct->getFinalPrice() === 0 || $product->getFinalPrice() === 0)
$observer->getEvent()->getCollection()->removeItemByKey($origProduct->getId());
}
}
}
Edit: I've tried added it to _getProductCollection in List.php as suggested by Mohit. This removes the product as expected but unfortunately still leaves a gap. I'll leave the code for that here as well.
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$layer = $this->getLayer();
/* @var $layer Mage_Catalog_Model_Layer */
if ($this->getShowRootCategory()) {
$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
}
// if this is a product view page
if (Mage::registry('product')) {
// get collection of categories this product is associated with
$categories = Mage::registry('product')->getCategoryCollection()
->setPage(1, 1)
->load();
// if the product is associated with any category
if ($categories->count()) {
// show products from this category
$this->setCategoryId(current($categories->getIterator()));
}
}
$origCategory = null;
if ($this->getCategoryId()) {
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
if ($category->getId()) {
$origCategory = $layer->getCurrentCategory();
$layer->setCurrentCategory($category);
}
}
$this->_productCollection = $layer->getProductCollection();
$this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}
}
//Try and remove a specific ID from the collection
$collection = $this->_productCollection;
$collection->removeItemByKey(4146);
return $collection;
}