rss
twitter
    Find out what I'm doing, Follow Me :)

Tuesday, April 27, 2010

Creating Block From the Code in Magento


Here are some methods of creating block by code in Magento. This type of code can be used directly in template, not need to create a layout file or add a child node inside already existing layout file. You can use this to echo a different type of block inside already loading block and template.

// Directly in the PHTML where you want to show the block
<?php
echo $this->getLayout()->createBlock("module/block")->setTemplate("module/page.phtml")->toHtml();
?>

What we have actually done is just load the layout and create an instance of a block class defined inside its paramerter, then assigned a template to the block and finally echoed as html. Simple! The code above will be equivalent to what we do as following in layout and in template file.
// In layout
<block type="module/block" name="module.name" template="module/page.html"/>
<?php
// In parent PHTML
$this->getChildHtml("module.name");
?>

Monday, April 26, 2010

How To install Magento In Wamp Server


Magento is an open-source ecommerce web application launched on March 31, 2008. It was created by Varien, building on components of the Zend Framework.
Magento is available under the Open Software License version 3.0. Since version 1.1.7 some parts are licensed under the Academic Free License version 3.0.

To Download Magento In Wamp, Follow below steps.

1. Download latest stable version of Magento from http://www.magentocommerce.com/download (Requires free registration), Unzip in www directory and Also create Mysql database and User from your phpmyadmin panel, which can be accessed by http://localhost/phpmyadmin.
2. Now start installing Magento by http://localhost/magento. If not accessible, please check the folder name of your folder.
3. Now Installation Wizard will be start, Step 2 is to enter your mysql details. It will take a while as it creates some tables.
4. Next Step will ask you about Admin login/Pass, So Create your admin/pass and save them.
Well, thats all. Go Open your store and Enjoy

Friday, April 16, 2010

Magento Commerce 1.4: How To Fix ‘Unable to submit your request. Please, try again later’ on the Contact Us form

If you’ve upgraded your Magento Commerce site to 1.4.x you may have run in to the problem where any attempt to send a response via your ‘Contact Us’ page fails with 
“Unable to submit your request.  Please try again later”.

Contact Us Form - Error: Unable to submit your request, please try again later



If you look at the code /app/code/core/Mage/Contacts/controllers/IndexController.php

public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);

                $error = false;

                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;
                }
        
                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }

                if ($error) {
                    throw new Exception();
                }

      (...snip...)

            } catch (Exception $e) {
                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }

The Contact Us form requires the Name, Comment, Email not be empty or NULL.  We do expect the “hideit” field to (a) exist and (b) be empty/NULL.  The “hideit” field is new in 1.4 and is designed to help prevent spam.  Automated bots would complete this field and this is how we can detect bots.  It’s best to implement a Captcha system as well on your Contact Us page to further improve your spam protection.

If any of the above checks fail, the function throws an exception and we get the “Unable to submit your request. Please, try again later” message.  The message would be more descriptive if it read “Unable to submit your request, please verify all required fields have been completed”.  You can change the message in the locale translation file if you want.  This will also future proof you against updates.

The best thing to do is examine the code on your site.  Those site’s I’ve looked at have the following:

<div class="button-set">
        <p class="required">* Required Fields</p>
        <button type="submit" class="form-button"><span>Submit</span></button>
    </div>

The correct code should be:

<div class="buttons-set">
        <p class="required">* Required Fields</p>
        <input type="text" style="display: none ! important;" value="" id="hideit" name="hideit">
        <button class="button" title="Submit" type="submit"><span><span>Submit</span></span></button>
    </div>

Either something went wrong with the upgrade and the underlying files didn’t get upgraded, or you’re overriding the files within your theme directory.

The Contact Us form is generated by /app/design/frontend/base/default/template/contacts/form.phtml.  The code from a working 1.4.0.1 environment looks like this:

<div class="buttons-set">
        <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
        <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
        <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
    </div>

Remember to backup any files you edit, before editing

You can choose either to edit the file directly or better yet, replace it with the correct 1.4.0.1 file.

Thursday, April 15, 2010

Upgrading To Magento 1.4

With the recent release of Magento version 1.4, many people are having trouble upgrading their current Magento installation, or installing their template on a fresh Magento 1.4 installation. A quick search online and you’ll find countless entries and posts stating that Magento 1.4 is buggy, not working, horrible, etc. Particularly when you browse through the Magento forums.

In fact Magento 1.4 is different than the last previous stable version (1.3.2.4) but you should definitely be able to upgrade your installation, or install your custom template on a Magento 1.4 installation! Here are the most common issues and how to fix them!

Magento modern theme 1.4

This relates to the following issues:

    * Can’t install Modern Theme through Magento Connect
          o Get error “magento-core/Interface_Frontend_Default_Modern requires package “magento-core/Mage_Core_Modules” (version >= 1.3.2.2, version <= 1.3.3, excluded versions: 1.3.3), installed version is 1.4.0.0
            Install Errors
            No valid packages found
            PEAR ERROR: install failed
    * Can’t upgrade Modern Theme through Magento Connect
          o Get similar error as above
    * etc

As of now, there is no good way to install or upgrade the Modern Theme via Magento Connect. BUT you should definitely be able to get it by installing a fresh Magento 1.4 installation.

Magento 1.4 comes by default with the default theme, the modern theme, and the iphone optimized theme. However, you’d have to install Magento manually and NOT use the downloader (as this will include the default theme only). Here is one way to install Magento 1.4 manually:

   1. Download all the files on your computer and upload them on your server
   2. Create a database and a database user and grant that user all permissions to the database. Make sure to write down your database name, database user name, and database user password.
   3. Go to the URL that points to your server and follow the instructions to complete Magento installation

Can’t access admin, blank page, products won’t load, error, etc

