WooCommerce 1.6.6 -> 2.0 – Plugin and theme compatibility

You are here:

This document provides handy code snippets, outlines some of the main changes in WC 2.0 and explains how you can maintain compatibility.

Other useful resources relating to WC 2.0 include:

  • Migrating your plugin to WC 2.0
  • Beta 1 announcement post
  • Beta 2 announcement post
  • Coen on WC 2.0

Detecting WC version

If you need to conditionally handle WC 2.0, use the following:

if ( version_compare( WOOCOMMERCE_VERSION, '2.0', '<' ) ) {
  // Pre 2.0
} else {
  // 2.0
}

Changes affecting Payment Gateways

There are 2 main changes to gateways which will affect plugins;

  1. The save hook
  2. Dynamic loading

Save hooks have been tweaks so that only 1 gateway is shown and saved per page. To maintain compatibility with 1.6.6 and 2.0 use the following snippet:

/* 1.6.6 */
add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );

/* 2.0.0 */
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );

This will ensure the save function kicks in on both versions of WooCommerce.

The second change is dynamic loading, and this can be a bit more involved. Basically, in the name of optimisation gateways are only loaded when needed. If you have an IPN like function (on ‘init’ for example) it may no longer be triggered. Therefore to get around this, use WC-API.

WC-API callbacks

Callbacks should be in the below format:

http://yoursite.com/?wc-api=WC_Gateway_Your_Gateway_Class

WooCommerce will see this callback, init the class passed to wc-api, and then trigger an action:

woocommerce_api_wc_gateway_your_gateway_class

Hook into that via your payment gateway and you can do your required actions.

add_action( 'woocommerce_api_' . strtolower( get_class( $this ) ), array( $this, 'gateway_response' ) );

Order statuses

If your gateway leaves an order in pending status you are #doingitwrong. Pending is unpaid, and as of WC 2.0, unpaid orders will be cancelled after x hours.

Changes to WC_Product classes

Probably the change most likely to break sites is the WC_Product class – this is now abstract and extended by specific product classes depending on product type. Do not initiate WC_Product – it will cause a fatal error in 2.0.

Instead use the get_product() function. This handles product types and returns a product object to you. Below is an example of backwards compatible code for loading products:

if ( function_exists( 'get_product' ) )
$product = get_product( $post->ID );
else
$product = new WC_Product( $post->ID );

If you only need 2.0 compatibility, just use get_product on its own.

Custom field data

product_custom_fields won’t be in the product classes as of 2.0 – you can get meta just like this:

$product_class->meta_key

The meta will be loaded dynamically.

Sessions with WC 2.0

In WC 2.0 we decided to stop supporting PHP SESSIONS – you can still use them, but you’ll need your own session_start(). Your choice. The alternative however, is our new session class (which at the time of writing uses cookies and transients/options).

To set session data just use:

$woocommerce->session->your_var = 'x';

To unset data use:

unset( $woocommerce->session->your_var );

Theme Gotchas

[box type=”alert”]Some class names containing woocommerce_ or wc- have been renamed for consistency to woocommerce- (hyphenated). You will need to replace all instances of ‘.wc-‘ or ‘.woocommerce_‘ with ‘.woocommerce-‘ in your custom stylesheet(s).[/box]

Widgets have been renamed so if you call them directly in your template, they need to be renamed. E.G. WC_Widget_Cart becomes WooCommerce_Widget_Cart.

Pulling gallery images dynamically? Re-check this, the attachment ID’s are stored in meta now.

Pagination and sorting has received an overhaul. Sorting is now before the loop and accompanied by a product count. Pagination remains beneath the loop.

Star ratings are in the loop now

Quantity inputs now use input type="number" so you will need to choose whether to hide our plus / minus buttons (added via jQuery) or hide the browser default UI. Use the following to hide the browser default:

input::-webkit-outer-spin-button, input::-webkit-inner-spin-button {
display:none;
}

Checkout form is now validated in real time adding .woocomemerce-invalid or .woocomemerce-valid
to the respective .form-row You may want to style this.

Up sells are now hooked in ahead of related products on product details pages

If your theme uses custom loops, be sure to read this.

If your theme doesn’t declare WooCommerce support a message will be displayed in the dashboard. To declare WooCommerce support in your theme add the following to your functions.php file;

add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}

Template overrides

Check *all* template overrides if you have any – many files changed so custom template are likely to cause issues. And remember to only override what you need to edit, not all templates! Some theme authors do this and it causes nothing but trouble.

Was this article helpful?
Dislike 0
Views: 9