Tuesday , 30 September 2014
Topic 9 – Part 3: Shipment

Topic 9 – Part 3: Shipment

Welcome to our Magento blog!

Let’s come back to our topic: Sales and Customer! It has been two weeks since we talked about part 2: Invoice. Can’t let you wait any more, we continue with order process in Magento. In this post, I’ll talk about the shipment, the next step of sale process.

1. Shipment Model

Shipment model (Mage_Sales_Model_Order_Shipment) works with shipment data. Magento allows you to create many shipments for an order with items which can ship separately. With each shipment, you can get shipment items by method getAllItems():

public function getItemsCollection()
{
if (empty($this->_items)) {
$this->_items = Mage::getResourceModel('sales/order_shipment_item_collection')
->setShipmentFilter($this->getId());

if ($this->getId()) {
foreach ($this->_items as $item) {
$item->setShipment($this);
}
}
}
return $this->_items;
}

public function getAllItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted()) {
$items[] =  $item;
}
}
return $items;
}

Besides, shipment has another important method is getAllTracks(). It returns all track information of shipment:

[php]public function getAllTracks()
{
$tracks = array();
foreach ($this->getTracksCollection() as $track) {
if (!$track->isDeleted()) {
$tracks[] = $track;
}
}
return $tracks;
}
[/php]

Specifically, track information has carrier name, title, and track number. This information allows you to track package shipment.

Magento blog on shipment1

Shipping carriers has API (such as DHL, Federal Express…) allowing you to track shipment online. With track number, you will get the detail of package shipment as shipping date, service, weight, length… You can find code to get tracking detail in file app\code\core\Mage\Sales\Model\Order\Shipment\Track.php:

[php]/**
* Retrieve detail for shipment track
*
* @return string
*/
public function getNumberDetail()
{
$carrierInstance = Mage::getSingleton(‘shipping/config’)->getCarrierInstance($this->getCarrierCode());
if (!$carrierInstance) {
$custom['title'] = $this->getTitle();
$custom['number'] = $this->getNumber();
return $custom;
} else {
$carrierInstance->setStore($this->getStore());
}

if (!$trackingInfo = $carrierInstance->getTrackingInfo($this->getNumber())) {
return Mage::helper(‘sales’)->__(‘No detail for number “%s”‘, $this->getNumber());
}

return $trackingInfo;
}
[/php]

2. Store Shipping Information

Magento stores shipping and tracking information in five tables as below:

Magento blog tutorial on shipment

Table sales_flat_shipment stores main information of shipment.

Table sales_flat_shipment_comment stores comments for a shipment.

Table sales_flat_shipment_item stores list of items of shipment.

Table sales_flat_shipment_grid stores some information of shipment to increase loading speed when showing a grid invoice.

Table sales_flat_shipment_track stores tracking information for a shipment.

That’s all about shipment today. Many people think it’s so sophisticated to practice; in fact, it’s not difficult to understand. I hope that all information in this post will help you with your jobs.

To get helpful knowledge for Magento developer exam, read Magento Certificate in our Magento blog.

About David

David has been developing many great extensions in Magestore. He has a strong passion on programming and want to create product/service that serve society.

6 comments

  1. Hi David,

    Yesterday I have cleared the Magento Certified Developer exam in my First Attempt. Your series dedicated for Magento Certification is very useful for me… Keep it up guys. :2thumbup

  2. hi david,
    thank you very much for your blogs and tutorial i cleared magento certified developer plus exam on 28 december 2012.
    thanks once again

  3. David, your posts on magento play a big part in my training for my Magento Certification (Plus).
    Thanks for all your hard work giving back to the community and keep it up !
    (Happy New Year)

  4. I’ve passed the certification last month, thanks for your tutorial!!