Skip to content

Instantly share code, notes, and snippets.

@jillmugge
Last active February 5, 2019 18:11
Show Gist options
  • Save jillmugge/11181349 to your computer and use it in GitHub Desktop.
Save jillmugge/11181349 to your computer and use it in GitHub Desktop.
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>';
}
@xanicblanc
Copy link

i like this code, one problem. i get this problem and i did a test run and chose a sales rep i cannot see it on the page

Warning: Illegal string offset 'EdiPure Sales Reps' in /home/********/public_html/wp-content/themes/replete/functions.php on line 366

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment