Thursday, March 20, 2014

Drupal Commerce Bulk Order Printing

You will need to download and enable Views Bulk Operations and Commerce Invoice Receipt for this solution. This also assumes you know how to create a basic module.

Configure commerce_orders view

Add the Bulk Operations Commerce Order field if it's not already there. In the fields configuration, you need to check "Pass ids as arguments to a page". The other two checkboxes are optional. Final thing to do is enter the url path. Apply and save view. You should see the following or something like it:





hook_menu

We need to use hook_menu to create the page specified above.
function commerce_bulk_printing_menu() {
 $items = array();
 $items['admin/commerce/orders/print/%'] = array(
  'title' => 'Bulk Printing',
  'description' => 'Outputs printable invoices for each order id passed.',
  'page callback' => '_commerce_bulk_printing_print',
  'page arguments' => array(4),
  'access callback' => TRUE,
  'weight' => 10,
 );
 return $items;
}

_commerce_bulk_printing_print($ids)

And now for the function that uses the order ids we passed and outputs printable invoices.
/**
 * outputs a printable view of invoices
 * most of the functionality in this module was taken from commerce_invoice_receipt_view_print
 */
function _commerce_bulk_printing_print($ids) {
 $orderids = explode(",", $ids);
 $styles[] = array(
  'type' => 'file',
  'media' => 'all',
  'data' => commerce_invoice_reciept_css_path(),
  'group' => CSS_DEFAULT,
  'every_page' => FALSE,
  'weight' => 0,
  'preprocess' => FALSE,
  'browsers' => array(),
 );
 $html = drupal_get_css($styles);
 foreach($orderids as $orderid) {
  $order = commerce_order_load($orderid);
  $build = entity_view('commerce_order', array($order->order_id => $order), 'invoice', NULL, TRUE);
  $invoice_info = _commerce_invoice_receipt_get_invoice_info($order, $build);
  $html .= theme('commerce_order_invoice_view', array('info' => $invoice_info, 'order' => $order));
 }
 print _emogrifier_process($html, NULL, NULL, NULL, NULL, NULL);
}

Override commerce-order-invoice-view.tpl.php

When printing multiple orders, they will show up on the same page unless we specify when the page ends. In commerce-order-invoice-view.tpl.php, add page-break-after:always class to the body tag.

That's it. You should now see a page like the image below when you try to print multiple orders

No comments:

Post a Comment