PHP
Upgrading Magento 1.5.0.1 to 1.6.2.0
Feb 2nd
Upgrading Magento is always scary – kind of like flashing your BIOS on a PC. It is always important to backup your entire file system and magento database prior to upgrading to the most recent version to avoid catastrophic results.
This tutorial is for those of you who are at whits end and you just want to curl up and die in a hole because you attempted to upgrade magento and you encountered errors like:
1 | Error in file: "app/code/core/Mage/Sales/sql/sales_setup/mysql4-upgrade-1.5.9.9-1.6.0.0.php" - Column "base_shipping_hidden_tax_amount" does not exists on table "sales_flat_creditmemo" |
1 | Error in file: "/app/code/core/Mage/Customer/sql/customer_setup/mysql4-upgrade-1.5.9.9-1.6.0.0.php" - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0-918' for key 'UNQ_BY_CUSTOMER' |
Not only are these errors annoying but magento has a very unhelpful error system that often gives cryptic errors or it stores them in random files in the /var/reports directory.
Before you begin any of this, be sure to back up your entire file system.
Fixing Integrity constraint violation: 1062 Duplicate entry ’0-918′ for key ‘UNQ_BY_CUSTOMER’
This fix actually isn’t too bad as you might think. Even if you have no coding experience or experience connecting to your server via FTP, this will be easy.
If you are not familiar with connecting to a server via FTP, we have a lovely tutorial explaining how to do this. If you are familiar then please continue reading!
Navigate to the root of your magento installation and find the app directory. Once there, enter the app directory and then enter the etc directory. You will see a few files (see image).

Open the config.xml file in your favorite text editor (we recommend using notepad++ for ease of use for those of you who do not have an IDE). Around line 41 you will see the following:
1 | <initStatements>SET NAMES utf8</initStatements> |
Comment out this line like the following:
1 | <!-- <initStatements>SET NAMES utf8</initStatements> --> |
And then add this line below the commented out code
1 | <initstatements>SET NAMES utf8; SET FOREIGN_KEY_CHECKS=0; SET UNIQUE_CHECKS=0;</initstatements> |
Now that this issue is resolved, try to load your magento site again. Your site is working again! But there may be a greater issue here so keep reading…
Fixing Column “base_shipping_hidden_tax_amount” does not exists on table “sales_flat_creditmemo” or other MySQL Related Errors
If you have the error above or you have other general MySQL errors in magento then there still is hope.
Step 1
Navigate to the /var/cache directory of your magento installation and delete all of the cache.
Navigate to the /var/sessions directory of your magento installation and delete all of the sessions.
Step 2
Go to this the magento website or download the magento database repair tool directly. Unzip this file and you will see a php file.
Place the php file in the root directory of your magento installation and continue to the next step.
Step 3
Install a fresh copy of Magento CE 1.6.2.0 or you can simply download this sample database for reference if you would like to avoid having to install a fresh copy of magento.
Import the fresh database via phpMyAdmin (or by using your favorite database managment tool) and wait until it is fully completed with NO errors! Remote databases are OK.
Note: if you are accessing your database via ssh then see here:
To export an entire database:
1 | mysqldump -u username -p database_name > database_name.sql |
To import an entire database:
1 | mysql -u username -p database_name < database_name.sql |
Step 4
Now that you have a copy of the database you can upgrade with and that we can reference, it is time to finally repair this database and get it working! We need to download the Magento Database Repair Tool.
Step 5
Extract the file anywhere within your file system (the document root directory will suffice) and then type in your web browser: http://yourdomain.com/magento-db-repair-tool-1.0.php and you will see this page:

Note: this script doesn’t care where the databases are located. You can connect to a remote database or a database that is on your localhost. Just ensure you enable access to your database remotely via your hosting control panel.
Type in the required information (be sure to include any and all prefixes you have) and then click continue. This script will match the corrupted database to the good database and repair any incorrect SQL table structures and repair any important data in those tables. Things like usernames, password, products will NOT be affected so do not worry!
When this process is complete you will see the following:

