Wednesday, September 30, 2015

Magento - Add new column (field) to credit memo items

If your extension wants to add a new field to each creditmemo item, then you should use this code in your extension's installer:


$installer->getConnection()->addColumn($installer->getTable('sales/creditmemo_item'), 'cmsubtotal', 'FLOAT  DEFAULT 0');


For example, this code will add cmsubtotal field to creditmemo_item table. Once this is added, it is enough to use setCmsubtotal($value) function at each $creditmemoItem , before saving the model, to persist the custom field data into database.

Tuesday, September 29, 2015

Debugging complex systems (such as Magento)

In modern day software, source code is separated in a huge number of blocks, This blocks are usually defined as classes. Debugging many problems comes down to finiding the class responsible for a particular action in particular point of time.

Case scenario:

You need to find out why your $x is always at value 0. You set its value in a model, but when you print it out , it is always zeroed.
This indicates that some other object must have altered the value of this variable.

Here's a neat way how to tell who has edited the variable:

1. Start at the end of process. Find the part of code which displays the variable with wrong value. Before displaying, add a line to print stack trace (in PHP its called debug_backtrace).

2. Find all the functions which were called prior to calling the display function. Go to the function you think that might have wrongly changed the variable value.

3. In that function , try to print out the variable value at the start, and at the end of the function. If they don't match, your problem is inside this function.


Magento 1.6-1.9 : Debugging one page checkout stalls

Recently, I have experienced a strange problem with Magento 1.6 checkout process.

The one page checkout would always hang after Billing details tab. It would never go to Shipping tab.

  • Apache error log had no errors.
  • Firebug had no JS errors.
  • var/log/exception.log and var/log/system.log were both empty


In Firebug, it was obvious that saveBilling() AJAX call was running as it should. However, getAdditional call, which follows saveBilling(), was acting strange. It was also returning HTTP status 200=OK, but the its response content was strange. Its response contained a list of update XML files.

After a few more thoughts, I remembered that I had modified code/core/Mage/core/Model/Layout/Update.php file. Specifically, I had modified function getFileLayoutUpdatesXml() to print out all loaded XML files, for debugging some layout update problems I had previously experienced. Obviously, I forgot about this, and it crashed my checkout process.

After commenting out this printout code, checkout instantly started working again. I decided to write about this as I think these problems occur often in practice: error logs are empty, but the application still malfunctions. This time, it was matter of wrong input data, returned by getAdditional(). This data contained no clues which Magento JS expected to move forward.

Monday, September 28, 2015

Magento - how to override core models

In order to override a Magento model, you should add something like this in your extension's config.xml:

 <sales>
                 
                            <rewrite>
                                  <order_creditmemo_total_subtotal>
                                      Gogi_Cmsubtotals_Model_Creditmemo_Newsubtotal
                       
                                 </order_creditmemo_total_subtotal>
                            </rewrite>
               
                   
           
          </sales>

This XML code will replace Subtotal model located in Mage/Sales/Model/Order/Creditmemo/Total folder, with Newsubtotal model located in app/code/local/Gogi/Cmsubtotals/Model/Creditmemo.


Saturday, September 26, 2015

OpenGL - SOIL_load_image() returns null ; image/texture not being loaded

If you have been working with SOIL library for loading OpenGL textures, and have troubles with SOIL_load_image() function, this might help.

SOIL_load_image() should return 1 if image (texture for example) was loaded correctly, and 0 if there was as an error in image loading.

If you get 0 as return value, you can check the actual error being generated by examing the result of SOIL_last_result().

Just place this code below the line where you use SOIL_load_image():


const char* errX = SOIL_last_result();

Stop the debugger at this line and check the contents of errX.

If you see it having "Unable to open file", please note that SOIL by default reads the image paths relative to current working directory.

And that is not always the same directory as the one where your .EXE resides.

In case of Visual Studio, you can change the working directory when debugging by going to :

Project Properties ->Debugging->Working Directory



Please note that this change might affect other parts of your code as well!



Thursday, September 24, 2015

Add custom block to new creditmemo in admin

Here is a block class which adds a new test row to create new credit memo screen in Magento 1.6.

It is important to use
            $this->getParentBlock()
instead of just
            $this



class Gogi_Cmsubtotals_Block_Cmsubtotal extends Mage_Sales_Block_Order_Totals
{
    protected $_code = 'credit';

    public function initTotals()
    {

        parent::initTotals();
     


       
            $this->getParentBlock()->addTotal(
                new Varien_Object(
                    array(
                        'code' => "cmsubtotal",
                        'value' => 14,
                        'base_value' => 14,
                        'label' => "CM SUBTOTAL",
                    )
                ));
       
        return $this;
    }

 

}

Magento - block not loading (2)

Magento is very case sensitive! While developing your modules / extensions, the best practice to use is to use only the first capital letter in your extension or company name.


Example of bad practice:
GogiProductions_CMSubtotal

Example of good practice:
Gogi_Cmsubtotal

The reason is in the way Magento parses the filenames. It interprets each capital letter as a new folder!

