Magento: Get a custom variable value in a phtml file

I think custom variables were originally designed to be used in emails but I use them within the main site for little bits of information whose value needs to change on a per store/language basis.

This is how you get a custom variable inside a phtml file:

Mage::getModel(‘core/variable’)->setStoreId(Mage::app()->getStore()->getStoreId())->loadByCode(‘[variable-code]’)->getValue(‘plain’);

Continue reading Magento: Get a custom variable value in a phtml file

Magento Custom Layout Update: Robots No-index

You may well have certain pages that you do not want Google, et al, to index in the SERPS.  Examples include log in pages, thank you pages, and perhaps content of little search value such as legal blurb.  This can let you control a little more, albeit very slightly, the pages you want Google to show and those that you don’t.

You can use the Custom Layout Update control (in the Custom Design tab) to set the robots control on a per-page basis.  Simply add the following code into the Custom Layout Update textarea in the Magento admin:

<reference name=”head”>
<action method=”setRobots”><value>NOINDEX,NOFOLLOW</value></action>
</reference>

Continue reading Magento Custom Layout Update: Robots No-index

Magento 1.9: Useful Collection Snippets

Some useful bits and bobs to do with ordering, size-limiting and getting data out of collections that I tend to often need to look up.

Limiting the Size of a Collection
Adding isActiveFilter
Loading Friendly URLs/Rewrites
Filter Collection by Attribute and Conditionals List
Get First Item From Collection

Continue reading Magento 1.9: Useful Collection Snippets

Magento (v1.x) : Show full page action name (module_controller_action)

Sometimes, when editing a page you need to know what the full module_controller_action route is so you can modify the correct blocks in the layout files.  Here is a simple bit of code to display the full action name in the form of module_controller_action.

Continue reading Magento (v1.x) : Show full page action name (module_controller_action)

Magento: Get store address, phone number or other config information

Here is how to get the the store config information for the phone number:

<?php echo Mage::getStoreConfig(‘general/store_information/phone’);?>

And for the address:

<?php echo Mage::getStoreConfig(‘general/store_information/address’);?>

In fact you can get all sorts of interesting “config” data out using this  getStoreConfig method.

Continue reading Magento: Get store address, phone number or other config information

Magento: Programmatically added products do not appear in store until you load and save them in admin.

Ok, so you have written an import script to add a whole load of products into a store but they just don’t show on the front end.  There they are in the admin but not on the front end.  Here is one possible cause. Continue reading Magento: Programmatically added products do not appear in store until you load and save them in admin.

Magento Error: Integrity constraint violation: 1062 Duplicate entry ‘123456’ for key ‘PRIMARY’

Following a database export/import as part of a migration  or copy to a new development environment some database problems can occur, even if the entire db is copied.  Sometimes the problem can be the log files references, as is likely with the error:

Integrity constraint violation: 1062 Duplicate entry  ‘123456’ for key ‘PRIMARY’

Where 123456 is some number.
Continue reading Magento Error: Integrity constraint violation: 1062 Duplicate entry ‘123456’ for key ‘PRIMARY’

Mysql: Change the full text index minimum word length

So you are trying to do a fulltext search but your searches for “cat” or “dog” always return zero results, even though you have articles all about cats and dogs. By default the minimum search text length for full index searches is 4 characters, anything less than that returns nothing, zip, nada, jack-all.  You need to change the minimum word length.

This can throw people using something like Magento whose product types or names are short, such as “bag”,”cup”, or “mug”.  The problem isn’t actually in Magento but a server setting. You might be kind of stuck if you are using a shared hosting environment – you can contact your hosting service and ask nicely.

Continue reading Mysql: Change the full text index minimum word length