Question

I've added a custom block to catalog/product/view.phtml using echo $this->getLayout()->createBlock('colorchart/colorchart')->setTemplate('colorchart/colorchart.phtml')->toHtml(); and it's working great on my localhost (Wamp) but I get this error when pushing to my live server: Fatal error: Uncaught Error: Call to a member function toHtml() on boolean in /var/www/..../template/catalog/product/view.phtml

I've also tried just using echo $this->getLayout()->createBlock('colorchart/colorchart')->toHtml(); in my view.phtml file and then the following in my block (app/code/local/MyName/ColorChart/Block/ColorChart.php):

 public function __construct()
{
    $this->setTemplate('colorchart/colorchart.phtml');
}

Again, this works perfectly on my localhost but not on the live server? I run the same PHP & magento version on localhost and the server...?

I've mostly been working with Magento 2, so I'm a bit lost with Magento 1.9.3? I don't think I need a controller as I'm not targeting an URL or rendering a full page? Maybe I need to create a layout XML file, but I can't figure out where to put this for my custom block/module in Magento 1.9.3? Here is my code:

app/code/local/MyName/ColorChart/Block/ColorChart.php:

<?php
class MyName_ColorChart_Block_ColorChart extends Mage_Core_Block_Template
{/** MyName_ColorChart_Block_ColorChart constructor. */
public function __construct()
{
    $this->setTemplate('colorchart/colorchart.phtml');
}
public function getColorProducts($id) {
   // several functions below here
}

app/code/local/MyName/ColorChart/etc/config.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <MyName_ColorChart>
      <version>0.1.0</version>
    </MyName_ColorChart>
  </modules>
  <global>
    <blocks>
      <colorchart>
        <class>MyName_ColorChart_Block</class>
      </colorchart>
    </blocks>
  </global>
</config>

app/design/frontend/myname/mytheme/template/colorchart/colorchart.phtml:

<?php $mainProduct = Mage::registry('current_product'); ?>
    <?php if ($mainProduct->getColorChart()) : ?>
        <?php $selectValues = $this->allNewFunction($mainProduct->getId()); ?>
        <div> .... a lot of HTML code and PHP logic .... </div>

app/design/frontend/myname/mytheme/template/catalog/product/view.phtml:

//...snip...
<?php if ($_product->getShortDescription()):?>
    // code from magento here
<?php endif;?>

<div class="pro-left">
<!-- ------ I put my code here (Color Chart) ------ -->
    <?php
    echo $this->getLayout()->createBlock('colorchart/colorchart')->toHtml();
    ?>
<!-- ------ End of my code ------ -->
<div class="add_to_cart">
   <?php if (!$this->hasOptions()):?> // 
// ...snip...

- - - EDIT - - -

I tried including the block using XML:

<catalog_product_view translate="label">
    <reference name="content">
        <block type="colorchart/colorchart" name="colorchart" template="colorchart/colorchart.phtml" />
    </reference>
</catalog_product_view>

and removing $this->getLayout()->createBlock('colorchart/colorchart')->toHtml()from view.phtmland this also works on my localhost but not on my live server?

Was it helpful?

Solution 2

Found a solution (but still don't know why my original code doesn't work on my server?).

Instead of using $this->getLayout()->createBlock() i tried just creating a new instance of the block with $main_block = new MyName_ColorChart_Block_ColorChart(); and now it works?

I can do a echo $main_block->setTemplate('colorchart/colorchart.phtml')->toHtml(); and the block renders perfectly on my product page...

I'm thinking that my getLayout() call fails on my live server for some reason? Strange when it works on my local host which is an exact copy of the live server (or actually the other way around, I git push from local to my repo and git pull on the live server).

OTHER TIPS

Try to use this code

echo $this->getLayout()->createBlock('myname_colorchart/colorchart')->toHtml();

Instead:

echo $this->getLayout()->createBlock('colorchart/colorchart')->toHtml();

Also check if you have allow the particular block you can allow block from:

System-> Permissions -> Blocks 

Try if it will help you or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top