Calling $this->getChildHtml on a new block outside template file
-
12-12-2019 - |
Question
I'd like to call $this->getChildHtml('product_type_data')
to get product's price outside view.phtml template file.
Currently I have echo $this->getChildHtml('product_type_data')
and that outputs contents of 'catalog/product/view/type/default.phtml'.
I need to move it to a file in a custom module(cache hole punch) so that it gets called separately. So in this file I've done:
$block = Mage::app()->getLayout()->createBlock('customerprices/catalog_product_view', $this->getId());
$block->setTemplate('catalog/product/view/type/default.phtml');
$block->setIsLoggedIn(Mage::getSingleton('customer/session')->isLoggedIn());
$out = $block->getChildHtml('product_type_data');
But in this case $out contains an empty string. Do I need to set something on this block so that it will return price html? If so how? I have a current $_product object if that's needed.
I would appreciate any suggestions.
Using Mage::getBlockSingleton(...)
when I use this code:
$thatx = Mage::getBlockSingleton('catalog/product_view');
$cnamex = get_class($thatx);
echo 'cname: '.$cnamex;
$cnamex does not contain class name(empty string/variable). I consequence I can't run any methods on it.
Solution
If you need only product price block to be rendered you can use like this:
$out=Mage::getBlockSingleton('catalog/product_view')->getPriceHtml($_product);
See getPriceHtml()
method of Mage_Catalog_Block_Product_Abstract
which is parent class of Mage_Catalog_Block_Product_View
Second Way
Extend your block customerprices/catalog_product_view
from Mage_Catalog_Block_Product_Abstract
and use it:
class MyCompany_Customerprices_Catalog_Product_View extends Mage_Catalog_Block_Product_Abstract{
}
and
$block = Mage::app()->getLayout()->createBlock('customerprices/catalog_product_view', $this->getId());
$out=$block->getPriceHtml($_product);