jimboobrien
4/22/2014 - 2:28 PM

Add Sales Reps to WooCommerce

Add Sales Reps to WooCommerce

//Add to funtctions.php file
//Adding Sales reps to the check out
add_action('woocommerce_after_order_notes', 'jmg_custom_checkout_field');

function jmg_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h2>'.__('Who Is Your EdiPure Sales Rep?').'</h2>';

    woocommerce_form_field( 'edipure_reps', array(
        'type'          	=> 'select',
        'label'        		=> __('EdiPure Sales Rep'),
        'placeholder'       => __( 'EdiPure Sales Rep', 'woocommerce' ),
		'required'          => true,
		'class'             => array( 'form-row-wide', 'address-field' ),
		'clear'             => true,
		'options'			=> array(
			'None'		=> 'None', 
			'Danny'		=> 'Danny', 
			'David'		=> 'David',
			'Andrea'	=> 'Andrea',
			'Eitan'		=> 'Eitan',
			'Other'		=> 'Other'
			),
        ), $checkout->get_value( 'edipure_reps' ));

    echo '</div>';

}

/**
 * Process the checkout
 **/
add_action('woocommerce_checkout_process', 'jmg_custom_checkout_field_process');

function jmg_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if (!$_POST['edipure_reps'])
         $woocommerce->add_error( __('Please select an EdiPure sales rep.') );
}
/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'jmg_custom_checkout_field_update_order_meta');

function jmg_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['edipure_reps']) update_post_meta( $order_id, 'EdiPure Sales Reps', esc_attr($_POST['edipure_reps']));
}
/**
 * Display field value on the order edition page
 **/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'jmg_custom_checkout_field_display_admin_order_meta', 10, 1 );
 
function jmg_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('EdiPure Sales Reps').':</strong> ' . $order->order_custom_fields['EdiPure Sales Reps'][0] . '</p>';
}