4
votes

I'm working in WordPress 4.3.1., with WooCommerce 2.4.7. For my employer's current store project, we need to have unique IDs generated for each item purchased, which can never be the same as any other purchase.

For example, if someone buys 3 shirts of the same design and size, each of these shirts, once in the cart, should each have a unique item ID displayed. Regardless of the number of orders we get, the unique item IDs cannot be duplicated.

As for the ID, I was considering mixing the product SKU with the date, and possibly the order ID to generate the date. How exactly would I go about this? Is there a plugin that can handle this, or should I deal in straight PHP?

2

2 Answers

5
votes

I assume you want to use the unique id for work order tracking internally use this hook (I am using processing rather then completed)

add_action( 'woocommerce_order_status_processing', 'add_unique_id' );

Then do this

function add_unique_id($order_id) {
        $order = new WC_Order( $order_id );
        $items = $order->get_items(); 
        foreach ($items as $item_id => $product ) {
          $gen_id = "generate id goes here"; 
          wc_add_order_item_meta($item_id, 'unique_id', $gen_id);
        }
2
votes

You could use a WooCommerce hook like woocommerce_order_status_completed in order to run something after every purchase.

add_action( 'woocommerce_order_status_completed', 'custom_function' );

function custom_function($order_id) {
    // completed order object (can be useful)
    $order = new WC_Order( $order_id );
    // run what you want here
}

I can suggest you to use a post meta or an user meta (based on your needs).