How to validate form fields in magento without submitting the form

If you want to validate a form in Magento using prototype validations, if you use


myForm = new VarienForm('myform-id'); myform.submit = function () { results=myForm.prototype.submit.bind(myForm)();

then the form will be submitted automatically if there is no error. That’s perfectly fine unless you want to do other work before submitting, for example submitting the form in an ajax request.

Instead you can do this to test


myForm = new VarienForm('myform-id'); myform.submit = function () { results=myForm.validator.validate();

If results return true then there is no error.

How to display Magento CMS blocks in template files programmatically

There are a lot of reasons you might want to call cms blocks programmatically in magento inside your template or inside your own plugin. Particularly cms blocks are very useful to allow admin access to html from control panel avoiding the need to change theme files.  So, how do you display a cms block inside a theme phtml file?

Here is how to get the CMS helper:


$cmsHelper = Mage::helper('cms');

Now to get the CMS block by id, you ll need to do this


$block = Mage::getModel('cms/block')->load("your CMS block id here");

It is better to check if the CMS block is active by:


if ($block->getIsActive()){

To get CMS block title:


$block->getTitle()

To get CMS block content/description:


$block->getContent()

However when you echo description directly you might notice it contains unprocessed magento path variables, therefore you’ll need to convert those to what they mean using Magento’s own method getPageTemplateProcessor


$processor = $cmsHelper->getPageTemplateProcessor(); $block_content=$processor->filter($block->getContent());

Here is a complete example that get the content and title of a CMS block, checking if it is active and process its variables


$cmsHelper = Mage::helper('cms'); $processor = $cmsHelper->getPageTemplateProcessor(); $block = Mage::getModel('cms/block')->load("my_cms_block_id"); if ($block->getIsActive()){ echo "Title:".$block->getTitle()."
"; echo "Content:".$processor->filter($block->getContent())."
"; }

Magento: How to hide products that have no images

If you would like to hide products without images in your Magento store product list page, this is one way to do it. edit app/design/frontend/[YOUR CURRENT TEMPLATE]/[YOUR CURRENT THEME]/template/catalog/list.phtml (e.g. app/design/frontend/default/default/template/catalog/list.phtml)

Change :


$_productCollection=$this->getLoadedProductCollection();

to


$_productCollection = clone $this->getLoadedProductCollection(); $_productCollection->clear() ->addAttributeToFilter('image', array('neq' => 'no_selection')) ->load();

Since the collection we get from getLoadedProductCollection was already fetched from database, we cannot add attribute filters to it, that’s why it wouldn’t work without the clone. So we need to clone it then clear it. then add the attribute filter.

If you are not familiar with neq, neq means not equal.

Magento: How to calculate tax programmatically

This is how to calculate tax on a given price and product programmatically in Magento.


$priceInclTax=Mage::helper('tax')->getPrice($product,$price);

You can get $price from $product using


$price=$product->getFinalPrice();

You can get price the way you want, it can be custom price or any number you want to calculate tax on, but you need to pass the $product which should be a valid product object.