3

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?

  • Did you clear the cache? – Black Sep 17 '18 at 21:44
  • Any errors in the logfiles? – Black Sep 18 '18 at 7:49
  • Yes, I cleared the cache, and there's nothing in the error-logs (about this error). – Kennet Leth Sørensen Sep 18 '18 at 20:39
0

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.

  • Adding ´myname_´ to the block reference doesn't work. It doesn't work on the live server and breaks the module in my local environment. Also it's a PHP block, so it doesn't show in ´System -> Permissions -> Blocks´ (only CMS blocks created in the admin panel are visible here)... – Kennet Leth Sørensen Sep 18 '18 at 15:31
  • In your view.phtml try this code and let me know the output $block=$this->getLayout()->createBlock('colorchart/colorchart'); echo get_class($block); – aman Sep 19 '18 at 1:46
  • On my local host it echos MyName_ColorChart_Block_ColorChart but i echoes nothing on my live server? I changed tried changing the last part of the code to if ($block){echo get_class($block);} else {echo 'No block';} and it echoes No block on my live server? – Kennet Leth Sørensen Sep 19 '18 at 18:53
  • I've tried everything on the server and do a fresh git clone and importing a fresh copy of the DB from my localhost... Still, it doesn't work on the server, even though it's an exact copy of my localhost? It's weird? – Kennet Leth Sørensen Sep 19 '18 at 18:57
  • Please check one more thing: module output is disabled or enabled for myname_colorchart in System > Configuration > Advanced > Disable Module Output. – aman Sep 20 '18 at 1:44
0

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).

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.