Final Step
Try entering in the URL of your magento installation to ensure everything worked correctly. If it did not then restore your file system and database and make sure you went through the motions correctly. Be sure to document and research any errors on the magento forums and see if anyone else has encountered/solved these errors!
But remember that line of code we replaced?
1 | <initStatements>SET NAMES utf8</initStatements> |
Be sure to replace our edited line of XML with the original as this is a security risk!
Hope this helps all those out there that are struggling with this!
Setting up Magento in XAMPP
Jan 30th
Some of you may be banging your head against a wall trying to figure out how to setup Magento in your local XAMPP installation. Fret not aggravated peoples, we have found the answer!
As you may know, Magento is a popular e-commerce platform that can handle anything from small business to enterprise e-commerce solutions. Despite it’s rather large size and not-very-optimized code, the community edition of Magento is notorious for having excessively large databases and a single .log file that is around 3GB after awhile (which is what we encountered)…
However, if you are installing a FRESH COMMUNITY EDITION (version 1.6.2.0 at the time of this tutorial’s birth), then we will go through the steps with you one by one!
The Process
Herein lies the process for installing magento:
Step One
Download the most recent community edition of magento!
Step Two
Go to http://localhost/phpmyadmin/ and add a new database!

Step Three
Extract the magento files in C:\xampp\htdocs\magento\ for Windows
OR
/Applications/XAMPP/htdocs/ on Mac
Note: the above directories are the default directories and may not necessarily be the places where you installed xampp. If this is different, then extract your magento files into the ApacheDoc document root “htdocs” folder of your xampp installation.
Step Four
Next we should update our hosts and get it out of the way now. Bear in mind, the hosts file maps domain names to ip addresses. When you type a domain into your browser window, your browser will check to see if there is a matching entry for that domain name in your host file.
On Windows navigate to C:\windows\system32\drivers\etc right click on your hosts file and select edit. You will see a bunch of random numbers which are ip addresses.
Add:
1 | 127.0.0.1 magento.localhost.com |
and save the file.
Press the window key + r (or simply search for and/or open cmd.exe) and type the following:
1 | ipconfig /flushdns |
Editing the Hosts File on Mac
Open Terminal and type the following (after each command press return):
We need to open the hosts file for editing
1 | $ sudo nano /private/etc/hosts |
Add this to the bottom of the hosts file
1 | 127.0.0.1 magento.localhost.com |
When done editing the hosts file, press control-o to save the file.
Press enter on the filename prompt, and control-x to exit the editor.
Now we must flush the DNS cache on our system!
1 | $ dscacheutil -flushcache |
Step Five
Assuming all went well, you are now ready to open your favorite web browser and type in magento.localhost.com/magento/ and you will be redirected to the magento installation screen!
Some options to you should choose when configuring your database information:
- Skip URL Validation
- Use Apache Server Rewrites
As you process through the installation process, please be patient. Do NOT click multiple times on any field especially the page where you enter in database information. Magento has a very extensive database and therefore it takes time to install and add all of the tables and populate those tables with relevant information.
Step Six
That’s it! You should have a fully functioning Magento application on your XAMPP installation!
Fixing T_PAAMAYIM_NEKUDOTAYIM with Clever CMS in Magento 1.6
Jan 27th
After a recent upgrade to Magento 1.6.2.0 and using Clever CMS, we realized there was an issue on our frontend! We saw a weird error we had never seen before with an older version of Clever CMS.
1 | Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /home/<em>your_account</em>/public_html/app/code/community/Clever/Cms/Model/Page.php on line 101 |
Some of you might be wondering what in the world who/what T_PAAMAYIM_NEKUDOTAYIM actually is. Aside from being an influential programmer, a T_PAAMAYIM_NEKUDOTAYIM is the double colon system that PHP uses to call a parent class’s static field, static constant, static variable, or static method. Thankfully, resolving this error is easy to fix.
Step 1
Open your favorite text editor or log into your web host via terminal and go to public_html/app/code/community/Clever/Cms/Model/Page.php. Open the file with your text editor or a edit or vim command in shell. Go to line 101 or find the method called getChildren.
1 2 3 4 5 6 7 8 9 10 11 | public function getChildren() { $collection = $this->getCollection(); // $collection->setOrder('position', $collection::SORT_ORDER_ASC); $collection->setOrder('position', $collection->SORT_ORDER_ASC); $collection->getSelect() ->where('main_table.store_id = ?', $this->getStoreId()) ->where('main_table.parent_id = ?', $this->getId()); return $collection; } |
Notice the line commented out with $collection::SORT_ORDER_ASC. We need to change the :: to a -> because $collection is an object calling a static constantin the Mage_Cms_Model_Page class. In fact, if we look in Magento 1.6.2.0 we’ll see that this static constant doesn’t even exist in this class or it’s parent class (Mage_Core_Model_Abstract)!
That should fix anyone’s issue with this error!
“How did you hear about us?” in Magento 1.5
Jun 23rd
One of our most recent projects required us to get a grip on the Magento e-commerce platform. The client was request a “How did you hear about us” feature on the Magento onepage checkout form. Rather than spend the time to learn exactly how Magento is setup and learn a new flavor of PHP, we figured a module would be best. Luckily, we found free module that did the trick!
Inchoo is the company that created this module. The module essentially places a drop down option by default under it’s own tab in the Magento onepage checkout.
Here is an example of the default usage of this module:

In order to get this module to work properly in Magento 1.5, we need to edit some files.
Configuration
First, we need to download the extension and unzip the files. Open your favorite FTP client and drag and drop the files into the root directory of your Magento 1.5 installation.
Once the extension is in place, log into the admin side of Magento and navigate to System > Configuration > Design > Admin Theme. We need to enter in custom into the admin theme field and save.
Once that is done, we need to dig around in the files to make this extension compatible with our English-speaking users and allow the extension to actually work!.
Our first matter of business is we need to change the text which appears on the onepage checkout form. Go to app/code/local/Inchoo/Heared4us/Block/Onepage/heard4us.phtml
Change this:
1 2 3 4 | $this->getCheckout()->setStepData('heared4us', array( 'label' => Mage::helper('checkout')->__('Where did you heared for us'), 'is_show' => true )); |
To this:
1 2 3 4 | $this->getCheckout()->setStepData('heared4us', array( 'label' => Mage::helper('checkout')->__('How did you hear about us?'), 'is_show' => true )); |
Next, we want to alter the text in the admin view to display correctly. Open app/design/adminhtml/default/custom/template/sales/order/view/info.phtml
Around line 134 (if you have line numbers, if not then search for “Where did you heared about us”) we want to replace this:
1 | <h4 class="icon-head head-heared4us"><?php echo Mage::helper('sales')->__('Where did you heared about us') ?></h4> |
With this:
1 | <h4 class="icon-head head-heared4us"><?php echo Mage::helper('sales')->__('How did you hear about us?') ?></h4> |
The code above will change the text on the checkout form with proper English syntax and grammar…
Open your favorite IDE or text-editing program (notpad or textedit) so we can edit this code.
Note: if you don’t have access to edit the files before you place them on the server then edit the files on your local system and then upload them to the server.
One of the first lines of PHP code we will see is the following:
1 2 3 4 5 6 7 8 9 10 | <?php $_options = array( $this->__('Unterwasserwelt.de'), $this->__('Sea Star Online'), $this->__('Sea Star Magazine'), $this->__('Google'), $this->__('YouTube'), $this->__('Referral') ); ?> |
This block of code controls the items that are in our drop down menu on the onepage checkout form. If you would like to use this drop down, the continue on reading how to customize it. If you would like to use a text field, then click here
Editing these drop down items is very easy. All we have to do is replace the text between the ‘ and ‘ inside of the parenthesis. For example:
1 2 3 4 5 6 7 8 9 10 11 | <?php $_options = array( $this->__('A Friend'), $this->__('Twitter'), $this->__('Facebook'), $this->__('Google'), $this->__('YouTube'), $this->__('Referral'), $this->__('Saw it in a Store') ); ?> |
We edited the text to suit our needs and we even added an option to the drop down. It’s important that each item in the array have a comma after it with the exception of the last item in the array. Be sure the structure is always as follows:
1 2 3 4 5 6 | $_options = array( $this->__('Item 1'), //first item, $this->__('Item 2'), //second item, many more items, $this->__('Item 100), //100th item, $this->__('Last Item') //final item (no comma after this) ); |
If you want a text field where the user can enter in their information just like this:
Then we only need a few minor tweaks before we can achieve this feature!
First, we need to comment out the array which contains our drop down menu options.
1 2 3 4 5 6 7 8 9 10 11 | <?php /*$_options = array( $this->__('Unterwasserwelt.de'), $this->__('Sea Star Online'), $this->__('Sea Star Magazine'), $this->__('Google'), $this->__('YouTube'), $this->__('Referral') );*/ $_options = ''; ?> |
Next, we want to comment out the drop down menu itself:
1 2 3 4 5 6 7 8 | <?php /* <select id="getvoice" name="getvoice"> <?php foreach($_options as $_option): ?> <option value="<?php echo $_option ?>"><?php echo $_option ?></option> <?php endforeach; ?> <option id="getvoice-other" value="<?php echo $this->__('Other') ?>"><?php echo $this->__('Other') ?></option> </select> */ ?> |
Finally, we want to put in our text input field, here is an example:
1 2 3 4 | <div class="field"> <div class="field-label"><label for="getvoice">How did you hear about us?<em>*</em></label></div> <div class="field-field"><input type="text" name="getvoice" id="getvoice" /></div> </div> |
Note: there are extra div’s in here that were created for formatting purposes. Here is the css that wwas used to style this field. If you decide to use this css, be sure to put it in /skin/frontend/default/yourtheme/css/skin.css
1 2 3 | #checkoutSteps #opc-heared4us .field {width:100%;} #checkoutSteps #opc-heared4us .field .field-label {font-weight:bold; color:#00c4ff;} #checkoutSteps #opc-heared4us .field .field-label em {color:red;} |
Typically, it’s good just to keep the variable set to an empty string, you never know if it is being used by something else and an error will occur.
Getting the Values to show in the Admin Area
There are few things we must do in order to enable either of options or the user input value to show in the admin side. We must navigate to app/code/local/Inchoo/Heared4us/sql/mysql4-install-0.1.0.php and open the file with your favorite IDE of text editor.
Replace this line:
1 | $row = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchRow($sql); |
with this
1 | $row = Mage::getSingleton('core/resource')->getConnection('core_read')->addColumn(Mage::getSingleton('core/resource')->getTableName('sales/order'), 'giftaid', 'TEXT NULL'); |
Next, we need to add a new field to a table so we need database access with a tool like phpMyAdmin. If you do not have database then contact your administrator and request the following change.
1 | ALTER TABLE sales_flat_order ADD heared4us TEXT; |
If you do have database access with phpMyAdmin, select your magento database and click on the SQL field. Copy and paste the SQL above and then press “Go”. See the image for details.
The end result should be under each new order. You will see a “How did you hear about us?” field and the option/text the user typed in will show as follows:
PHP #6: Even More Control Structures
Nov 27th
So far we have learned about the following control structures:
- if, elseif, else
- while
- for
But there are still more. We’ll first look at the switch construct.
Switch
The syntax for a switch statement looks something like this:
1 2 3 4 5 6 7 8 9 10 | switch (expression) { case value1: //some code case value2: //some code case value3: //some code default: //some code } |
When this code is executed, each case is looked at in order. If(value1 == expression), then all code under that case and the code under all subsequent cases will be executed. If not, it will check if(value2 == expressions) and so on. If none of the cases match, the default case is used. You can have as many or as few cases as you want, and having the default case is optional. An important thing that is often forgotten is that all code underneath a true case is executed, even the code within the following cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $n = 3; switch($n){ case 0: print "$n is equal to 0\n"; case 1: print "$n is less than or equal to 1\n"; case 2: print "$n is less than or equal to 2\n"; case 3: print "$n is less than or equal to 3.\n"; case 4: print "$n is less than or equal to 4.\n"; case 5: print "$n is less than or equal to 5.\n"; } |
When the above code is executed, the following will be printed:
3 is less than or equal to 3. 3 is less than or equal to 4. 3 is less than or equal to 5.
Notice that once an appropriate case is found, all code in the following cases is executed as well. There is a way to prevent this though. Using the break; command you can jump to the closing brace of the switch statement preventing any more code from being executed.
Also, a default case will be used when no other cases match.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | switch ($n) { case 0: print "$n is zero."; break; case 1: case 4: case 9: print "$n is a perfect square."; break; case 2: print "$n is an even number."; case 3: case 5: case 7: print "$n is a prime number."; break; case 6: case 8: print "$n is an even number."; break; default: print "Only single-digit numbers are allowed."; break; } |
Test for Understanding
- What will print if $n is set to 7?
- What will print if $n is set to 1?
- What will print if $n is set to 20?
Answers:
- 7 is a prime number.
- 1 is a perfect square.
- Only single-digit numbers are allowed.






