Magento – How to call a block method from a script file

blocksscript

I'm running a script off my root directory, http://www.example.com/my_script.php.

Within this script, I am trying to call a function from Mage_Catalog_Block_Product_Abstract:

public function getPriceHtml($product, $displayMinimalPrice = false, $idSuffix = '')
{
    $type_id = $product->getTypeId();
    if (Mage::helper('catalog')->canApplyMsrp($product)) {
        $realPriceHtml = $this->_preparePriceRenderer($type_id)
            ->setProduct($product)
            ->setDisplayMinimalPrice($displayMinimalPrice)
            ->setIdSuffix($idSuffix)
            ->toHtml();
        $product->setAddToCartUrl($this->getAddToCartUrl($product));
        $product->setRealPriceHtml($realPriceHtml);
        $type_id = $this->_mapRenderer;
    }

    return $this->_preparePriceRenderer($type_id)
        ->setProduct($product)
        ->setDisplayMinimalPrice($displayMinimalPrice)
        ->setIdSuffix($idSuffix)
        ->toHtml();
}

How can I call Mage::Catalog_Block_Product_Abstract::getPriceHtml from within my script file? I've tried:

  1. Creating an object using Mage::getBlockSingleton('catalog/product_abstract')->getPriceHtml

Which results in this error:

Fatal error: Call to a member function getPriceHtml() on a non-object
  1. Class inclusion

Like this:

include('../app/code/core/Mage/Catalog/Block/Product/Abstract.php');
$catalog_block = new Mage_Catalog_Block_Product_Abstract();

Which results in:

Fatal error: Cannot instantiate abstract class Mage_Catalog_Block_Product_Abstract

Because it is an abstract class.

Is it possible for me to do this somehow?

Best Answer

the Mage_Catalog_Block_Product_Abstract is abstract so it cannot be instantiated.
But you can use one of its child classes. For example Mage_Catalog_Block_Product_View.

You can try this:

Mage::app()->getLayout()->createBlock('catalog/product_view')->getPriceHtml(....)
Related Topic