CSS – How to Hide Out of Stock Product Price

csshtmlPHPprice

I have this css method:

<?php if(!$_product->isSaleable()): ?>
<style type="text/css">

.price-box {display:none;}
</style>
<?php endif; ?>

to hide price for out of stock products, i placed it at the bottom of this file:

app/design/frontend/mytheme/default/template/catalog/product/view.phtml
and its working fine.

but when i put it in list.phtml to hide out of stock products prices in category view, it won't work.
this is my list.phtml including the code:

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
    $helper = $this->helper('shoppersettings/image');

    $listX = 252;
    $listY = $helper->calculateHeight($listX);
?>
<?php if(!$_productCollection->count()): ?>
<?php
$empty_category_block = Mage::getModel('cms/block')
    ->setStoreId( Mage::app()->getStore()->getId() )
    ->load('shopper_empty_category');
 if($empty_category_block->getIsActive()){
     echo $this->getLayout()->createBlock('cms/block')->setBlockId('shopper_empty_category')->toHtml();
 } else { ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php } ?>
<?php else: ?>
<div class="category-products">
    <?php echo $this->getToolbarHtml() ?>
    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <div class="f-left">
                <?php if (Mage::getStoreConfig('shoppersettings/ajax/cart')) : //quick view button ?>
                <a href='<?php echo $this->getUrl('ajax/index/options',array('product_id'=>$_product->getId()));?>' data-fancybox-group="gallery" class='fancybox quick-view' id='fancybox<?php echo $_product->getId()?>'><?php echo $this->__('quick view')?></a>
                <?php endif; ?>
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
                    <?php echo $this->helper('shoppersettings')->getLabel($_product);  ?>
                    <?php
                    $_product->load('media_gallery');
                    $back_img = $_product->getMediaGalleryImages()->getItemByColumnValue('label', 'back');
                    if ( $back_img ) {
                        echo '<img class="back_img" src="'. $this->helper('catalog/image')->init($_product, 'small_image', $back_img->getFile())->resize($listX, $listY).'" data-srcX2="'. $this->helper('catalog/image')->init($_product, 'small_image')->resize($listX*2, $listY*2) .'" width="'.$listX.'" height="'.$listY.'" alt="'.$this->htmlEscape($_product->getName()).'" />';
                    }
                    ?>
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($listX, $listY); ?>" data-srcX2="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($listX*2, $listY*2); ?>" width="<?php echo $listX ?>" height="<?php echo $listY ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
                </a>
            </div>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                    <div class="price-container clearfix">
                    <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php if($_product->getRatingSummary()){ echo $this->getReviewsSummaryHtml($_product); } ?>
                    </div>
                    <div class="desc std">
                        <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                    </div>
                    <div class="button-container">
                        <?php if($_product->isSaleable()): ?>
                            <?php if (Mage::getStoreConfig('shoppersettings/ajax/cart')) : ?>
                                <?php if ( !($_product->getTypeInstance(true)->hasRequiredOptions($_product) || $_product->isGrouped()) ) { ?>
                                <p><a href="<?php echo $this->getAddToCartUrl($_product) ?>" title="<?php echo $this->__('Add to Cart') ?>" class="btn-cart ajax-cart btn-add-cart" data-url="<?php echo $this->getAddToCartUrl($_product); ?>" data-id="<?php echo $_product->getId(); ?>"><span></span><?php echo $this->__('Add to Cart') ?></a></p>
                                <?php } else { ?>
                                <p><a href="<?php echo $this->getAddToCartUrl($_product) ?>" title="<?php echo $this->__('Add to Cart') ?>" class="btn-cart show-options btn-add-cart" data-id="<?php echo $_product->getId(); ?>"><span></span><?php echo $this->__('Add to Cart') ?></a></p>
                                <?php }  ?>
                            <?php else: ?>
                            <p><a href="<?php echo $this->getAddToCartUrl($_product) ?>" title="<?php echo $this->__('Add to Cart') ?>" class="btn-cart btn-add-cart"><span></span><?php echo $this->__('Add to Cart') ?></a></p>
                            <?php endif; ?>
                        <?php else: ?>
                            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                        <?php endif; ?>
                    </div>

                    <span class="ajax_loading" id='ajax_loading<?php echo $_product->getId()?>'><img src='<?php echo $this->getSkinUrl('images/ajax-loader.gif')?>'/></span>

                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><span></span><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist" data-id="<?php echo $_product->getId(); ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if ($_compareUrl=$this->getAddToCompareUrl($_product)) : ?>
                        <li><span></span><a href="<?php echo $_compareUrl ?>" class="link-compare" data-id="<?php echo $_product->getId(); ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>

                </div>
            </div>
        </li>
    <?php endforeach; ?>
    </ol>
    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

    <?php else: ?>

    <?php // Grid Mode ?>

    <?php
    $_collectionSize = $_productCollection->count();

    $pageLayout = $this->getLayout()->getBlock('root')->getTemplate();
    if ( strpos($pageLayout, '2columns') ) {
        $grid_class = 'two_columns_' . Mage::getStoreConfig('shoppersettings/catalog/grid_columns_2col');
    } else {
        $grid_class = 'one_column_' . Mage::getStoreConfig('shoppersettings/catalog/grid_columns_1col');
    }
    $imgX = 252;
    $imgY = $helper->calculateHeight($imgX);
    ?>
    <ul class="products-grid <?php echo $grid_class; ?>">
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <li class="item">

            <div class="regular">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image">
                    <?php echo $this->helper('shoppersettings')->getLabel($_product);  ?>
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgX, $imgY) ?>" data-srcX2="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgX*2, $imgY*2) ?>" width="<?php echo $imgX; ?>" height="<?php echo $imgY; ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
                </a>
                <div class="product-info">
                    <div class="button-container">
                        <?php if($_product->isSaleable()): ?>
                            <?php if (Mage::getStoreConfig('shoppersettings/ajax/cart')) : ?>
                                <?php if ( !($_product->getTypeInstance(true)->hasRequiredOptions($_product) || $_product->isGrouped()) ) { ?>
                                    <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="availability in-stock" data-url="<?php echo $this->getAddToCartUrl($_product); ?>" data-id="<?php echo $_product->getId(); ?>"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                                <?php } else { ?>
                                    <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="availability in-stock" data-id="<?php echo $_product->getId(); ?>"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                                <?php }  ?>
                            <?php else: ?>
                                <p><button type="button" title="<?php echo $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></p>
                            <?php endif; ?>
                        <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                        <?php endif; ?>
                    </div>
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a>
                    <?php echo $this->getPriceHtml($_product, true) ?>
                </div>
            </div>

            <div class="hover">
                <?php if (Mage::getStoreConfig('shoppersettings/ajax/cart')) : //quick view button ?>
                    <a href='<?php echo $this->getUrl('ajax/index/options',array('product_id'=>$_product->getId()));?>' data-fancybox-group="gallery" class='fancybox quick-view' id='fancybox<?php echo $_product->getId()?>'><?php echo $this->__('quick view')?></a>
                <?php endif; ?>
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image">
                    <?php echo $this->helper('shoppersettings')->getLabel($_product);  ?>
                    <?php
                    $_product->load('media_gallery');
                    $back_img = $_product->getMediaGalleryImages()->getItemByColumnValue('label', 'back');
                    if ( $back_img ) : ?>
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image', $back_img->getFile())->resize($imgX, $imgY) ?>" data-srcX2="<?php echo $this->helper('catalog/image')->init($_product, 'small_image', $back_img->getFile())->resize($imgX*2, $imgY*2) ?>" width="<?php echo $imgX; ?>" height="<?php echo $imgY; ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
                    <?php else : ?>
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgX, $imgY) ?>" data-srcX2="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgX*2, $imgY*2) ?>" width="<?php echo $imgX; ?>" height="<?php echo $imgY; ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
                    <?php endif; ?>
                </a>

                <?php if (Mage::getStoreConfig('sales/msrp/enabled')) :  ?>
                    <?php echo $this->getPriceHtml($_product, true);  ?>
                <?php else : ?>
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                        <?php echo $this->helper('shoppersettings/data')->priceFormat( $this->getPriceHtml($_product, true) ); ?>
                    </a>
                <?php endif; ?>

                <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                <a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a>
                <?php if($_product->getRatingSummary()){ echo $this->getReviewsSummaryHtml($_product); } ?>
                <div class="button-container">
                    <?php if($_product->isSaleable()): ?>

                        <?php if (Mage::getStoreConfig('shoppersettings/ajax/cart')) : ?>

                            <?php if ( !($_product->getTypeInstance(true)->hasRequiredOptions($_product) || $_product->isGrouped()) ) { ?>
                                <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart ajax-cart" data-url="<?php echo $this->getAddToCartUrl($_product); ?>" data-id="<?php echo $_product->getId(); ?>"><span><span><em></em><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                            <?php } else { ?>
                                <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart show-options" data-id="<?php echo $_product->getId(); ?>"><span><span><em></em><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                            <?php }  ?>

                        <?php else: ?>
                            <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><em></em><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                        <?php endif; ?>

                    <?php else: ?>
                    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                </div>

                <span class="ajax_loading" id='ajax_loading<?php echo $_product->getId()?>'><img src='<?php echo $this->getSkinUrl('images/ajax-loader.gif')?>'/></span>

                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                    <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist" data-id="<?php echo $_product->getId(); ?>"><span>+</span><?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if ($_compareUrl=$this->getAddToCompareUrl($_product)) : ?>
                    <li><a href="<?php echo $_compareUrl ?>" class="link-compare" data-id="<?php echo $_product->getId(); ?>"><span>+</span><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>

            </div>

        </li>
        <?php endforeach ?>
    </ul>
    <?php endif; ?>

    <div class="toolbar-bottom">
        <?php echo $this->getToolbarHtml() ?>
    </div>
</div>
<?php endif; ?>

<!--MENTIONED CSS METHOD-->
<?php if(!$_product->isSaleable()): ?>
<style type="text/css">

.price-box {display:none;}
</style>
<?php endif; ?>

Best Answer

Is there a reason for using css? I would do this instead:

Wrap all instances of echo $this->getPriceHtml($_product, true) in your if statement.

<?php if($_product->isSaleable()){
      echo $this->getPriceHtml($_product, true);
}
?>
Related Topic