3

On my home page I have a custom block that called a list of 4 "featured" products. This displays 4 products on my homepage. I want to style the price section quite a bit different then it displays on the actual product page.

How can I call a separate price template file for use just in that one block on my home page?

Here is the code I use to show the featured product.

<?php
/**
 * Small Featured Homepage Products
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">

    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i <= 2): ?>
        <?php if ($i++%$_columnCount==0): ?>

        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)->resize(135, null); ?>" width="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h3>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>

                <!-- THIS IS WHAT I WANT TO CHANGE -->
                <?php echo $this->getPriceHtml($_product, true) ?>

                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <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>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>
</div>
  • show us the code that you use to show that featured product – Rajeev K Tomy Oct 3 '14 at 2:42
  • you are not using cms page as your home page ? – Rajeev K Tomy Oct 3 '14 at 3:17
6

try this

<?php echo $this->getLayout()->createBlock('catalog/product_price')
            ->setTemplate('catalog/product/customprice.phtml')
            ->setProduct($_product)
            ->setDisplayMinimalPrice(true)
            ->setIdSuffix($idSuffix='amit')
            ->toHtml(); ?>

Instead of <?php echo $this->getPriceHtml($_product, true) ?>

| improve this answer | |
  • Awesome, that worked perfect! – Switch Oct 3 '14 at 15:30
3

Price block is hardly coded in Magento core. By default magento uses catalog/product/price.phtml file to render it's content. This block is actually set through Mage_Catalog_Block_Product_Abstract class. So the easy way would be change the following line in that class.

File : app\code\core\Mage\Catalog\Block\Product\Abstract.php

 protected $_priceBlockDefaultTemplate = 'path/to/your/custom/price.phtml';

But changing the core code is not recommended. So I am not promoting this direct change.

Actually there exists another alternative and it is the right method to do this. Magento will allow us to set custom template depends upon the product type. We are going to do this via layout update. So for this, I assume you are using CMS page for your home page. In that case,

File : app/design/frontend/<your_package>/<your_theme>/layout/local.xml

<layout>
    <cms_index_index>
        <reference name="product_list">
            <action method="addPriceBlockType">
                <type>simple</type>
                <block>catalog/product_price</block>
                <template>path/to/your/custom/price.phtml</template>
            </action>
            <action method="addPriceBlockType">
                <type>bundle</type>
                <block>catalog/product_price</block>
                <template>path/to/your/custom/price.phtml</template>
            </action>
            <action method="addPriceBlockType">
                <type>configurable</type>
                <block>catalog/product_price</block>
                <template>path/to/your/custom/price.phtml</template>
            </action>
            <action method="addPriceBlockType">
                <type>grouped</type>
                <block>catalog/product_price</block>
                <template>path/to/your/custom/price.phtml</template>
            </action>
            <action method="addPriceBlockType">
                <type>virtual</type>
                <block>catalog/product_price</block>
                <template>path/to/your/custom/price.phtml</template>
            </action>
            <action method="addPriceBlockType">
                <type>downloadable</type>
                <block>catalog/product_price</block>
                <template>path/to/your/custom/price.phtml</template>
            </action>
        </reference>
    </cms_index_index>
</layout>

As you can see, here we are setting custom template for all product type.

But this probably will not work. :) That is because, in order to work this code, it is required that, you need to define product_list block before this file get processed. Most probably you have added featured product via admin and those layout update will process after this file process. Hence it will not work.

So the actual solution is, you need to use this xml update in your cms page itself. But if you didn't do it properly there, it wont work again. But if you have good knowledge on magento layout update, then this is the best method that you can adopt and I highly recommend it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.