add and order checkout fields

PHOTO EMBED

Mon Aug 22 2022 07:41:50 GMT+0000 (Coordinated Universal Time)

Saved by @itaiki

*Checkout Fields*/
//Remove Postcode and state
add_filter( 'woocommerce_checkout_fields', 'misha_remove_fields', 9999 );

function misha_remove_fields( $checkout_fields ) {
	
	// she wanted me to leave these fields in checkout
	// unset( $checkout_fields[ 'billing' ][ 'billing_first_name' ] );
	// unset( $checkout_fields[ 'billing' ][ 'billing_last_name' ] );
	// unset( $checkout_fields[ 'billing' ][ 'billing_phone' ] );
	// unset( $checkout_fields[ 'billing' ][ 'billing_email' ] );
	// unset( $checkout_fields[ 'order' ][ 'order_comments' ] ); // remove order notes
	
	// and to remove the billing fields below
	unset( $checkout_fields[ 'billing' ][ 'billing_state' ] ); // remove state field
	unset( $checkout_fields[ 'billing' ][ 'billing_postcode' ] ); // remove zip code field

	return $checkout_fields;
}
//Add Leaving near the door.
add_filter('woocommerce_checkout_fields', 'my_custom_checkout_field');
function my_custom_checkout_field( $fields )
{
    $fields['billing']['billing_door'] = array(
        'label'       => __('ניתן להשאיר ליד דלת הדירה', 'woocommerce'), // Add custom field label
        'placeholder' => __('האם ניתן להשאיר את החבילה ליד הדלת?', 'woocommerce'), // Add custom field placeholder
        'required'    => false, // if field is required or not
        'clear'       => false, // add clear or not
        'type'        => 'checkbox', // add field type
        'class'       => array( 'billing_door', 'form-row-wide' ), // array only, read more about classes and styling in the previous step
        'priority'    => 140, // Priority sorting option
    );

    return $fields;
}
/**
 * Update the user meta with field value
 **/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta_door');

function my_custom_checkout_field_update_user_meta_door( $user_id ) {
	if ($user_id && $_POST['billing_door']) update_user_meta( $user_id, 'billing_door', esc_attr($_POST['billing_door']) );
}
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_door');

function my_custom_checkout_field_update_order_meta_door( $order_id ) {
	if ($_POST['billing_door']) update_post_meta( $order_id, 'השארת החבילה ליד הדלת', esc_attr($_POST['billing_door']));
}
/**
 * Add the field to order emails
*/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys_door');

function my_custom_checkout_field_order_meta_keys_door( $keys ) {
	$keys[] = 'ניתן להשאיר ליד דלת הדירה';
	return $keys;
} 
// add to meta admin
add_filter( 'woocommerce_customer_meta_fields', 'misha_admin_address_field_door' );
function misha_admin_address_field_door( $admin_fields ) {
	
	$admin_fields[ 'billing' ][ 'fields' ][ 'billing_door' ] = array(
		'label' => 'ניתן להשאיר ליד דלת הדירה',
		'description' => 'האם ניתן להשאיר ליד הדלת',
	);
	return $admin_fields;
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'show_new_checkout_field_order', 10, 1 );
   
function show_new_checkout_field_order( $order ) {    
   $order_id = $order->get_id();
   
   if ( get_post_meta( $order_id, 'billing_door', true ) ) echo '<p><strong>ניתן להשאיר ליד הדלת:</strong> ' . get_post_meta( $order_id, 'billing_door', true ) . '</p>';
}


//Add Billing Code
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields( $fields )
{
    $fields['billing']['billing_kod'] = array(
        'label'       => __('קוד כניסה', 'woocommerce'), // Add custom field label
        'placeholder' => __('1234 דג מלוח', 'woocommerce'), // Add custom field placeholder
        'required'    => false, // if field is required or not
        'clear'       => false, // add clear or not
        'type'        => 'text', // add field type
        'class'       => array( 'billing_kod', 'form-row-wide' ), // array only, read more about classes and styling in the previous step
        'priority'    => 90, // Priority sorting option
    );

    return $fields;
}
/**
 * Update the user meta with field value
 **/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');

function my_custom_checkout_field_update_user_meta( $user_id ) {
	if ($user_id && $_POST['billing_kod']) update_user_meta( $user_id, 'billing_kod', esc_attr($_POST['billing_kod']) );
}
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['billing_kod']) update_post_meta( $order_id, 'קוד כניסה', esc_attr($_POST['billing_kod']));
}

/**
 * Add the field to order emails
*/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');

function my_custom_checkout_field_order_meta_keys( $keys ) {
	$keys[] = 'קוד כניסה';
	return $keys;
} 
// add to meta admin
add_filter( 'woocommerce_customer_meta_fields', 'misha_admin_address_field' );
function misha_admin_address_field( $admin_fields ) {
	
	$admin_fields[ 'billing' ][ 'fields' ][ 'billing_kod' ] = array(
		'label' => 'קוד כניסה',
		'description' => 'קוד כניסה יהיה כאן',
	);
	return $admin_fields;
	
}

/* End Of custom fields for woocommerce checkout order fields.*/
content_copyCOPY