2

I recently created a custom variable named storecount - which I'll be programatically increasing for every store that is added to my store locator plugin.

I'd like to show this in various locations including product and category descriptions so when we do add a new store, we're not having to find every occurrence of the store count and manually increase it by 1.

Using {{customVar code=storecount}} doesn't work in category descriptions. Is there any way I can get this variable without hacking the Magento core to pieces?

3 Answers 3

3

The easiest way to do it, is to use the same template processor for category description as it is used for the cms blocks and pages content. The one from the widget module.
For this you need an extension. Let's call it StackExchange_Catalog.
You will need the following files.

app/etc/modules/StackExchange_Catalog.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_Catalog>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
                <Mage_Cms />
                <Mage_Widget />
            </depends>
        </StackExchange_Catalog>
    </modules>
</config>

app/code/local/StackExchange/Catalog/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <global>
        <catalog>
             <content>
                <tempate_filter>widget/template_filter</tempate_filter>
            </content>
        </catalog>
    </global>
</config>

clear the cache and try again.

2
  • Awesome, thanks @Marius! Are there any security implications in doing this? Is there a particular reason why Magento don't allow the input of widgets etc in the category description? Oct 1, 2015 at 15:18
  • @LiamMcArthur. I cannot answer that question. I can only speculate that it might screw up the product search index if it's allowed for products. But I don't have a valid answer
    – Marius
    Oct 1, 2015 at 15:41
1

Without hacking Magento core, yes; without code, no.

You can write an observer that listens to the catalog_category_load_after event and manipulate the category description there.

/Edit: When the category with the altered description is saved, the custom variable will also be saved, so this is not a great solution.

1

Another way would be to add your storecount variable to a template file with either:

// To get the TEXT value of the custom variable:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('text');

// To get the HTML value of the custom variable:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('html');

If you're wondering which template, turn on Template Path Hints and you should be able to find where to add it into a template file. Don't override a core template, copy it into your theme's directory, so make a copy of it somewhere like app/design/frontend/[Your Design Package]/[Your Theme]/template/catalog/category/view.phtml. If you don't follow this practice you will loose your work when you upgrade Magento.

It's probably better practice to create CMS blocks and add it to your template files so it is easier to edit and turn off.

create your CMS block with your content including {{customVar code=storecount}}and add it where you need to in your template files with:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your_cms_block_identifier')->toHtml() ?>

UPDATE Just had a proper look at Marius's answer - his solution seems very neat and adds the constitutionality you need to Magento.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.