Simply put, I’m getting this error in the Product Admin of Magento after adding a new edit tab.
Fatal error: Call to a member function createBlock() on a non-object in
/var/www/app/code/local/RedoxStudios/ErpTab/Block/Adminhtml/Catalog/Product/Tab.php
on line 11
I’m having this in my code:
<?php
class RedoxStudios_ErpTab_Block_Adminhtml_Catalog_Product_Tab
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface {
/*
* Set the template for the block
*/
public function __construct() {
parent::__construct();
$this->getLayout()->createBlock('Purchase/Product_Widget_StockDetails_Summary');
$this->setProduct($this->getProduct());
$this->setTemplate('Purchase/Product/StockDetails/Summary.phtml');
}
/**
* Return current product instance
*
* @return Mage_Catalog_Model_Product
*/
public function getProduct()
{
return Mage::registry('product');
}
}
Previously I was able to just call the createBlock function. I’m I overlooking something that makes it that I cannot call this function?
Summary.phtml:
<div class="stock-details-summary">
<table border="0">
<tr>
<td class="a-right"><?php echo $this->__('Waiting for delivery'); ?> : </td>
<td class="a-right"><?php echo ($this->getWaitingForDeliveryQty() ? $this->getWaitingForDeliveryQty() : 0); ?></td>
</tr>
<tr>
<td class="a-right">
<?php echo $this->__('Manual supply need'); ?> :
<?php if ($this->getManualSupplyNeedQty() > 0): ?>
<i><?php echo $this->getProduct()->getmanual_supply_need_comments(); ?></i>
<?php endif; ?>
</td>
<td class="a-right">
<?php echo $this->getManualSupplyNeedQty(); ?>
</td>
</tr>
<tr>
<td class="a-right"><?php echo $this->__('Min qty to purchase'); ?> : </td>
<td class="a-right"><font color="red"><?php echo $this->getTotalNeededQtyForValidOrdersMinusWaitingForDelivery(); ?></font></td>
</tr>
<tr>
<td class="a-right"><?php echo $this->__('Max qty to purchase'); ?> : </td>
<td class="a-right" width="60"><font color="red"><?php echo $this->getTotalNeededQtyMinusWaitingForDelivery(); ?></font></td>
</tr>
<tr>
<td class="a-right"><?php echo $this->__('Status'); ?> : </td>
<td class="a-right"><?php echo $this->getGeneralStatus(); ?></td>
</tr>
</table>
</div>
You are not correctly getting the layout object (
Mage_Core_Model_Layout). In action controllers and blocks it’s$this->getLayout()->createBlock(), everywhere else it’sMage::app()->getLayout()->createBlock()EDIT: Sergy also pointed out that the layout object is not loaded, and this caused me to realize that you are using the php
__construct(), rather than the typical Magento_construct(). Block instances do not have the layout object set on them until after they are instantiated (and their constructor has been called) inMage_Core_Model_Layout::createBlock()– notice in that method how the block instance gets the layout set on it via itssetLayout()method. This is the purpose behind the block method_prepareLayout()– it’s a constructor-like method which is fired after the block instance is created.Corrections to your code below: