Jan 17, 2012
kalpesh

Magento: Linking multiple shipments with their invoices

In Magento, it’s a feature to create multiple invoices and shipments. But you can’t find the link between invoice with their respective shipment if you have more than one invoice and shipment. It’s because if you have forced invoice and shipment enabled (Invoice and Ship button combined in Manage Orders view page), it saves both invoice and shipment object together and hence can’t give the invoice id to shipment and hence fails in building the link between them.

So what we need to do here is:
1. Add a column to sales_flat_shipment which will store invoice increment id (say invoice_id)
2. Before invoice and shipment are saved, get the invoice’s latest increment id and increment it by 1 (to get next invoice increment id)
3. Give that invoice increment id to shipment object, so it will get saved along with other shipment columns

Here we go technically,
Continue reading »

Jan 17, 2012
kalpesh

Magento: Adding column to sales_flat_order_item, sales_flat_invoice_item and sales_flat_shipment_item

Suppose you want to add column to some table before it gets save in Magento. Example, Magento doesn’t save regular price of product when an order is placed, it only saves the selling price. So if your product have some special price in it, then Magento only saves it’s special price when an order is placed, so there is no track of regular price of that product in order item table. Same it goes to invoice item and shipment item. When creating invoice and shipment, Magento doesn’t have any track on the regular price of invoiced item’s and shipment item’s regular price.

So here I show you how you will add a column “product_mrp” in each of 3 tables and update the information without firing any query!

First of all, make an installer script in your module that will alter these three tables and add column “product_mrp” or any of your choice.
Continue reading »

Jan 8, 2012
kalpesh

Magento: Save shipment information of order programatically

After creating invoice and shipment, it is necessary to add tracking information to shipment. Here is how to write a observer which will invoke as shipment save method is called and save tracking information programatically.

config.xml – under global -> events node

1
2
3
4
5
6
7
8
9
<sales_order_shipment_save_before>
                <observers>
                    <namespace_modulename_ship_before>
                        <type>singleton</type>
                        <class>Namespace_Modulename_Model_Observer</class>
                        <method>salesOrderShipmentSaveBefore</method>
                    </namespace_modulename_ship_before>
                </observers>
            </sales_order_shipment_save_before>

Observer.php -> under Model directory of module

1
2
3
4
5
6
7
8
9
public function salesOrderShipmentSaveBefore($observer)
    {
            $shipment = $observer->getEvent()->getShipment();
            $track = Mage::getModel('sales/order_shipment_track')
                        ->setNumber('824343454454') //tracking number / awb number
                        ->setCarrierCode('aramex') //carrier code
                        ->setTitle('Aramex'); //carrier title
                    $shipment->addTrack($track);
       }
Jan 8, 2012
kalpesh

Magento: Mysql records with NULL values are not fetched in query

After banging my head on my desk trying to get the records with NULL values with Magento ORM, it was found that writing

1
2
$collection->addAttributeToFilter('somefield', 'null')
$collection->addAttributeToFilter('somefield', array('is' => 'null'))

will check for any blank values like this: WHERE somefield = ”

So if you want to fetch records that have NULL values in Magento style, you need to write as following:

1
$collection->addAttributeToFilter('somefield', array('null'=>'null')

will check like WHERE somefield = null

Hope this saves someone’s time!

Jan 5, 2012
kalpesh

Magento: Wrong count in admin Grid when using GROUP BY clause, overriding lib module

If you have noticed or not, in version 1.5 of Magento when you use GROUP BY clause in any Grid.php file in admin, the count always display wrong. Many times it displays 1 even if there are hundreds of records. Due to this your pagination also doesn’t work. This is a bug in Magento. Your getSize() always returns wrong count whereas total records in grid are proper.

To fix this, you need to edit one of your core file. As it’s not a good practice to edit core file, we will here override the core file.
Overriding LIB module

Copy Db.php file from magento / lib / Varien / Data / Collection / Db.php
Paste it to your local directory so the resultant folder structure would look like this:
magento / app / code / local / Varien / Data / Collection / Db.php
Continue reading »

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications