(Note – this guide assumes a good familiarity with PHP development and WordPress in general)

WooCommerce is an excellent e-commerce plugin for WordPress, powering over 28% of all online stores at the time of writing. It’s free, packed full of features, has its own API and integrates with dozens of other services and plugins right out of the box.

If you’ve never worked with WooCommerce before, the huge feature set can seem a bit daunting.

The documentation is pretty good, but WooCommerce has been around since 2011 undergoing constant, heavy development, so a lot of the code snippets you’ll find online are quite out of date.

This guide is intended to give you a brief glimpse of how WooCommerce works under the hood, with some useful code snippets for building custom integrations or working with WooCommerce data.

It will be up to date at the time of writing (May 2018) but if you’re reading this in the distant future – possibly via some kind of haptic glove or neural interface – you should probably consult the contemporary WooCommerce documentation.

Relax – it’s quite WordPress-ish

WooCommerce does most things in a very WordPress way and is packed with filters and actions to help you work with it. If you’re already familiar with custom post types and meta fields, you’ll be pleased to know that WooCommerce uses these too.

Products are a custom post type. When added to the cart, they become a cart item (a property on the WC cart object). When checkout is completed, an order is created and the items in the cart are converted to order_items.

You can find a lot of background on the structure and implementation of different WooCommerce features in the documentation, so I’ll skip the lengthy explanations and just drop some code snippets for common tasks:


Get all (published) products:

$products = wc_get_products();

Load a single product:

$product = wc_get_product($post_id);

Get some data from a product:

$price = $product->get_price();
$sale_price = $product->get_sale_price();
$sku = $product->get_sku();

Update some product data:

$product->set_sku('abc123'); //or update_post_meta($post_id,'_sku','abc123');

Create a product programmatically (minimal example):

$data = array(
  'name' => 'Test product',
  'description' => 'Lorem ipsum',
  'regular_price' => '9.99'
$request = new WP_REST_Request( 'POST' );
$request->set_body_params( $data );
$products_controller = new WC_REST_Products_Controller;
$response = $products_controller->create_item( $request );

Note: there a lot of tutorials for doing this via wp_insert_post(), which will (probably) work but will not be as reliable as a basic WooCommerce API request.


Load an order:

$order = wc_get_order($post_id);

Update order meta:

$order->update_meta_data( 'my_meta_key', $my_value );

Work with order items (products on the order):

Note: order items and order item meta have their own database tables – they are not stored in wp_posts and wp_postmeta, presumably to help with scaling on large stores.

$items = $order->get_items();

foreach ( $items as $item_guid => $item ) {
  $some_meta = wc_get_order_item_meta($item_guid, 'my_meta_key', true);

Update WooCommerce order addresses:

$address = array(
 'first_name' => get_user_meta( $user_id, 'billing_first_name', true ),
 'last_name' => get_user_meta( $user_id, 'billing_last_name', true ),
 'company' => get_user_meta( $user_id, 'billing_company', true ),
 'email' => get_user_meta( $user_id, 'billing_email', true ),
 'phone' => get_user_meta( $user_id, 'billing_phone', true ),
 'address_1' => get_user_meta( $user_id, 'billing_address_1', true ),
 'address_2' => get_user_meta( $user_id, 'billing_address_2', true ),
 'city' => get_user_meta( $user_id, 'billing_city', true ),
 'state' => get_user_meta( $user_id, 'billing_state', true ),
 'postcode' => get_user_meta( $user_id, 'billing_postcode', true ),
 'country' => get_user_meta( $user_id, 'billing_country', true )
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );

The Cart

Get cart URL:

$cart_url = wc_get_cart_url();

Access the cart contents:

$cart = WC()->cart->get_cart();

Empty the cart:


In conclusion

Hopefully, you’ve found a useful snippet here or at least got an idea of how (relatively) simple it can be to work with WooCommerce products, carts and orders.

If you are looking for custom WooCommerce plugin development or integrations, don’t hesitate to get in touch.