Created
August 21, 2021 13:37
-
-
Save bagerathan/e3f4093d30b3dab5671fab096060e1f2 to your computer and use it in GitHub Desktop.
[Edit checkout form] #woo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//source: https://www.cloudways.com/blog/how-to-edit-delete-fields-and-email-in-woocommerce-custom-checkout-fields/ | |
//remove first name and last name. | |
function woocommerce_remove_additional_information_checkout($fields){ | |
unset( $fields["billing_first_name"] ); | |
unset( $fields["billing_last_name"] ); | |
return $fields; | |
} | |
add_filter( 'woocommerce_billing_fields', 'woocommerce_remove_additional_information_checkout' ); | |
//add a new field | |
function cloudways_custom_checkout_fields($fields){ | |
$fields['cloudways_extra_fields'] = array( | |
'cloudways_text_field' => array( | |
'type' => 'text', | |
'required' => true, | |
'label' => __( 'Input Text Field' ) | |
), | |
'cloudways_dropdown' => array( | |
'type' => 'select', | |
'options' => array( 'first' => __( 'First Option' ), 'second' => __( 'Second Option' ), 'third' => __( 'Third Option' ) ), | |
'required' => true, | |
'label' => __( 'Dropdown field' ) | |
) | |
); | |
return $fields; | |
} | |
add_filter( 'woocommerce_checkout_fields', 'cloudways_custom_checkout_fields' ); | |
function cloudways_extra_checkout_fields(){ | |
$checkout = WC()->checkout(); ?> | |
<div class="extra-fields"> | |
<h3><?php _e( 'Additional Fields' ); ?></h3> | |
<?php | |
foreach ( $checkout->checkout_fields['cloudways_extra_fields'] as $key => $field ) : ?> | |
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?> | |
<?php endforeach; ?> | |
</div> | |
<?php } | |
add_action( 'woocommerce_checkout_after_customer_details' ,'cloudways_extra_checkout_fields' ); | |
//save newly added field | |
function cloudways_save_extra_checkout_fields( $order_id, $posted ){ | |
// don't forget appropriate sanitization if you are using a different field type | |
if( isset( $posted['cloudways_text_field'] ) ) { | |
update_post_meta( $order_id, '_cloudways_text_field', sanitize_text_field( $posted['cloudways_text_field'] ) ); | |
} | |
if( isset( $posted['cloudways_dropdown'] ) && in_array( $posted['cloudways_dropdown'], array( 'first', 'second', 'third' ) ) ) { | |
update_post_meta( $order_id, '_cloudways_dropdown', $posted['cloudways_dropdown'] ); | |
} | |
} | |
add_action( 'woocommerce_checkout_update_order_meta', 'cloudways_save_extra_checkout_fields', 10, 2 ); | |
//use newly added form field data | |
function cloudways_display_order_data( $order_id ){ ?> | |
<h2><?php _e( 'Extra Information' ); ?></h2> | |
<table class="shop_table shop_table_responsive additional_info"> | |
<tbody> | |
<tr> | |
<th><?php _e( 'Input Text Field:' ); ?></th> | |
<td><?php echo get_post_meta( $order_id, '_cloudways_text_field', true ); ?></td> | |
</tr> | |
<tr> | |
<th><?php _e( 'Drop Down Field:' ); ?></th> | |
<td><?php echo get_post_meta( $order_id, '_cloudways_dropdown', true ); ?></td> | |
</tr> | |
</tbody> | |
</table> | |
<?php } | |
add_action( 'woocommerce_thankyou', 'cloudways_display_order_data', 20 ); | |
add_action( 'woocommerce_view_order', 'cloudways_display_order_data', 20 ); | |
//display in admin area | |
function cloudways_display_order_data_in_admin( $order ){ ?> | |
<div class="order_data_column"> | |
<h4><?php _e( 'Additional Information', 'woocommerce' ); ?><a href="#" class="edit_address"><?php _e( 'Edit', 'woocommerce' ); ?></a></h4> | |
<div class="address"> | |
<?php | |
echo '<p><strong>' . __( 'Text Field' ) . ':</strong>' . get_post_meta( $order->id, '_cloudways_text_field', true ) . '</p>'; | |
echo '<p><strong>' . __( 'Dropdown' ) . ':</strong>' . get_post_meta( $order->id, '_cloudways_dropdown', true ) . '</p>'; ?> | |
</div> | |
<div class="edit_address"> | |
<?php woocommerce_wp_text_input( array( 'id' => '_cloudways_text_field', 'label' => __( 'Some field' ), 'wrapper_class' => '_billing_company_field' ) ); ?> | |
<?php woocommerce_wp_text_input( array( 'id' => '_cloudways_dropdown', 'label' => __( 'Another field' ), 'wrapper_class' => '_billing_company_field' ) ); ?> | |
</div> | |
</div> | |
<?php } | |
add_action( 'woocommerce_admin_order_data_after_order_details', 'cloudways_display_order_data_in_admin' ); | |
function cloudways_save_extra_details( $post_id, $post ){ | |
update_post_meta( $post_id, '_cloudways_text_field', wc_clean( $_POST[ '_cloudways_text_field' ] ) ); | |
update_post_meta( $post_id, '_cloudways_dropdown', wc_clean( $_POST[ '_cloudways_dropdown' ] ) ); | |
} | |
add_action( 'woocommerce_process_shop_order_meta', 'cloudways_save_extra_details', 45, 2 ); | |
//add data to order emails | |
function cloudways_email_order_meta_fields( $fields, $sent_to_admin, $order ) { | |
$fields['instagram'] = array( | |
'label' => __( 'Some field' ), | |
'value' => get_post_meta( $order->id, '_cloudways_text_field', true ), | |
); | |
$fields['licence'] = array( | |
'label' => __( 'Another field' ), | |
'value' => get_post_meta( $order->id, '_cloudways_dropdown', true ), | |
); | |
return $fields; | |
} | |
add_filter('woocommerce_email_order_meta_fields', 'cloudways_email_order_meta_fields', 10, 3 ); | |
//customize emails | |
function cloudways_show_email_order_meta( $order, $sent_to_admin, $plain_text ) { | |
$cloudways_text_field = get_post_meta( $order->id, '_cloudways_text_field', true ); | |
$cloudways_dropdown = get_post_meta( $order->id, '_cloudways_dropdown', true ); | |
if( $plain_text ){ | |
echo 'The value for some field is ' . $cloudways_text_field . ' while the value of another field is ' . $cloudways_dropdown; | |
} else { | |
echo '<p>The value for <strong>input text field</strong> is ' . $cloudways_text_field. ' while the value of <strong>drop down</strong> is ' . $cloudways_dropdown . '</p>'; | |
} | |
} | |
add_action('woocommerce_email_customer_details', 'cloudways_show_email_order_meta', 30, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment