Search code examples
phpmagentomagento-1.5

Magento: Include Configurable Product Sku on Invoice - Currently Associated Simple Product Only Shown


I need to include the Configurable Product SKU on all print invoices as the configurable product SKU is human readable but the simple products are 16 digit numerical barcodes which makes for picking products a nightmare.

I've found /app/code/core/Mage/Sales/Model/Order/Pdf/Invoice.php which writes the document. In it, it has the following which I believe creates the file.

    $page->drawText(Mage::helper('sales')->__('Product'), 35, $this->y, 'UTF-8');
    $page->drawText(Mage::helper('sales')->__('SKU'), 255, $this->y, 'UTF-8');
    $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
    $page->drawText(Mage::helper('sales')->__('Qty'), 430, $this->y, 'UTF-8');
    $page->drawText(Mage::helper('sales')->__('Tax'), 480, $this->y, 'UTF-8');
    $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');

My question is how can I change 'SKU' to be the configurable product SKU. All products in my store are configurable so it's not an issue it overrides the simple product sku.

Thanks.


Solution

  • Magento saves two order items for each configurable product sold. You can see this in the sales_flat_order_item table. However, you'll notice that the SKU on both of those items is the same... the simple SKU. So, unfortunately, there isn't a simple way to get the configurable product's SKU, but it can be done.

    1. Find where the order items are loaded*. Look for:

      $order/$invoice->getAllItems()

    2. Get the product_id of the configurable product using the product_id stored with the order_item

      $item->getProductId()

    3. Load the configurable product

      $product = Mage::getModel('catalog/product')->load($product_id_from_step2)

    4. Set** the SKU on the order_item to be the SKU from the real product

      $item->setSku($product->getSku())

    *- This is somewhere in the file you referenced in your question. Note, however, that the code you have above is actually displaying the table's header row, not the actual product data.

    **- This isn't saving anything to the database unless you call $item->save(). You are just modifying the object loaded in memory temporarily