Hi, Shawn,
This PHP snippet will automatically generate a random 4-digit code (when order is processed on checkout) and save it in the door_code
order meta:
add_action( 'woocommerce_checkout_order_processed', function ( $order_id, $posted_data, $order ) {
if ( '' !== $order->get_meta( 'door_code' ) ) {
return; // make sure we don't overwrite order's existing door code
}
$door_code = sprintf( '%04d', rand( 0, 9999 ) );
$order->update_meta_data( 'door_code', $door_code );
$order->save();
}, 9, 3 );
Then we can add it to the custom email with the [order_meta]
shortcode:
[order_meta key="door_code" before="Door code: "]
The shortcode should be added to the “Email content” option (in “WooCommerce > Settings > Emails > Custom email #X”).
P.S. When testing, I’ve set the “Triggers” option to the “New order (Any status)”.
Please give it a try and let me know what you think.