Archive for the ‘Tips’ Category

Adding sql script to add column to existing d b table

filename: mysql4-upgrade-1.1.1-1.1.2.php <?php   $installer = $this; $installer->startSetup();   $connection = $installer->getConnection();   $tableName = $installer->getTable(’your_table_name’); $column = ‘newcolumn_id’; $comment = ‘Newcolumn ID’;   if (!$connection->tableColumnExists($tableName, $column)) { $connection->addColumn($tableName, $column, array( "type" => Varien_Db_Ddl_Table::TYPE_INTEGER, "nullable" => true, "length" => 10, "after" => "customer_id", "comment" => $comment )); }   $installer->endSetup();

Adding sql script to create database table

filename: mysql4-install-1.1.1.php <?php   $installer = $this; $installer->startSetup();   $tableName = $installer->getTable(’your_table_name’);   // Check if the table already exists if ($installer->getConnection()->isTableExists($tableName) != true) {   $table = $installer->getConnection() ->newTable($installer->getTable($tableName)) ->addColumn(’column_id’, Varien_Db_Ddl_Table::TYPE_INTEGER, null, array( ‘identity’ => true, ‘unsigned’ => true, ‘nullable’ => false, ‘primary’ => true, ), ‘Email Stats Id’) ->addColumn(’customer_id’, Varien_Db_Ddl_Table::TYPE_INTEGER, null, array( ‘identity’ […]

Magento create Creditmemo to refund online or offline

<?php   $orderId =’123456789′; $invoiceId = ‘1234’;   $data = array( ‘items’ => array( ‘12345’ => array(’qty’ => ‘1’), ‘12346’ => array(’qty’ => ‘2’) ), ‘comment_text’ => null, ‘do_offline’ => 1, ‘shipping_amount’ => 0, ‘adjustment_positive’ => ‘0’, ‘adjustment_negative’ => ‘0’, ‘refund_customerbalance_return_enable’ => 1, ‘refund_customerbalance_return’ => 39.9 );   // load order and invoice $order = […]

Magento get order invoice id and order items

<?php $orderId = ‘1234567890’; $order = Mage::getModel(’sales/order’)->load($orderId);   if ($order->hasInvoices()) { $invoiceIds = $order->getInvoiceCollection()->getAllIds(); $invoiceId = $invoiceIds[0]; echo ‘invoiceId: ‘.$invoiceId."\n"; }   foreach ($order->getAllItems() as $item) { echo $item->getId().’->’.$item->getQtyOrdered()."\n"; }

Images media folder

Magento keeps images under media/catalog/product/ folder, full path to the file in adminhtml can be for example media/catalog/product/[1st_letter_of_filename]/[2nd_letter_of_filename]/filename.jpg In frontend images loaded from media/catalog/product/cache/ folder, full path to cache file in frontend can be for example media/catalog/product/cache/image/*, media/catalog/product/cache/small_image/* and media/catalog/product/cache/thumbnail/* When we do System -> Cache Management -> Flush Catalog Images Cache it deleted entire […]

Magento update order billing or shipping address

$shippingAddress = Mage::getModel(’sales/order_address’)->load($order->getShippingAddress()->getId());   $shippingAddress ->setFirstname("value") ->setMiddlename("value") ->setLastname("value") ->setSuffix("value") ->setCompany("value") ->setStreet("value")) ->setCity("value") ->setCountry_id("value") ->setRegion("value") ->setRegion_id("value") ->setPostcode("value") ->setTelephone("value") ->setFax("value")->save();

Magento add Js Script

Method 1. Adding to layout file <reference name="head"> <action method="addJs"><script>skin/frontend/base/default/js/yourextension/index/scriptname.js</script></action> </reference> Method 2. Adding to phtml file <?php echo $this->helper(’core/js’)->includeSkinScript(’js/yourextension/index/scriptname.js’) ?>

Magento order calculation formula

orders.subtotal = ((orders.grand_total+IFNULL(orders.base_customer_balance_amount,0)) + ABS(orders.base_discount_amount))-orders.base_shipping_amount Explanation: Order items makes subtotal, then shipping cost is added to it. From that sum discount is extracted and customer balance (if any) extracted. Remaining amount is total due to pay (amount which really needs to be paid by customer) or grand_total

How-to generate magento extension package

1. Go to magento root folder and inside the var folder, create a new folder connect. 2. Login to admin panel in your browser. 3. From the admin panel, navigate to System > Magento Connect > Package extensions. 4. This will enable you to create extension package page.

Put magento into maintenance mode

Simply create empty file maintenance.flag in magento www folder