John Davidson

php - Custom checkout billing field data from Woocommerce isn't showing on admin order page and order email

0 comments
Message:


UPDATE:
I used the following code from here and it is fixed. The custom billing field is showing up in the backend and e-mails. Seemed like I needed to save the data to meta. Strange this isn't described on woocommerce's own documentation. Hope some else benefits from it too.


I've added some custom date fields to my checkout page. They are showing and I can input dates. But the input isn't showing in my admin order page or in de emails. I've searched some topics, but can't figure it out. I've followed this instruction under 'Adding Custom Shipping And Billing Fields'. I only see the label at the oder edit page and not the value. What is wrong here?


I only tried it with de 'date_from' field for testing purpose. 'date_till' also needs to be shown.


Code I got:


// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_new_checkout_fields_date_from' );

// Our hooked in function – $fields is passed via the filter!
function custom_new_checkout_fields_date_from( $newDateFrom ) {
$newDateFrom['billing']['date_from'] = array(
'type' => 'date',
'label' => __('Datum huur vanaf', 'woocommerce'),
'placeholder' => _x('Datum huur vanaf', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);

return $newDateFrom;
}

// Datum tot veld
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_new_checkout_fields_date_till' );

// Our hooked in function – $fields is passed via the filter!
function custom_new_checkout_fields_date_till( $newDateTill ) {
$newDateTill['billing']['date_till'] = array(
'type' => 'date',
'label' => __('Datum huur tot', 'woocommerce'),
'placeholder' => _x('Datum huur tot', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);

return $newDateTill;
}

/**
* Display field value on the order edit page
*/

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Datum huur van').':</strong> ' . get_post_meta( $order->get_id(), '_date_from', true ) . '</p>';
}

/**
* Add a custom field (in an order) to the emails
*/
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );

function custom_woocommerce_email_order_meta_fields( $keys, $sent_to_admin, $order ) {
$keys['_date_from'] = array(
'label' => __( 'Datum huur vanaf' ),
'value' => get_post_meta( $order->id, '_date_from', true ),
);
return $keys;
}


My Answer:
To retrieve custom checkout billing field data from Woocommerce in PHP, you can use the following code snippet:

php
// Get the billing fields data
$billing_first_name = isset( $_POST['billing_first_name'] ) ? sanitize_text_field( $_POST['billing_first_name'] ) : '';
$billing_last_name = isset( $_POST['billing_last_name'] ) ? sanitize_text_field( $_POST['billing_last_name'] ) : '';
$billing_address = isset( $_POST['billing_address'] ) ? sanitize_text_field( $_POST['billing_address'] ) : '';
$billing_city = isset( $_POST['billing_city'] ) ? sanitize_text_field( $_POST['billing_city'] ) : '';
$billing_state = isset( $_POST['billing_state'] ) ? sanitize_text_field( $_POST['billing_state'] ) : '';
$billing_postcode = isset( $_POST['billing_postcode'] ) ? sanitize_text_field( $_POST['billing_postcode'] ) : '';
$billing_country = isset( $_POST['billing_country'] ) ? sanitize_text_field( $_POST['billing_country'] ) : '';

// Output the billing fields data
echo 'Billing First Name: ' . $billing_first_name . '
';
echo 'Billing Last Name: ' . $billing_last_name . '
';
echo 'Billing Address: ' . $billing_address . '
';
echo 'Billing City: ' . $billing_city . '
';
echo 'Billing State: ' . $billing_state . '
';
echo 'Billing Postcode: ' . $billing_postcode . '
';
echo 'Billing Country: ' . $billing_country . '
';


This code snippet retrieves the custom checkout billing field data from the Woocommerce checkout page and sanitizes the input to prevent any malicious code injection. You can then use this data for further processing or display it on the checkout page.

Rate this post

3 of 5 based on 5166 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us