Generate code at checkout

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #136124
    Shawn
    Participant

    Good afternoon,

    I am setting up a room reservation system which uses door codes to get in. Is there a feature with WooCommerce where I can have it generate a custom code and then have it sent in a custom email? Thank you.

    #136285
    Moshtafizur
    Participant

    Hi there,

    Thanks for reaching out.

    I have escalated this with our development team. They will back to you as soon as possible.

    Kind regards.

    #136303
    Tom Anbinder
    Moderator
    Plugin Support

    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.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.