Magento 1.9 – Product Price is Zero Only on Homepage

categorymagento-1.9priceproduct

The products on the category pages are doing fine, price is showing correctly.

On the homepage, I made an extra products list (via layout.xml block), which shows products from a specific category (category ID 40). Everything is doing fine, except for the price, which shows zero.

My homepage.products.phtml file:

<?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@magento.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.magento.com for more information.
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.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
 */
/* @var $this Mage_Catalog_Block_Product_List */
?>
<?php

$category_id = 40;
$catagory_model = Mage::getModel('catalog/category')->load($category_id); 
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model);
$collection->addAttributeToFilter('status',1);
$collection->addAttributeToSelect(array('name','url','small_image'));
$collection->addStoreFilter();          
$collectionSize = $collection->count();
$columnCount = $this->getColumnCount();
$_helper = $this->helper('catalog/output');
$i = 0;

?>

<div class="std">
<div class="widget widget-new-products">
<div class="widget-title">
    <h2><?php echo $this->__('Our tip for a fantastic trip!'); ?></h2>
    <p><?php echo $this->__('Experience it on board'); ?></p>
</div>

<?php
if(!empty($collection))
{ ?>
<div class="widget-products">
    <?php $collectionSize = $collection->count() ?>
    <?php $columnCount = $this->getColumnCount(); ?>
    <ul class="products-grid products-grid--max-<?php echo $columnCount; ?>-col first odd">

        <?php foreach ($collection as $product){ ?>
        <?php if ( $i < 4 ) { ?>

            <?php $productID = $product->getId(); ?>
            <?php $options = Mage::getModel('catalog/product_option')->getProductOptionCollection($product); ?>
            <?php $qtyTotal = 0; ?>
            <?php $attrId = $product->getAttributeSetId(); ?>

            <?php foreach ($options as $option): ?>
                <?php foreach($option->getValues() as $_value): ?>
                    <?php $qty = $_value->getCustomoptionsQty() ?>
                    <?php if (!empty($qty)): ?>
                        <?php $qtyTotal += $qty; ?>
                    <?php endif; ?>
                <?php endforeach; ?>
            <?php endforeach; ?>

                <li class="item<?php if($i == 0): ?> first<?php elseif($i == 3): ?> last<?php endif; ?> item<?php echo $i; ?><?php if( $qtyTotal <= 0 && $attrId != 12 ): ?> full<?php endif; ?> attr<?php echo $attrId; ?>">
                    <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" class="product-image">

                        <?php 
                            $imgSizeWidth = 220; 
                            $imgSizeHeight = 152;
                        ?>
                        <div class="catalog-item-image" id="product-collection-image-<?php echo $product->getId(); ?>"
                            style="background-image:url('<?php echo $this->helper("catalog/image")->init($product, "small_image")->keepFrame(false)->resize($imgSizeWidth,$imgSizeHeight); ?>');">
                        </div>
                    </a>
                    <h2 class="product-name"><a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($product->getName(), null, true) ?>"><?php echo $product->getName(); ?></a></h2>
                    <?php if ( $product->getAttributeText('period') != '' ) { ?>
                        <p class="period-box">
                            <label class="label">
                                <?php $period = $product->getAttributeText('period'); ?>
                                <?php echo $period; ?>
                            </label>
                        </p>
                    <?php } ?>
                    <p class="price">
                        <?php 
                        // Check if attributeset is 'complete-ship'
                        if ( $attrId != 12 )
                        {
                            // If attributeset is not 'complete-ship', show normal price
                            if ( !empty($this->getPriceHtml($product, true)) )
                            {
                                var_dump($this->getPriceHtml($product, true));
                            }
                            else
                            {
                                echo $this->__('Price on request'); 
                            }
                        } 
                        // If attributeset is 'complete-ship', and attribute 'price_request' it's not empty
                        else if ( $attrId == 12 && $product->getAttributeText('price_request') != '' )
                        {
                            echo $product->getAttributeText('price_request');
                        }
                        // If attribute 'price-request' is empty, and product is in stock
                        else if ( $product->getAttributeText('price_request') == '' && $qtyTotal > 0 )
                        {
                            echo $this->__('Price on request');                       
                        }
                        // If product is out of stock and attributeset is not 'complete-ship'
                        else if ( $qtyTotal <= 0 && $attrId != 12 )
                        {
                            echo $this->__('Out of stock');
                        }
                        // else
                        else {
                            echo $this->__('Price on request');     
                        }
                        ?>
                    </p>
                </li>
            <?php $i++; ?>
            <?php } ?>      
        <?php } ?>
    </ul>
    <script type="text/javascript">docReady(function() { decorateGeneric($$('ul.products-grid'), ['odd','even','first','last']) })</script>
</div>
<?php
}
else
{
    ?><p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p><?php
}              
?>
</div>
</div>

I copied most of the code from the template > catalog > product > list.phtml file.

The layout:

<reference name="root">
    <block type="catalog/product_widget_new" name="home_products" before="follow_vessel" template="custom/homepage.products.phtml">
        <action method="setBlockId"><block_id>home_products</block_id></action>
    </block>
</reference>

UPDATE

I have changed my block type to catalog/product, and it seems the error lies in this line:

$this->getPriceHtml($product, true)

Also, the console returns this error:

(index):136 Uncaught ReferenceError: Mage is not defined(…)

Best Answer

You can do it in another way like instead of loding priceHtml templatedirectly use final price of product and format that price according to need.

$product = Mage::getModel('catalog/product')->load($product->getId());

$productPrice = Mage::helper('core')->currency($product->getFinalPrice(), true, false);
Related Topic