Magento 1.9.x - Blocks not being displayed from local module
-
28-09-2020 - |
Question
I am creating a module to add some extra call to action buttons. I cannot get an output to display. I believe I am getting either the naming conventions or the file location (or both) wrong.
It may also be the way I am trying to insert the block. I dont want to modify the phtml files if I can avoid it.
As you can see from my files, I am trying a number of ways to get the anything to be output - with no luck.
The aim is to be able to add additional CTA buttons after Add to Cart - such as 'Request Samples'
File structure:
app
Code
local
CompanyName
CtaAttributes
Block -> CtaButtons.php
etc -> config.xml
design
frontend
base
default
layout->ctabuttons.xml
template
ctaattributes
catelog
product ->ctabuttons.phtml
config.xml
<config>
... includes Module and Resource
<global>
<blocks>
<ctaattributes>
<class>CompanyName_CtaAttributes_Block</class>
</ctaattributes>
</blocks>
</global>
</config>
CtaButtons.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<catalog_product_view>
<reference name="content">
<block type="core/text_list" name="product.info.addtocart" before="addtocart" template="ctaattributes/catalog/product/ctabuttons.phtml"/>
<action method="append">
<block>ctabuttons</block>
</action>
</reference>
<reference name="product.info.addtocart">
<block type="core/text_list" name="core-text"><action method="setText"><text><![CDATA[<div>Test</div>]]></text></action></block>
</reference>
</catalog_product_view>
</layout>
ctabuttons.phtml
<?php
/**
*/
Mage::log(get_class($this));
echo 'this is my test';
CtaButtons.php
<?php
class CompanyName_CtaAttributes_Block_CtaButtons extends Mage_Core_Block_Template {
echo 'test';
}
Solution 4
I found a solution - it is in two parts.
I did not define an frontend->update path for my layout update. I inserted this after
</global>
<frontend> <layout> <updates> <ctaattributes> <file>ctaattributes.xml</file> </ctaattributes> </updates> </layout> </frontend>
Had I used local.xml, I dont believe this would have been required, but in my testing I thought I would change the file name to keep it separate.
- You cannot use camel case in the Block Class Name
Originaly my class was CompanyName_CtaAttributes_Block_CtaButtons
- with a corresponding file called CtaButtons.php
.
Changing the name to CompanyName_CtaAttributes_Block_Ctabuttons
and Ctabuttons.php
has resolved the issue.
I don't know if it is the Class file in Block/Blockname.php or the layout xml file that has a problem.
I have tested this with CamelCase in the company name and module with no impact, it seems to be restricted to the Blockname class and file.
OTHER TIPS
- The /Blocks/CtaButtons.xml in you file structure should be an php file.
- I don't see the module file which should be in /app/etc/modules
- You should have module Versioning in the config.xml
If you use the latest patch your blocks should also be allowed in the System->Permission->block
Hi you need to define block type first
From
<?xml version="1.0"?>
<layout version="0.1.0">
<catalog_product_view>
<reference name="content">
<block type="core/text_list" name="product.info.addtocart" before="addtocart" template="ctaattributes/catalog/product/ctabuttons.phtml"/>
<action method="append">
<block>ctabuttons</block>
</action>
</reference>
<reference name="product.info.addtocart">
<block type="core/text_list" name="core-text"><action method="setText"><text><![CDATA[<div>Test</div>]]></text></action></block>
</reference>
</catalog_product_view>
</layout>
To
<?xml version="1.0"?>
<layout version="0.1.0">
<catalog_product_view>
<reference name="product.info">
<block type="core/text_list" name="product.info.ctaattributes" before="addtocart" template="ctaattributes/catalog/product/ctabuttons.phtml"/>
</reference>
</catalog_product_view>
</layout>