1
votes

I would appreciate any assistance with the following...

On my WooCommerce checkout page, I need to allow my customers to select their multiple preferred distributors and then have the new order emails sent out to myself and only the dealers they select.

I can't for the life of me figure out how to set it up using multi-checkboxes, which would allow them to select more than one (i.e. they select Distributor 1 and 3, and only the three of us receive the new order email).

I was able to figure it out using a dropdown menu (thanks to reading through a variety of posts within this community). Any help would be greatly appreciated! :)

Current code I was able to piece together (just a tiny bit proud of myself because it actually works):

// // Add custom checkout field
add_action( 'woocommerce_after_order_notes', 'sj_custom_checkout_field' );
function sj_custom_checkout_field( $checkout ) {
    echo '<div id="sj_custom_checkout_field"><h2>' . __('Distributor') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'      => 'select',
        'class'     => array('wps-drop'),
        'required'  => true, // Missing
        'options'   => array(
            ''          => __( 'Select Your Preferred Distributor', 'wps' ),
            'Distributor 1'   => __( 'Distributor 1', 'wps' ),
            'Distributor 2'      => __( 'Distributor 2', 'wps' ),
            'Distributor 3'    => __( 'Distributor 3', 'wps' ),
            'Distributor 4'    => __( 'Distributor 4', 'wps' ),
            'Distributor 5'    => __( 'Distributor 5', 'wps' )

        )
    ), $checkout->get_value( 'my_field_name' ) );
    echo '</div>';
}

// Process the checkout
add_action('woocommerce_checkout_process', 'sj_custom_checkout_field_process');
function sj_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( empty( $_POST['my_field_name'] ) )
        wc_add_notice( __( 'Please select your preferred distributor.' ), 'error' );
}

// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_update_order_meta', 'sj_custom_field_checkout_update_order_meta', 10, 1 );
function sj_custom_field_checkout_update_order_meta( $order_id ) {

    if ( ! empty( $_POST['my_field_name'] ) )
        update_post_meta( $order_id, 'my_field_name', $_POST['my_field_name'] );
}

add_filter( 'woocommerce_email_recipient_new_order',  'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {
    if( is_admin() ) return $recipient;

    // Get the order ID (Woocommerce retro compatibility)
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;


    // Get the custom field value (with the right $order_id)
    $my_field_name = get_post_meta( $order_id, 'my_field_name', true );


    if ($my_field_name == "Distributor 1")
        $recipient .= ',[email protected]';
    elseif ($my_field_name == "Distributor 2")
        $recipient .= ',[email protected]';
    elseif ($my_field_name == "Distributor 3")
        $recipient .= ',[email protected]';
    elseif ($my_field_name == "Distributor 4")
        $recipient .= ',[email protected]';
     elseif ($my_field_name == "Distributor 5")
        $recipient .= ',[email protected]';

    return $recipient;
}
1
You're better off with type="radio" and then use css to make it look like checkboxes. - Reigel

1 Answers

0
votes

Alternatively, select and radio type on has same args, you can change your select to radio. Use css to make radio looks like a checkboxes if you really need the style to look like checkbox.

woocommerce_form_field( 'my_field_name', array(
    'type'      => 'radio',
    'class'     => array('wps-drop'),
    'required'  => true, // Missing
    'options'   => array(
        ''          => __( 'Select Your Preferred Distributor', 'wps' ),
        'Distributor 1'   => __( 'Distributor 1', 'wps' ),
        'Distributor 2'      => __( 'Distributor 2', 'wps' ),
        'Distributor 3'    => __( 'Distributor 3', 'wps' ),
        'Distributor 4'    => __( 'Distributor 4', 'wps' ),
        'Distributor 5'    => __( 'Distributor 5', 'wps' )

    )
), $checkout->get_value( 'my_field_name' ) );