Skip to main content

Posts

Showing posts from 2015

Error reporting in php

Error reporting in php will allow us to track the errors on our website. The error may be notice, warning, fatal errors. The syntax is: error_reporting(); error_reporing(E_ALL); error_reporing(E_ALL ^ E_NOTICE); E_ALL is "everything" E_ALL & ~E_NOTICE is "everything except notices" E_ALL ^ E_NOTICE is "everything except notices"

Customize or Merge the fields in admin grid Magento

Sometimes we need to customize the columns of grid list other than the table fields. Like, merging two fields into one column or displaying content after conditional check. For this we need to add a renderer. Renderer is the class that can render the values of a column by manipulating the data and outputting the data as we like. For this, 1. Create a folder Renderer inside "Block/Adminhtml" of our module. 2. Create a file Somefilename.php with following contents class Organicfeast_Customshipping_Block_Adminhtml_Renderer_Suburbname extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {     public function render(Varien_Object $row) {                 if ($row->getData('first_name') != NULL || $row->getData('last_name') != NULL) {             $firstName = $row->getData('first_name');                     $lastName = $row->getData('last_name');             if ($lastName != NULL) {                 return $firstName .

Print Customer's Group Name on the transactional emails

For printing the customer’s group name on the order confirmation email, Firstly, we need to copy the following file "app/code/core/Mage/Sales/Model/Order.php" to  "app/code/local/Mage/Sales/Model/Order.php" as core files shouldn't be changed. In Order.php, we should add the following function. This function will return the customer group name. public function getCustomerGroupName(){     if ($groupId = $this->getCustomerGroupId()){         $group = Mage::getModel ('customer/group')->load($groupId);         if ($group->getId()){             return $group->getCode();         }     }     return ''; } Finally, we need to add the following code on the email template to get the group name, {{htmlescape var=order.getCustomerGroupName()}} Cheers!!

Error 503: Service Temporarily Unavailable Magento

We can come across a situation in magento where we are presented with the following message, "Service Temporarily Unavailable  The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later." This happens because when Magento is performing certain tasks, it temporarily creates a file called maintenance.flag. Magento automatically deletes this file when the task is complete. But when the task is incomplete or something wrong happen in between, then this file remains there. Magento checks for its existence and if found redirects to error 503 page. So, the solution is to check if the file maintenance.flag is in magento root. If it is there, then delete it. Cheers!!

Composer in php

For those who are unaware of composer in php, Composer is a tool for dependency management. It allows us to declare the dependent libraries our project needs and installs them in our project. It is inspired by the node's npm and ruby's bundler. So, basically if we have a project that depends on a number of libraries and those libraries depend on other libraries, we declare the libraries we depend on. Composer downloads and installs the required packages in our project. For this, we need a file composer.json that describes the project's dependencies. For e.g. we have used here monolog library to do logging. {     "require": {         "monolog/monolog": "1.2.*"     } }

Detecting the browsing device

We can check the device that is used for browsing. The following code detects if the browsing device is mobile. if (preg_match("/phone|iphone|itouch|ipod|symbian|android|htc_|htc-|palmos|blackberry|opera mini|iemobile|windows ce|nokia|fennec|hiptop|kindle|mot |mot-|webos\/|samsung|sonyericsson|^sie-|nintendo/", strtolower($_SERVER['HTTP_USER_AGENT']))) {  //browsed from mobile device }

Database server does not support the InnoDB storage engine

During installation of magento, sometimes we get the error as "database server does not support the InnoDB storage engine". To overcome this, edit the line of the file app/code/core/Mage/Install/Model/Installer/Db/Mysql4.php Replace public function supportEngine()     {         $variables  = $this->_getConnection()             ->fetchPairs('SHOW VARIABLES');         return (!isset($variables['have_innodb']) || $variables['have_innodb'] != 'YES') ? false : true;     } with public function supportEngine()     {         $variables  = $this->_getConnection()             ->fetchPairs('SHOW ENGINES');         return (isset($variables['InnoDB']) && $variables['InnoDB'] != 'NO');     }

Prevent a html element to stop moving at certain point when page is scrolled

We should make the position of the element fixed. For this, the following small jquery snippet will fulfill the requirement. jQuery(window).scroll(function() {                var button = jQuery("#div").position().top;            var window_top = jQuery(window).scrollTop();                if(window_top <= top || window_top >= button) {             jQuery("#sbasket").removeClass('sticky');                   } else {             if(window_top > top) {                 jQuery("#sbasket").addClass('sticky');             }         }     }); and on css, .sticky{position:fixed;top:0;} Cheers!!

Sending Transactional emails Magento

It's quite simple to send transactional emails within our custom code in magento. Below is the code snippet to send email.                 $templateId = 11; //its the transactional email template id                 $senderEmail = "no-reply@senderemail.com";                                $senderName = Mage::app()->getStore()->getName();                                 $sender = array('name' => $senderName,                                 'email' => $senderEmail);                                                 $recepientEmail = Mage::getStoreConfig('trans_email/ident_sales/email');                 $recepientName = Mage::getStoreConfig('trans_email/ident_sales/name');                                                 $storeId = Mage::app()->getStore()->getId();                 // Set variables that can be used in email template                 $vars = array('ordernum' => $refordernum);               (variable

Setting up cron using ssh

To setup cron job from ssh, firstly we need a ssh client like putty for windows. Then, Open the ssh client. Connect to the host using the ssh credentials. Type crontab -e command to view the cron file. There we can see the scripts to setup the cronjob something like this:  GNU nano 2.2.6        File: /tmp/crontab.nUqtfZ/crontab             Modified # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h  dom mon dow   command We can see the lines are commented. No cron script has been setup here. To add cron script, we can write it at end of the file. After adding cron script, # daemon's notion of time and timezones. # # Output

Writing custom query in Magento Ecommerce platform

Magento has its own database logic and functions to execute the queries. We don't need to hardcode the query string as we can call the magento model functions. However, we can also execute custom query whenever needed. The following script shows how we can achieve it, From the magento controller: protected $db; $this->db = Mage::getSingleton('core/resource')->getConnection('core_write'); $query = "SELECT * FROM " . $this->db->getTableName('tablename') ; $results = $this->db->fetchAll($query); From anywhere in site, / * Get the resource model */ $resource = Mage::getSingleton(‘core/resource’);     /** Retrieve the read connection */ $readConnection = $resource->getConnection(‘core_read’); $tableName = $resource->getTableName(‘catalog_product_entity’); $query = "SELECT * FROM " . $ tableName ;

Changing the attribute type in Magento

Sometimes, we need to change the input type of attributes in Magento. Magento by default doesn't allow to change the input type from backend simply. We need to do this programmatically. Here is a code to change the input type from dropdown to text. $installer = new Mage_Eav_Model_Entity_Setup('core_setup'); $installer->startSetup(); $iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId(); $idAttributeOldSelect = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer'); $installer->updateAttribute($iProductEntityTypeId, $idAttributeOldSelect, array(     'frontend_input' => 'text' )); $installer->endSetup(); Finally, we need to remove the old values or they will conflict with the new setup. DELETE FROM catalog_product_entity_int WHERE entity_type_id = 4 and attribute_id = YOUR_ATTRIBUTE_ID_HERE; Cheers!!