Skip to content

Instantly share code, notes, and snippets.

@eder-dias
Last active November 3, 2017 05:16
Show Gist options
  • Save eder-dias/1d74c7e98477531b4bb67a9f04656ccd to your computer and use it in GitHub Desktop.
Save eder-dias/1d74c7e98477531b4bb67a9f04656ccd to your computer and use it in GitHub Desktop.
Adicionar campo checkout
//Adicionar campo no checkout e nova coluna no painel admin
/*Add the field to the checkout*/
add_action('woocommerce_before_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>'.__('Como nos conheceu?').'</h3>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'required' => true,
'label' => __('Escolha uma opção'),
'placeholder' => __('Enter something'),
'options' => array(
'' => __(''),
'pelo Google' => __('Pelo Google', 'woocommerce' ),
'pelo Facebook' => __('Pelo Facebook', 'woocommerce' ),
'pelo blog Site da Mamãe' => __('Pelo blog Site da Mamãe', 'woocommerce' ),
'por indicação' => __('Por indicação', 'woocommerce' )
)
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
/*Process the checkout*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error.
if (!$_POST['my_field_name'])
$woocommerce->add_error( __('Please enter something into this new shiny field.') );
}
/*Update the order meta with field value*/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['my_field_name']) update_post_meta( $order_id, 'Como nos conheceu?', esc_attr($_POST['my_field_name']));
}
//Define columns position and names
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['MY_COLUMN_ID_1'] = 'Como nos conheceu?';
//stop editing
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
//Show values
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION($column){
global $woocommerce;
$data = get_post_meta( $order_id );
$columns = array();
$columns["MY_COLUMN_ID_1"] = __( 'my_field_name', 'woocommerce' );
//stop editing
}
//make the columns sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns ) {
$custom = array(
//start editing
'MY_COLUMN_ID_1' => 'MY_COLUMN_1_POST_META_ID',
//stop editing
);
return wp_parse_args( $custom, $columns );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment