← back to code snippets


Woocommerce 2.0 plugin hooks

Posted on February 19th, 2014 in WordPress by Graeme

Over the last couple of days, I’ve been making a WooCommerce plugin which enables users to personlize products by attaching their own custom information, such as an image, or extra text to a product.

In my hunt for a good starting point, I came accross this excellent guide by Craig Martin. He outlines how to add custom forms to a WooCommerce product, which is exactly the goal of my plugin. He even provides great examples of his own code. Great!

His guide however, would have been an ideal solution had WooCommerce not altered a few of their plugin hooks in the new version 2.0. Although, Craig’s guide is still mostly relevant! So here’s how to upgrade the plugin to be compliant with WooCommerce 2.0 (instead of ?1.6?)


Here’s the changes I had to make 

  • The first page of the Craig’s plugin is fine (as far as I can remember), it is still complaint with WooCommerce 2.0.

Now the second page is where there are some alterations needed!

This hook: woocommerce_order_item_meta has been removed in Woo 2.0, and now we must use: “woocommerce_add_order_item_meta” instead.

So in Craig’s code, the following change is necessary:

  • add_action(’woocommerce_order_item_meta’, array(&$this, ‘xatik_wc_order_item_meta’), 10, 2);

  • add_action(’woocommerce_add_order_item_meta’, array(&$this, 'xatik_wc_order_item_meta’), 10, 2);

You can read about the change here.


As a consequence of the above change, the function ’xatik_wc_order_item_meta’ must be changed! Here is Craigs original function:

function xatik_wc_order_item_meta($item_meta, $cart_item) {
$xatik_wc_options = $cart_item[’_xatik_wc_options’];
if (isset($xatik_wc_options['display_form’]) && $xatik_wc_options['display_form’]) {
if (isset($cart_item[’_xatik_wc_data’])) {

$data = $cart_item[’_xatik_wc_data’];

$item_meta->add('Font Size’, $data['font_size’]);
}
}
}

This (I think) should be changed to something like this (I’ve done it just for font size):

public function add_order_item_meta( $item_id, $cart_item ) 
{
  $xatik_wc_options = get_post_meta($post->ID, '_xatik_wc_options', true);
  
if (isset($xatik_wc_options['display_form']) && $xatik_wc_options['display_form'])
{
if ( ! empty( $cart_item['font_size'] ) )
{ woocommerce_add_order_item_meta($item_id,__( 'Font Size'), __('$cart_item['font_size']') ); } } }

1. So one of the main changes you see above is the assignment to $xatic_wc_options. And then the next one is instead of $item_meta->add, we are now using the new function:

woocommerce_add_order_item_meta ($item_id, $title, $value)

2. Aditionally, we can now access the $cart_item meta directly! So instead of doing this:

$data = $cart_item[‘_xatik_wc_data’];

$item_meta->add(‘Font Size’, $data[‘font_size’]);

We now only have to do this:

$cart_item[‘font_size’]

Compare the above two snippets. We no longer need to use $cart_item[’_xatik_wc_data], since we can access font_size directly now. That is the main transition of Craig’s plugin to be compliant with WooCommerce 2.0! It did seem a bit confusing at first, but I’m sure this way of accessing the $cart_item meta is much simpler after all.

That is probably the only change needed, except, wherever in any of the other functions, there is an assignment to $xatic_wc_options, I have swapped it for the first change outlined at 1.