Wednesday, 24 June 2015

Magento get product custom attribute value by attribute code

Wrong way to do it

In many Magento template files, $_product, which is an instance of Mage_Catalog_Model_Product, can be found and used to obtain the product details. In some places, we can get custom attribute value by $_product->getMyCustomAttribute(), but we will get NULL if $_production is in the loop of a product collection, which is not what we expected.

Right way to do it

If $_product is in the loop of a product collection, we would have to load the product manually and then get the attribute value as below:
Mage::getModel('catalog/product')->load($_product->getId())->getMyCustomAttribute()

If $_product is obtained by Mage::getModel('catalog/product')->load($_product->getId()), we can absolutely get the attribute value by $_product->getMyCustomAttribute()

Wednesday, 17 June 2015

How to re-install magento

Why do I need to re-install magento

For some reason, when I download the Magento from a server and overrode my local magento, I couldn't log in magento. What I assumed is that the salt key were different or some other reasons. So I decided to re-install magento on my local.

It's very easy to re-install magento by following the steps below:

First, delete the app/etc/local.xml which contains all of the configurations made by installation.

Second, open http://yourdomain/install.php in the browser.

And then follow the installing steps of magento, but pay attention to the database name that needs to be input when installing. You need a new database which is different from the old magento database or you need to delete the old database if you want to use the same database name.

Potential Reasons for not being able to access admin

I'm using WAMP and magento seems not working well with WAMP directory alias and it only works well with vhost. 
Magento store the domain name in the database, after I transferred to vhost from directory alias, it kept redirecting me to localhost/mydomain even if I typed the vhost mydomain.loc.com.

Thursday, 11 June 2015

How to set product display/sort order

First level of the configuration of product display order

Go to System->Configuration->Catalog and click frontend on the right panel. Set the Product Listing Sort By select to one of the options comprised of 'best value' which is the same as 'position', 'name' and 'price'.

Second level of the set up

Go to Catalog->Manage Categories and click Display Setting on the right pane. You can configure both Available Product Listing Sort By, which will affect the sort-by options from which the customer can choose on the front-end, and Default Product Listing Sort By, which will be the default option on the front-end page.

Set the Position value to manually tweak the product order

Go to Catalog->Manage Categories and click Category Products on the right pane. On the following page, you can change the position of any product that belongs to to the category.
To improve Magento performance, it's better to search the products that need to be sorted and then change and save the position values for them. Note: This is a very important tip for large data website.

Wednesday, 10 June 2015

Add a responsive banner slider

As most of the websites are having a banner slider, it's a required web skill of adding a responsive slider. Here I will write down how to get a slider for Magento by using some slider extensions

First, I'm going to describe how to use Magento Responsive Banner Slider extension. 

Download the extension from the URL: http://www.magentocommerce.com/magento-connect/responsive-banner-slider.html
Install it from Magento Connect Manager if your Magento version is before 1.9. Otherwise, you need to manually download the module and copy the the files to your Magento folder. Cause there is an installation bug in this extension for Magento 1.9.
Go to System->Configuration->MKS Responsive Banner and click the general settings of the extension. Fill out the banner settings form which suits your website.
Go to Responsivebannerslider->Manage Responsivebannerslider. You will be led to a page where the images of the slider can be uploaded and set and you can add as many images as you want for the slider.
Put the below block code in the CMS pages  or any template files where you want to add the slider.
{{block type="responsivebannerslider/index" name="responsivebannerslider_index" template="responsivebannerslider/index.phtml"}}


Friday, 5 June 2015

Solution of Magento Common Errors

CMS: Upload Image icon doesn't show up when clicking Insert a Image icon

Since uploading image needs the permission of Media Gallery, so we need to add this permission to the user which was used to manage CMS system in order for the user to have the ability to upload and insert images.

CMS: The directory is not writable by the server

The error might occur when trying to upload images in CMS. 
The reason is that CMS is trying to upload images to media/wysiwyg folder, but the folder was not created when Magento installed. Therefore, what we need to do is just to create the folder and make sure nginx/php user has the permission of it.