Most issues will be resolved once you reindex Magento data and refresh all Magento cache. To reindex Magento data follow these quick steps:

   1. Go to System>Index Management
   2. Click on “select all” in the upper left corner
   3. Under “Action” select “Reindex Data” and click submit

To refresh all Magento cache following these quick steps:

   1. Go to System>Configuration and
   2. Click on “select all” in the upper left corner
   3. Under “action” click on “refresh” and click submit
   4. Then click on these buttons:
         1. Flush Magento Cache
         2. Flush Cache Storage
         3. Fluch Catalog Images Cache
         4. Flush JavaScript/CSS Cache

Upgrading your existing theme (modern or else)

Obviously I cannot cover here all the possible problems and issues that may arise when upgrading your particular template to be compatible with Magento 1.4. However, I found that the most common issue is “Categories not working after upgrading to 1.4″. This relates to the error people get when clicking on some categories: “Error processing your request” followed by a number.

To fix that, you’ll need to do 2 things:

1. First, edit your toolbar.phtml file located under app/design/frontend/default/yourthemename/template/catalog/product/list. Take the updated toolbar.phtml from the new 1.4 default theme (located under app/design/frontend/base/default/template/catalog/product/list) and use it in your theme. Then edit that file to apply any styling you had in your original toolbar.phtml file. Another way of doing it is editing your toolbar.phml file to use the right class names and code (you can compare with the default one and take it from there).

2. Second, open catalog.xml located under app/design/frontend/default/yourthemename/layout/ and find this line:

<block type=”catalog/product_list_toolbar” name=”product_list_toolbar” template=”catalog/product/list/toolbar.phtml”>

And underneath insert this code:

<block type=”page/html_pager” name=”product_list_toolbar_pager”/>

And that should do it!

How to show error description in Magento 1.4

Simply go to your magento installation folder, locate the errors folder and you will find local.xml.sample, simply duplicate the file and rename it local.xml

Sunday, April 11, 2010

Magento 1.4 cronjobs

On some points, Magento needs regular maintenance. For instance, when running a shop of which the products are updated frequently, it is needed to refresh catalog indices. Also the search-index might need regular updating. To accomplish these tasks you can run Magento cronjobs. With MagentoTM 1.4 this is even easier. Here's how.

Meet cron

Within Linux environments, the program responsible for running scheduled tasks is called cron. Tasks that are run through cron are referred to as cronjobs. By runnning cronjobs daily or perhaps weekly, you can automate certain processes like refreshing the Magento indices or cleaning up logfiles.

Running cronjobs is something that depends on your hosting environment. Almost every Linux-server is shipped with cron-functionality, but within shared hosting environments it just depends whether you are allowed to use or not. Popular control panels like DirectAdmin or CPanel often allow you to create new cronjobs.

Running a Magento 1.4 job through cron

With Magento 1.3 and older, you had to write your own PHP-scripts to get things done. With Magento 1.4 this has become much easier. Within the Magento directory "shell" you can find various PHP-scripts that automate certain actions. The rest of this tutorial will discuss those actions as well.

Running such a script through cron doesn't mean calling this script through the browser. You'll need command-line access to your Magento site to run such a script manually. In exactly the same way that you run this from the command-line, the cronjob is also run. In the following examples, you will need to replace "/path/to/magento" with the exact directory that contains Magento.

php /path/to/magento/shell/indexer.php reindexall

This will reindex all the Magento indices. Now this command first calls the PHP-program "php" with as first argument the location of the PHP-script, and as second argument something that is used as argument for the script.

It might be that the "php" program is not found by cron, in which case you will need to define the exact path to the "php" binary program:

/usr/local/bin/php /path/to/magento/shell/indexer.php reindexall

To surpress output from the Magento script you can also add the flag "-q" (quiet):

php -q /path/to/magento/shell/indexer.php reindexall

More Magento reindexing

When running the indexer.php script from the command-line we can find more tricks. First of all we can check for the current status of all the indices:

php /path/to/magento/shell/indexer.php --status

We can also refresh a specific index. To do so, we first need to find out which arguments can be used:

php /path/to/magento/shell/indexer.php info

This gives a listing like the following:

catalog_product_attribute     Product Attributes
catalog_product_price         Product Prices
catalog_url                   Catalog Url Rewrites
catalog_product_flat          Product Flat Data
catalog_category_flat         Category Flat Data
catalog_category_product      Category Products
catalogsearch_fulltext        Catalog Search Index
cataloginventory_stock        Stock status

Now we found the technical names for the various indices, which we can use like the following example:

php /path/to/magento/shell/indexer.php --reindex catalog_product_flat

The last example is again something we can use through cron.
Clean logs and using the Magento compiler

Besides refreshing indices, we can also clean up all the logs within the database:

php /path/to/magento/shell/log.php clean

Even cooler is the new Magento Compiler module which is still in beta but gives great performance benefit by reducing the number of include-paths (4 by default) to one single include-path. This mainly saves CPU-resources.

You can check for the current state of the compiler by running the following:

php /path/to/magento/shell/compiler.php state

Once checked you can disable the compiler temporarily, rebuild the include-paths again with the flag "compile" and enable the compiler once more.

php /path/to/magento/shell/compiler.php disable
php /path/to/magento/shell/compiler.php compile
php /path/to/magento/shell/compiler.php enable

Thursday, April 1, 2010

How to link pages using store URL short code

If you want to add hyperlinks between the pages on your site you could specify the full http://www.yourdomain/…, but Magento provides a short code which automagically prefixes your base store URL.  If you have a CMS page with the slug “about-us”, you can link to that page from any other CMS page

using:

<a href="{{store url='about-us'}}">About Us</a>

If you wanted to link to your home page use:

<a href="{{store url=''}}">Home Page</a>