Sunday, September 2, 2012

Fix for Magento’s Shipping API’s “sales_order_shipment.list” call


When searching for shipments, the most likely parameter we’d want to search by is “order_increment_id” i.e. what shipments are attached to a given order.  However, this is not possible with the way the API is written.  If we attempt to do a search of the form:

$params = array(array('order_increment_id' => $value));
$shipments = $this->call('sales_order_shipment.list', $params);


we get back this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'order_increment_id' in 'where clause'

The reason for this lies in:
/core/Mage/Sales/Model/Order/Shipment/Api.php

This call does not work correctly:
$collection = Mage::getResourceModel('sales/order_shipment_collection')
   ->addAttributeToSelect('increment_id')
   ->addAttributeToSelect('created_at')
   ->addAttributeToSelect('total_qty')
   ->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left')
    ->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left')
    ->joinAttribute('order_increment_id', 'order/increment_id', 'order_id', null, 'left')
    ->joinAttribute('order_created_at', 'order/created_at', 'order_id', null, 'left');

If you go search for this on the net, you will find in various places where it says that “joinAttribute” will join the “order” table using the “using the “order_id” field as the join ON column and map “increment_id” to “order_increment_id”.   But after much effort trying to figure out what parameters you can pass to this call to access said “order_increment_id” field, you will eventually have to give up in frustration.  The reason for this lies in the “joinAttribute” function.

Here it is in all its glory
From:  app/code/core/Mage/Sales/Model/Mysql4/Collection/Abstract.php

/**
 * Backward compatibility with EAV collection
 * @todo implement join functionality if necessary
 */
 public function joinAttribute($alias, $attribute, $bind, $filter=null,
                                $joinType='inner', $storeId=null)
 {
    return $this;
 }

Note the “todo”.   This function currently does absolutely nothing but return “$this” so as to not break the chaining.  So if we want to be able to search for shipments by “order_increment_id”, we are going to have to modify either “joinAttribute” or the API. 

I opted to go the route of modifying the API.  By adding one line, we can join in the order shipment grid table ‘sales_flat_order_shipment_grid’ and pick up the field we are after.  But of course, we don’t want to modify the core file itself, so we move it to:
app/code/local/Mage/Sales/Model/Mysql4/Collection/Abstract.php first.

$collection = Mage::getResourceModel('sales/order_shipment_collection')
   ->addAttributeToSelect('increment_id')
   ->addAttributeToSelect('created_at')
   ->addAttributeToSelect('total_qty')
   ->join('sales/shipment_grid',
          '`main_table`.`increment_id`=`sales/shipment_grid`.`increment_id`',
          'order_increment_id')
   ->joinAttribute('shipping_firstname', 'order_address/firstname',
                   'shipping_address_id', null, 'left')
   ->joinAttribute('shipping_lastname', 'order_address/lastname',
                   'shipping_address_id', null, 'left')
   ->joinAttribute('order_increment_id', 'order/increment_id',
                   'order_id', null, 'left')
   ->joinAttribute('order_created_at', 'order/created_at', 'order_id', null, 'left');  


Note that I opted not to remove the useless “joinAttribute” method calls.  It will be easier to figure out what to do with this if and when Magento finally fixes this if we leave them in so it’s more obvious what we’ve done.