Magento Connect: 404 Not found

I got some answers from the internet which were all about the permission of downloader/index.php and some folders. You can clear cache using magento-clear-cache.php.
However, the cause of my issue is that the configuration of Ngninx. In my configuration I didn't rewrite dowloader/ to dowloader/index.php which might be a common mistake if nginx is used as PHP server, because .htaccess file which is doing the rewrite in Magento won't take effect under nginx.
To figure out whether this is the one causing your 404, you just need to put index.php manually after downloader URL to see whether it will work or not. If it works, then you have the same issue as mine.
To solve the issue, you just need to adjust the nginx configuration to make the rewrite also worked for downloader folder.


Thursday, 4 June 2015

Two ways to set up the default store and defaute store view

The easiest and well known of setting the default store and the store views is setting them through Magento Admin Panel

Go into System->Manage Stores.
Click the website for which you want to set up the default store and you will get the setting page where the default store can be configured.
Click the store for which you want to set up the default store view and you can set it up on the page you get.

There is another way we have to know which may cause the first way ineffective without any obvious clues to solve the issue

In the index.php file, there are two lines related to the default store and store view as shown below:

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

This means we can set up the variable MAGE_RUN_CODE and MAGE_RUN_TYPE in Apache/Nginx configuration files or PHP configuration file. Here is a sample configuration snippet in nginx configuration file:

fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param  MAGE_RUN_TYPE store;

Note: remember to check whether the configuration of the second way exists when you find that the default store/store view settings in Admin Panel don't work

Magento Re-index from shell which is super-useful for large data store

Reason of re-indexing from shell

Magento do provide the functionality of re-indexing in admin pane, but it might cause some timeout failure, which may brings a somewhat failure of re-indexing procedure, because of php execution limit and nginx/apache timeout. However, the shell script can avoid the timeout and execution limit, so it's better to re-index from shell for large data stores.

How to re-index from shell

Log on the website server and go into the Magento root directory.
Run the command of php shell/indexer.php help which will show the manual of using the script.

Here are some useful commands provided by the script

Get a list of indexers: php shell/indexer.php info
Get the status of current indexes: php shell/indexer.php --status
Re-index all: php shell/indexer.php reindexall #which is not recommended especially for large data website
Run one of the indexers: php shell/indexer.php --reindex indexer #indexer is coming from the list of indexers

Magento index modes

There are two Magento index modes. The first one is 'Update on save' which means the index will be rebuilt whenever the change related to the index has been made. While the other one is 'Manual Update' which makes the re-indexing executed only when the re-indexing command is ran manually or it's operated in the admin panel.
To conclude, it's highly recommended to set the mode to 'Manually update' for large data stores.

Set up Magento index modes

Set the mode to 'Update on save': php shell/indexer.php --mode-realtime indexer #indexer is from the index list
Set the mode to 'Manually save': php shell/indexer.php --mode-manual indexer #indexer is from the index list
After it's set to 'Manually save', the re-indexing command need to be added in the Linux cron table  to make rebuilding the index took place at a specific time which is supposed to be an idle time.
Note: the index mode should be set to 'Manually save' when doing any importation or it will be very slow for the importing process.

What if the re-indexing process is stuck for some reason

Restart nginx and php-fpm to stop the process. 
Go into the root directory of Magento and check the locks under the folder of var/locks/. And delete the locks if you want to stop the stuck process.
Go into Mysql and  check the table named index_process. And set the status from working to pending if you want to stop the stuck process.
Use indexer script to check the status of indexers to make sure there is no running indexers.
Run the indexer from shell instead of from the admin panel.

What are the statuses of indexers and their meanings

In Magento there are three statuses of indexers, which are quite confusing from the names,  comprising 'pending', 'working' and 'require reindex'.
'pending' means the index has been rebuilt and there is no need to re-index it.
'working' indicates the re-indexing process of the index is running.
'require reindex' shows the index is out of date and it needs to be rebuilt.