Mar 30 2013
Mar 30

Few month ago I developed  a simple module called Commerce Order2pdf and then I also added custom tokens as you can read from this post Custom token creation for Drupal 7 and now I reviewed this module a bit and planned to add views field handler (This means I can create custom view with download links). 

So how did I implemented this?

First step was to edit commerce_order2pdf.module file and add hook_views_api. So I could start working with views.

/**
 * Implements hook_views_api().
 */
function commerce_order2pdf_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'commerce_order2pdf') . '/includes/views',
  );
}

Next one was to create subdirectories includes/views into my commerce_order2pdf folder (module folder).

After this I had to tell views about my data structure.
So I did created new file /includes/views/commerce_order2pdf.views.inc and added hook_views_data.

/**
 * Implements hook_views_data()
 */
function commerce_order2pdf_views_data() {
  $data = array();
  $data['commerce_order']['order2pdf_link'] = array(
    'field' => array(
      'title' => t('Download pdf order'),
      'help' => t('Provide a simple link to download the order.'),
      'handler' => 'commerce_order2pdf_handler_field_order_pdf_link',
    ),
  );
  return $data;
 }

In this array I did extended commerce_order group and added field handler class callback.

Now we will add a new file commerce_order2pdf_handler_field_order_pdf_link.inc to includes/views folder.

/**
 * Field handler to present an order pdf download link.
 */
class commerce_order2pdf_handler_field_order_pdf_link extends commerce_order_handler_field_order_link {
  function construct() {
    parent::construct();
  }

  function render($values) {
    $order = commerce_order_new();
    $order->order_id = $this->get_value($values, 'order_id');
    // Add hash key for a download link.
    $hash = hash('md5', $order->order_id . $values->commerce_order_created);

    $text = !empty($this->options['text']) ? $this->options['text'] : t('Download');
    return l($text, "order2pdf/{$order->order_id}/{$hash}");
  }
}

My last step is to say Drupal about these files in commerce_order2pdf.info

For this step I will just add two rows to my .info file. 

files[] = includes/views/commerce_order2pdf.views.inc
files[] = includes/views/commerce_order2pdf_link_handler.inc

Now you can add views download field to your view. (If you make thses changes after you enabled your module then you can just clear caches and it will work).

Edit 13. April 2013 
Just found a good videos

[embedded content]

[embedded content]

Nov 06 2012
Nov 06

Few days ago my client came and said.. I need to exlude a shipping when there's special type of product.
At first I was sure this can be done with rules but it didn't work out for me.
So after spending some time with this I wrote some php.
At first I would like you to know. Its not good idea to put some php throught the database, becouse it will make your site at least a bit slower. But still my client needed this really fast so I did it.
At first from the shipping options I choosed configure component and added new condition wich was a PHP.

# Get all the line item ids from the order.
$line_item_ids = $commerce_order->commerce_line_items[LANGUAGE_NONE];
$line_items = commerce_line_item_load_multiple($line_item_ids);
# If the line item is the type of product then add this to array.   
foreach($line_items as $line_item) {
  # Put all the product types the array.
  if(isset($line_item->commerce_product[LANGUAGE_NONE][0]['product_id'])) {
      $product_ids[] = $line_item->commerce_product[LANGUAGE_NONE][0]['product_id'];
  }
}
$products = commerce_product_load_multiple( $product_ids);
foreach($products as $product) {
  $types[] = $product->type;
}
# If the product type (tops) is in array then return true.
if (in_array('tops', $types)) {
   return TRUE;
}

After I just added if this is is true then negate. This could be easily used other ways. Lets say you want to do something if theres this type of procuct in the shppping cart.

Oct 11 2012
Oct 11

 Today I had a plan to create tokens for my new module wich will create hashed pdf link for commerce order, becouse it would be really nice to use tokens with rules. So I just looked how it works and wanted to create some simple examples.
I can easily create new token from scratch with these two functions.

/**
 * Implements hook_token_info_alter().
 */
function commerce_order2pdf_token_info() {
  $info['tokens']['commerce-order']['hashed_pdf_link'] = array(
    'name' => t('Hashed link to pdf'),
    'description' => t("Hashed link for pdf generation."),
  );
  return $info;
}
/**
 * Implements hook_tokens().
 */
function commerce_order2pdf_tokens($type, $tokens) {
 if($type = 'commerce-order') {
   $replacements['[commerce-order:hashed_pdf_link]'] = 'my replacement';
 }
  return $replacements;
}

What I really want to do is to add new tokens to my existing tokens and use existing data. So I just had to use hook_token_info_alter and hook_token_alter functions.

/**
 * Implements hook_token_info_alter().
 */
function commerce_order2pdf_token_info_alter(&$data) {
  $data['tokens']['commerce-order']['hashed_pdf_link'] = array(
    'name' => t('Hashed link to pdf'),
    'description' => t("Hashed link for pdf generation."),
  );
  return $data;
}
/**
 * implements hook_tokens_alter().
 */
function commerce_order2pdf_tokens_alter(array &$replacements, array $context) {
  $order = $context['data']['commerce-order'];
  $hash = hash('md5', $order->order_id . $order->created);
  $replacements['[commerce-order:hashed_pdf_link]'] =  l(t('Download order as pdf'),
"order/{$order->order_id}/" . $hash, array('absolute' => TRUE));
}

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web