I am having the Magento website. I want to remove the Out of Stock products from New Books Arrived tab of homepage, but it should be visible on categories of my website. I just want to remove it only from homepage, else it should be visible on other pages. For better understanding i am attaching the screenshot of my homepage. I had tried to copy the file New.php to local/Mage/Catalog/Block/Product/Widget/New.php & used below code, but still problem did not solved
New.php
<?php
/**
* New products widget
*
* @category Mage
* @package Mage_Catalog
* @author Magento Core Team <[email protected]>
*/
class Mage_Catalog_Block_Product_Widget_New extends Mage_Catalog_Block_Product_New
implements Mage_Widget_Block_Interface
{
/**
* Display products type
*/
const DISPLAY_TYPE_ALL_PRODUCTS = 'all_products';
const DISPLAY_TYPE_NEW_PRODUCTS = 'new_products';
/**
* Default value whether show pager or not
*/
const DEFAULT_SHOW_PAGER = false;
/**
* Default value for products per page
*/
const DEFAULT_PRODUCTS_PER_PAGE = 5;
/**
* Name of request parameter for page number value
*/
const PAGE_VAR_NAME = 'np';
/**
* Instance of pager block
*
* @var Mage_Catalog_Block_Product_Widget_Html_Pager
*/
protected $_pager;
/**
* Default product amount per row
*
* @var int
*/
protected $_defaultColumnCount = 5;
/**
* Initialize block's cache and template settings
*/
protected function _construct()
{
parent::_construct();
$this->addPriceBlockType('bundle', 'bundle/catalog_product_price', 'bundle/catalog/product/price.phtml');
}
/**
* Product collection initialize process
*
* @return Mage_Catalog_Model_Resource_Product_Collection|Object|Varien_Data_Collection
*/
protected function _getProductCollection()
{
switch ($this->getDisplayType()) {
case self::DISPLAY_TYPE_NEW_PRODUCTS:
$collection = parent::_getProductCollection();
break;
default:
$collection = $this->_getRecentlyAddedProductsCollection();
break;
}
return $collection;
}
/**
* Prepare collection for recent product list
*
* @return Mage_Catalog_Model_Resource_Product_Collection|Object|Varien_Data_Collection
*/
protected function _getRecentlyAddedProductsCollection()
{
/** @var $collection Mage_Catalog_Model_Resource_Product_Collection */
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
if (Mage::getBlockSingleton('page/html_header')->getIsHomePage()) {
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
}
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToSort('created_at', 'desc')
->setPageSize($this->getProductsCount())
->setCurPage(1);
return $collection;
}
/**
* Get key pieces for caching block content
*
* @return array
*/
public function getCacheKeyInfo()
{
return array_merge(parent::getCacheKeyInfo(), array(
$this->getDisplayType(),
$this->getProductsPerPage(),
intval($this->getRequest()->getParam(self::PAGE_VAR_NAME))
));
}
/**
* Retrieve display type for products
*
* @return string
*/
public function getDisplayType()
{
if (!$this->hasData('display_type')) {
$this->setData('display_type', self::DISPLAY_TYPE_ALL_PRODUCTS);
}
return $this->getData('display_type');
}
/**
* Retrieve how much products should be displayed
*
* @return int
*/
public function getProductsCount()
{
if (!$this->hasData('products_count')) {
return parent::getProductsCount();
}
return $this->getData('products_count');
}
/**
* Retrieve how much products should be displayed
*
* @return int
*/
public function getProductsPerPage()
{
if (!$this->hasData('products_per_page')) {
$this->setData('products_per_page', self::DEFAULT_PRODUCTS_PER_PAGE);
}
return $this->getData('products_per_page');
}
/**
* Return flag whether pager need to be shown or not
*
* @return bool
*/
public function showPager()
{
if (!$this->hasData('show_pager')) {
$this->setData('show_pager', self::DEFAULT_SHOW_PAGER);
}
return (bool)$this->getData('show_pager');
}
/**
* Render pagination HTML
*
* @return string
*/
public function getPagerHtml()
{
if ($this->showPager()) {
if (!$this->_pager) {
$this->_pager = $this->getLayout()
->createBlock('catalog/product_widget_html_pager', 'widget.new.product.list.pager');
$this->_pager->setUseContainer(true)
->setShowAmounts(true)
->setShowPerPage(false)
->setPageVarName(self::PAGE_VAR_NAME)
->setLimit($this->getProductsPerPage())
->setTotalLimit($this->getProductsCount())
->setCollection($this->getProductCollection());
}
if ($this->_pager instanceof Mage_Core_Block_Abstract) {
return $this->_pager->toHtml();
}
}
return '';
}
}
new_grid.phtml
<?php
/**
*/
?>
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="widget widget-new-products">
<div class="widget-title">
<h2><?php echo $this->__('New Books Arrived') ?></h2>
</div>
<div class="widget-products">
<?php echo $this->getPagerHtml() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; ?>
<ul class="products-grid products-grid--max-<?php echo $_columnCount; ?>-col-widget">
<?php foreach ($_products->getItems() as $_product): ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<?php $_imgSize = 210; ?>
<?php // The image size is locked at 210 for this for display purposes. CSS has it at 75% which should equate to 278px?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(210) ?>" alt="<?php echo $this->stripTags($_product->getName(), null, true) ?>" />
</a>
<div class="product-info">
<h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $this->helper('catalog/output')->productAttribute($_product, $_product->getName() , 'name') ?></a></h3>
<?php echo $this->getPriceHtml($_product, true, '-widget-new-grid') ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<div class="actions">
<?php if ($_product->isSaleable()): ?>
<button type="button" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="<?php echo $this->getAddToWishlistUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif; ?>
$this->getRequest()->getControllerName();
get controller name in list.phtml file so you can track page name cms-index or category-page and you can check if product is sale able or not$_product->isSaleable()