Skip to main content

Posts

Showing posts from June, 2015

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!!