1

I need to call $this->getPriceHtml($_product, true); to my newly created block.

I called the product image, name and etc... but I didn't able to call the price.

Here is the code.

<?php
$manufacturer = Mage::registry('current_product')->getManufacturer();

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('manufacturer', $manufacturer);

/** I have tried with this**/
$productCollection = new Mage_Catalog_Block_Product_Abstract;

$productCollection->getSelect()->order('RAND()');
$productCollection->getSelect()->limit(5);

if ($productCollection->getSize() > 0) {
    foreach ($productCollection as $_product):
        ?>
        <div class="random-brand-products">
            <a href="<?php echo $_product->getProductUrl() ?>"
               title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image"><img
                    src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(228) ?>"
                    width="228" height="228" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/></a>

            <div class="product-details">
                <p class="product-name"><a
                        href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a>
                </p>

                <?php
                $merchant_name = $_product->getAttributeText('merchant_name');
                if ($merchant_name) {
                    ?>
                    <div>by <?php echo $merchant_name; ?> </div>
                <?php } else if ($_product->getIsEbayaffiliate()) { ?>
                    <div>by eBay</div>
                <?php } else { ?>
                    <div>by Home Done</div>
                <?php } ?> </div>

            /** I have tried with this**/
            $this->getPriceHtml($_product, true);

        </div>
        <?php
    endforeach;
} else {
    echo "There are no products matching the selection";
}
?>

I think I am doing something wrong the class $productCollection = new Mage_Catalog_Block_Product_Abstract;

Any suggestions would be appreciated.

2

Try:

<?php
$manufacturer = Mage::registry('current_product')->getManufacturer();

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('manufacturer',$manufacturer);
$productCollection->getSelect()->order('RAND()');
$productCollection->getSelect()->limit(5);

if($productCollection->getSize()>0){
    /** Try this **/
    $block = Mage::getSingleton('core/layout')->createBlock('catalog/product_price'); // or $this->getLayout()->createBlock('catalog/product_price') if exists
    foreach ($productCollection as $_product):
        ?>
        <div class="random-brand-products">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->resize(228) ?>" width="228" height="228" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
            <div class="product-details">
                <p class="product-name"><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></p>

                <?php
                $merchant_name = $_product->getAttributeText('merchant_name');
                if ($merchant_name){?>
                    <div>by <?php echo $merchant_name;?> </div>
                <?php }

                else if ($_product->getIsEbayaffiliate()) { ?>
                    <div>by eBay</div>
                <?php }

                else { ?>
                    <div>by Home Done</div>
                <?php } ?> </div>
            <?php
            $price = $block->getPriceHtml($_product);
            echo $price;
            ?>
        </div>
        <?php
    endforeach;
}else{
    echo "There are no products matching the selection";
}
?>
  • getting "Parse error: syntax error, unexpected 'if' (T_IF) in line 13" – Doodu Apr 3 '16 at 13:06
  • @Doodu Please, show the complete file here (with line 13) – Siarhey Uchukhlebau Apr 3 '16 at 14:04
  • I used the code you suggested above... No changes in that code... – Doodu Apr 3 '16 at 14:40
  • @Doodu It's strange, becouse the "IF" condition not exist on line 13. Could you update the your question with the actual code (copy-paste)? – Siarhey Uchukhlebau Apr 3 '16 at 14:53
  • It works... I didn't realize there was 2 options you given in line 13 "$block = Mage::getSingleton('core/layout')->createBlock('catalog/product_price'); // or $this->getLayout()->createBlock('catalog/product_price') if exists" ;) – Doodu Apr 3 '16 at 15:09

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.