Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Display CMS Blocks on product description? 
 
Mattse
Jr. Member
 
Total Posts:  9
Joined:  2009-08-05
Germany
 

Hey,

is there any possibility to show a CMS Block on the product description?

The following code only works on CMS pages:

{{block type="cms/block" block_id="any_block_name"}}

Or is there an extension for a similar “text blocks on product pages” function? something like a simple search and replace function for the whole page output (e.g. “[my text block]” is replaced by “This product contains this special material....") with admin interface?

Any help would be great!

Thanks,
Mattse

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

You can add a new method to the product model.

function getDescription(){
        $processor 
Mage::getModel('core/email_template_filter');
        
$html $processor->filter($this->getData('description'));
        return 
$html;
}

In this way you can use the same {{block}} and {{skin}} sections just like the static blocks and pages and e-mail templates.

For security reasons you can find a copy of this post here : 

http://marius-strajeru.blogspot.com/2010/01/problem-add-static-block-to-product.html

Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
bmotters
Member
 
Total Posts:  46
Joined:  2009-04-16
 

I don’t see how the code from the previous poster will solve the problem.  It just shows how to process some text through an email template filter.  That is useful, but only if the Cms block contains something fancy like “{{...}}” constructs which need to be expanded.  But it doesn’t show how to get the content of the Cms block.

Here is code that will retrieve the Cms block with an id of “mycmsblockid”

$content Mage::getModel('cms/block')
  ->
setStoreId($store_id)
  ->
load('mycmsblockid')
  ->
getContent();

You can then process this content through a “core/email_template_filter”, as shown by the previous poster, but only if that might be necessary:

$content Mage::getModel('core/email_template_filter')->filter($content);

However, all this is basically what a block of type “cms/block” will do anyway.  You can use a cms/block in a layout. It does not have to be within another cms/block or on a Cms page.  It will work anywhere.  Simply put the following within the block where you will use the cms/block in your layout file:

<block type="cms/block" block_id="mycmsblockid" name="mycmsblock" />

This can go either within a <reference> or within another <block> within your layout.  Once you have the cms/block in your layout, you can output this block in your template (or whereever) the usual way, with:

echo $this->getChildHtml('mycmsblock');

In some situations, the call to getChildHtml isn’t necessary, because the core code will output all the child blocks in a layout without any extra help from you.  I don’t know which way is simpler, editing the layout or editing the code. I guess it depends on whether you prefer editing PHP code in a template or editing XML code in a layout, in case you ever have to change the block id.

By the way, I don’t see what “security reasons’ there are for the previous poster to be putting his post on his blog and posting the link here.  Is that a bit of link-spamming, per chance?

Brian Mottershead
Riverscape Software

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

What I’ve posted before solves the problem if you want to print out something like this for the product description:

Some product description, bla bla....
CONTENT OF A STATIC BLOCK
Some other description....

And that was the whole point of the problem.
Try it before you say it doesn’t work.
If I misunderstood the problem please point it out to me. But this can be done only by Mattse, because only he knows his problem.

 
Magento Community Magento Community
Magento Community
Magento Community
 
bmotters
Member
 
Total Posts:  46
Joined:  2009-04-16
 

tzyganu,

I did try it.  Where in your code is the connection made to a static Cms block with a particular id?  If it works on your system as you say, then the call in your code

$this->getData('description')

must return “CONTENT OF A STATIC BLOCK”, or something that is going to turn into that if passed through an email template filter.  You don’t post the code that makes that true.  But it is precisely how that is done which the O.P is asking about. 

For your code to work, “{{block ... }}” has to be passed to the filter method of a core/email_template_filter model.

So some code like:

$html Mage::getModel('core/email_template_filter')->filter('{{block type="cms/block" block_id="mycmsblockid"}}');

would work, and would be another solution in addition to the two which I posted.  This code doesn’t have to be anywhere in particular to work.  If it works, it will work anywhere.  It could be in a template or a block, also.

But this isn’t the code you posted, is it?  I think in the code you posted, you were supposing that the {{block}} construct is magically in the “description” data field of some custom model, though you overlooked to mention this or how it gets there.  That might be true with the particular custom model you are working with.  But it is not generally going to be the case.  Indeed, the business about adding a getDescription method to a customized product model is pretty much irrelevant to the problem at hand.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Maybe what I forgot to post is that you don’t use getData(’description’). You should use getDescription().
If the method does not exist(as in the original Magento) it will have the same effect as getData(’description’). If it exists it will be called and will return the filtered ddescription, replacing {{block...}} with the content of the block.
It’s simple as that.

I know that what you are saying works anyware, but the main question was ‘is there any possibility to show a CMS Block on the product description?’ (see original post).

 
Magento Community Magento Community
Magento Community
Magento Community
 
bmotters
Member
 
Total Posts:  46
Joined:  2009-04-16
 
tzyganu - 19 January 2010 08:04 AM

Maybe what I forgot to post is that you don’t use getData(’description’). You should use getDescription().
If the method does not exist(as in the original Magento) it will have the same effect as getData(’description’). If it exists it will be called and will return the filtered ddescription, replacing {{block...}} with the content of the block.
It’s simple as that.

I know that what you are saying works anyware, but the main question was ‘is there any possibility to show a CMS Block on the product description?’ (see original post).

I still don’t get what getDescription has to do with this.  How does the model have “{{block...}}” in its $_data[’description’] field?  Are you telling the poster to put the {{block ...}} etc into the Description field of every product where he wants the static Cms block to be displayed, using the Admin UI?  If so, that is an important little detail that you might have mentioned.  And this would require someone to edit the Product Description of every relevant product to include the {{block}} reference.  Fine perhaps, if there are only one or two that need to have a static CMS block dropped in, but cumbersome if it is more than that.  This idea also depends on all other code calling getDescription rather than getData(’description’) to retrieve the product description.  Those two are normally equivalent, and I think it is a defect of the solution if it makes them not equivalent.  Finally, it requires a custom model, whereas the other approaches I mentioned just require editing layout files and/or templates.

As I see it, there are now four solutions to this problem:

(1) You can put <block type="cms/block" block_id="yourid" /> into the layout and use $this->getChildHtml() in the code of the parent block or the template for the parent block.  In some cases you can dispense with this getChildHtml call because all the child blocks of the parent block will be output automatically.

(2) You can call Mage::getModel("core/email_template_filter")->filter(’{{block type="cms/block" block_id="yourid"}}’) core/email_template_filter is what processes the various “{{ ... }}” constructs.

(3) You can also put the {{block }} construct into a product attribute, such as in the Product Description field of the products. Those {{block }} references have to be processed somewhere, and this does not happen in standard Magento except for a few places like the “content” of a CMS page and, of course, email templates.  So this means that you have to have custom code somewhere to filter those attributes through a core/email_template_filter. 

(4) All of the above let a “cms/block” Block do the work possibly combined with a core/email_template_filter.  You can also call Mage::getModel("cms/block")->setBlockId()->getContent(), You can then run the content that is returned through an core/email_template_filter, but normally that will only be necessary if there is something fancy like a {{..}} construct in the returned content.  The call to the getContent method of a cms/block model is what the cms/block code does for you, but nothing stops you from doing it yourself.

tzyganu seems to be advocating solution (3).  He says:  put the {{block ..}} reference by hand into the Product Description and filter the description in a custom product model with a custom getDescription method.  This requires a custom model and makes getDescription and getData(’description’) behave differently, which is unfortunate.  You could also do solution (3) and put the filtering in your template, but with this approach the filtering has to go somewhere.  If I were going to go with this approach, I think I would put the filtering into the template because editing templates is pretty routine and creating custom models is not.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mattse
Jr. Member
 
Total Posts:  9
Joined:  2009-08-05
Germany
 

Thank you very much, tzyganu and bmotters!
Solution 3 was the one I was looking for.
I need the {{block..}} thing in some descriptions only, and different blocks, too.

I put the filtering into the template, this was easy and it runs perfectly.

Thanks,
Mattse

 
Magento Community Magento Community
Magento Community
Magento Community
 
Zbynja
Jr. Member
 
Total Posts:  3
Joined:  2009-11-27
 

hi guys,
I was dealing with the same problem and just created a simple function to replace the content of whatever string you get from the Description attribute. It s probably not the ‘Magento approach’ solution but works for me. And mainly, you can call it with any attribute.

The function itself needs some more error handling and might be useful to replace with a RegEx pattern instead of using split arrays but enough for the rough idea..

so you load your Description content

$attr_content $_product->getDescription();
                        if(
$attr_content && strlen($attr_content)>0{
                            
echo checkStaticBlocks($attr_content,$this);
                        
}
and run it through a function :
function checkStaticBlocks($sInput,$oProduct)
{
    $l_delimiter 
'{{';
    
$r_delimiter '}}';
    
$aBeginings split($l_delimiter,$sInput);
    
$html '';
    foreach(
$aBeginings as $value)
    
{
           $aEndings 
split($r_delimiter,$value);
           if (
count($aEndings)==2){
             
if(strlen($aEndings[0])>0){
                 $html
.=$oProduct->getLayout()->createBlock('cms/block')->setBlockId($aEndings[0])->toHtml();
             
}
               $html
.=$aEndings[1];
           
}else{
             $html
.=$value;          
           
}
    }
    
return $html;
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Appmerce
Guru
 
Avatar
Total Posts:  376
Joined:  2008-05-13
Amsterdam, The Netherlands
 

To show a CMS static block anywhere, simply use this snippet:

echo $this->getLayout()->createBlock('cms/block')->setBlockId('identifier')->toHtml()

Change ‘identifier’ to your own identifier!

 
Magento Community Magento Community
Magento Community
Magento Community
 
mk2dev
Member
 
Total Posts:  42
Joined:  2008-08-07
Chicago, IL
 
tzyganu - 18 January 2010 03:44 AM

You can add a new method to the product model.

function getDescription(){
        $processor 
Mage::getModel('core/email_template_filter');
        
$html $processor->filter($this->getData('description'));
        return 
$html;
}

ok, not sure what all this drama is about, but I will say this: the above code added to Mage/Catalog/Mode/Product.php allows my client to use the code: {{block type="cms/block" block_id="whatever_the_id" template="cms/content.phtml"}} in a product description field and it shows up correctly on the front end.

solved my problem. Thanks for saving me the time of looking through the code!

 
Magento Community Magento Community
Magento Community
Magento Community
 
MaikelSmith
Member
 
Avatar
Total Posts:  59
Joined:  2009-03-31
 

Hey guys,

just one question: what happens, if a visitor searches for a product and the search string is for example: “cms/block”. Will the visitor see all products using the term “cms/block” in the description? Or will it be possible to use a word from the block content to search for, and the products using the block will be found?

If it’s not possible to search for the products, would it be possible by using the flat catalog function of magento?

*Maikel*

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

I never thought of this smile.
but I think it will find all the products with cms/block in the description.
This won’t work with a word from the static block.

I never tested it but this is the logical result. The search does not use model methods. It’s just a ‘blank’ text search.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tacitus
Member
 
Avatar
Total Posts:  49
Joined:  2009-03-25
 

@mk2dev

Yes!!! That works!!! Great… short and good answer matching the first question exactly! wink

The (full) path to product.php is:

/app/code/core/Mage/Catalog/Model/Product.php

1) Add

function getDescription(){
$processor = Mage::getModel(\’core/email_template_filter\’);
$html = $processor->filter($this->getData(\’description\’));
return $html;
}

to product.php

2) Make static block in magento admin: \"yourstaticblock\"

3) Add {{block type=\"cms/block\" block_id=\"yourstaticblock\" template=\"cms/content.phtml\"}}

That will work!

(just mentioned it twice to help search machine finding this important content wink)

Tacitus

 
Magento Community Magento Community
Magento Community
Magento Community
 
jpffm
Jr. Member
 
Total Posts:  24
Joined:  2010-09-29
 

Hi all,

this works like a charm, thanks for the good idea.

However, is it possible to change the code of the search engine somehow so it also parses the product description before searching it? Because as of now, the search engine will not find products which contain a specific text which is not included in the product description itself but in the loaded cms block.

Also, can the above mod be done to the short description as well?

Thanks a lot!

With best regards,
JP

 
Magento Community Magento Community
Magento Community
Magento Community
 
yldziner
Member
 
Total Posts:  52
Joined:  2008-04-20
 
Morningtime - 04 February 2010 03:16 AM

To show a CMS static block anywhere, simply use this snippet:

echo $this->getLayout()->createBlock('cms/block')->setBlockId('identifier')->toHtml()

Change ‘identifier’ to your own identifier!

where do you paste this?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2