I have a function that is generating a unique string. This is currently working. However it is running for all products that are sold at checkout. I would like the following function to only run if a specific product is in the order Items.
Here is what I have so far:
add_action( 'woocommerce_order_status_processing', 'add_unique_id' );
function add_unique_id($order_id) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item_id => $product ) {
$str = "";
$length= 45;
$characters = array_merge(range('A','Z'), range('a','z'), range('0','9'));
$max = count($characters) - 1;
for ($i = 0; $i < $length; $i++) {
$rand = mt_rand(0, $max);
$str .= $characters[$rand];
}
wc_add_order_item_meta($item_id, 'Member Number', $str);
}
}