Also, make sure you reference your blocks in the way you choosed in config.xml file.

For example, if this is your config.xml (part of it):



             <blocks>
                  <cmsubtotals>
                        <class>Gogi_Cmsubtotals_Block</class>
                  </cmsubtotals>
               
             
            </blocks>


Then , in your layout.xml ([yourextensionname].xml), you should reference your blocks like this:



    <adminhtml_sales_order_creditmemo_new>
     
 
            <reference name="creditmemo_totals">
                <block type="cmsubtotals/cmsubtotal" name="cmsubtotal" template="cmsubtotals/cmsubtotals.phtml"/>
            </reference>
     

    </adminhtml_sales_order_creditmemo_new>

Pleaase note we use block type with reference to the setting in our config.xml.

Magento - block not displaying / loading

If your custom Magento block is not being loaded by Mage, you can check your exception log for possible troubles while trying to load your block.

Exception log is located at var/log folder.

It is important to note that block not loading is a type of silent error - one very difficult to debug. The block is not being loaded, not being outputed to HTML, and no error is displayed in front end.

So, exception log is your friend here - it can help you to find and eliminate root cause of the error preventing your block from showing up in your Magento store.

Magento - your extension's setup sql file is not being executed

This blog entry is relevant for  Magento 1.6.

If you are writing a Magento extension which needs to alter Magento database, and you don't see the db being changed after your logout / login to your admin panel, then make sure these settings are correct:

1. Your sql / [folder] needs to match [folder] inside config.xml file of your extensions
Example:

If your config is like this:

            <resources>
                    <cmsubtotals_setup>
                        <setup>
                            <module>GogiProductions_CMSubtotals</module>
                        </setup>
                    </cmsubtotals_setup>
             </resources>  

Then you should have your sql setup files in folder:
sql / cmsubtotals_setup

2. Your extension version needs to be exactly the same in both your config.xml and mysql*install.php filename:

Example:

 <modules>
        <GogiProductions_CMSubtotals>
            <version>1.0.0</version>
        </GogiProductions_CMSubtotals>
    </modules>

Must be matched with this filename:

sql/mysql4-install-1.0.0.php


You can confirm that your setup is being initialized by checking your core_resources Magento table for entry:

cmsubtotals_setup


Saturday, September 19, 2015

PRODUCTIVITY AND MUSIC - Does background music increase or reduce your attention?

We all strive to be more productive. Some of use consider listening to music while working at their computer a good idea. However, one recent study shows that it might be the best not to listen to any kind of music while working. The study compared the attention and concentration of several student groups. The control group was put in a quiet room, without background music of any kind. Another group was tested with classical music, and yet another group was listening to hip hop music.

The best results in reading and comprehension of read text was achieved by control group - the group which was working in a quiet room.



Here's a quote from results of a research paper:

"From the finding of this study, we can conclude that the best way for students to study is to study in a quiet room. The participants who scored the highest in the reading comprehension task were the control group who performed the reading task in silence. A quiet or silent room would be the best condition for learning because there are fewer distractions that would take the attention or focus away from the task at hand. This is important in today’s society since our daily lives are immersed in technology"


Full paper can be found here:
http://josotl.indiana.edu/article/view/1733


Do you agree with this?
Do you perform the best when working in a quiet room? Or you consider listening to music a major boost factor for your productivity?

Monday, September 7, 2015

Enabling PDO for MySQL database access on GoDaddy shared hosting

If you find yourself with an error such as "Fatal Error: Class PDO not found", and you are using GoDaddy shared host (or any similar shared hosting), here is what you have to do.

First of all, in case of GoDaddy shared hosting, make sure you are using Linux host, as PDO is supported only for Linux shared hosting.

Assuming you are using Linux, you need to modify php.ini file in your root folder.

Add these 4 lines to it:
extension=pdo.so
extension=pdo_sqlite.so
extension=sqlite.so
extension=pdo_mysql.so

This will enable PDO database acccess for MySQL and SQLite databases.

After that , to make this change immediately effective, go to your cpanel -> PHP Processes and hit Kill All (PHP) processes. After this , you should have your PDO class for database access back online!


Saturday, September 5, 2015

CSS not being applied, CSS selector not matching elements

Whenever you have a non-working CSS code, you first must check if there's any kind of syntax error in the css itself. If there's any error in any line, CSS execution will stop on that line. This means that anything below the error line will be discarded. So, whenever you have CSS problems, remember to first check for syntax errors in CSS files. A good editor, such as Sublime, can help you out here.

CSS link selector - a vs a:link

In case of visited links not showing proper CSS values, you can try to replace a selector with it's pseudo-class: a:link. I.E.

Instead of:

a:visited,a:active, a
{

}

one could write:

a:visited, a:active, a:link
{

}

CSS - space problems

In CSS, it seems that #id.class is invalid syntax. Space character must be included between two selectors. Therefore, the right syntax for  obtaining a class inside #id element is:

#id .class

PSR-1 and PSR-2 coding standards for PHP

Visual aspects of code play a significant role in raising or drowning developer's productivity. In case that there's too much clutte...