I need to replace some character in my custom checkout field.
this is the whole code of my custom checkout field , (maybe we could use str_replace here)
/* Add the field to the checkout */
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'phone_sabet', array(
'type' => 'tel',
'required' => true,
'clear' => true,
'class' => array('my-field-class form-row-first'),
'label' => __(''),
'placeholder' => __(''),
'description' => '',
), $checkout->get_value(('phone_sabet')));
echo '</div>';
}
this is the part of code when the custom field going to update
/* 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 ( ! empty( $_POST['phone_sabet'] ) ) {
update_post_meta( $order_id, 'Phone', sanitize_text_field( $_POST['phone_sabet'] ) );
}
}
i tired to use str_replace and change it to below but no luck.
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 ( ! empty( $_POST['phone_sabet'] ) ) {
update_post_meta( $order_id, 'Phone', sanitize_text_field( $_POST['phone_sabet'] ) );
$getMeta = get_post_meta( get_the_ID(), 'Phone', true);
$newMeta = str_replace(array('۱'), '1', $getMeta);
update_post_meta(get_the_ID(), 'Phone', $newMeta);
}
}
and this is the part of when checkout field going to process. its okay if we could done it with str_replace here.
/* Process the checkout */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
if ( $_POST['phone_sabet'] )
// do something
}
The correct hook is woocommerce_checkout_update_order_meta, so you could try this:
## Save the order meta with custom field value
add_action( 'woocommerce_checkout_update_order_meta', 'custom_update_order_meta' );
function custom_update_order_meta( $order_id ) {
if ( ! empty( $_POST['phone_sabet'] ) ) {
// Replace before saving translating )
$phone_sabet = str_replace( array('۱'), array('1'), $_POST['phone_sabet'] );
update_post_meta( $order_id, 'phone', sanitize_text_field( $phone_sabet ) );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works
Related
I've added a number field to the checkout page but it doesn't update the totals when selected and allow the order to be placed.
add_action( 'woocommerce_after_checkout_billing_form', 'donation_field' );
function donation_field( $checkout ) {
woocommerce_form_field( 'donation_amount', array(
'type' => 'number',
'required' => true,
'label' => 'Donate',
'description' => 'Please enter a donation amount',
), $checkout->get_value( 'donation_amount' ) );
}
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 ( ! empty( $_POST['donation_amount'] ) ) {
update_post_meta( $order_id, 'donation_amount', sanitize_text_field( $_POST['donation_amount'] ) );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee', 10, 1 );
function add_custom_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$fee = WC()->checkout->get_value('donation_amount');
$cart->add_fee( __( 'Donate Value', 'woocommerce' ) , $fee, false );
}
I have a similar like in here: I don't understand why the value of this custom field is not saved. Here's my code:
add_filter( 'woocommerce_checkout_fields' , 'altri_campi' );
function altri_campi( $fields ) {
$fields['billing']['codice_fiscale'] = array(
'class' => array('form-row-wide'),
'label' => __('Codice Fiscale', 'woocommerce'),
'placeholder' => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide')
);
return $fields;
}
// like LoizTheAztec above
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['codice_fiscale'] ) ) {
update_post_meta( $order_id, 'Codice Fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );
}
}
// then I'm expecting that custom field value will be saved somehow, but won't
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>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_codice_fiscale', true ) . '</p>';
}
Meta field is correctly saved and printed, but I cannot add the custom field value in the order data view.
What I am doing wrong and how to display this custom field value in the Order edit view?
Otherwise the text I expect to find after Customer's billing and shipping data is void, since I read only the html part of the latest snippet.
Here is the correct commented and explained code:
// Creating and displaying the custom checkout field in checkout page
add_filter( 'woocommerce_checkout_fields' , 'altri_campi' );
function altri_campi( $fields ) {
$fields['billing']['codice_fiscale'] = array(
'class' => array('form-row-wide'),
'label' => __('Codice Fiscale', 'woocommerce'),
'placeholder' => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide')
);
return $fields;
}
// Saving the custom checkout field value in the order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['codice_fiscale'] ) ){
update_post_meta( $order_id, 'codice_fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );
// get the customer ID
$customer_id = get_post_meta( $order_id, '_customer_user', true );
// Update customer user data
update_user_meta( $customer_id, 'codice_fiscale', true );
}
}
// Displaying the custom checkout field value in the order edit page (backend)
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
$codice_fiscale = get_post_meta( $order->get_id(), 'codice_fiscale', true );
if( ! empty( $codice_fiscale ))
echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . $codice_fiscale . '</p>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. You will get in backend order edit page something like (below addresses):
I added a custom field on the woocommerce checkout page that is populated by URL but I am struggling to find a way to make this field not editable.
I added the following code to function.php:
//Add custom field
function custom_woocommerce_checkout_fields( $checkout_fields = array() ) {
$checkout_fields['order']['imei'] = array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('IMEI'),
'placeholder' => __('imei'),
'default' => $_GET['imei'],
);
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_woocommerce_checkout_fields' );
What should I change in my code to accomplish this ?
Try this code. It work for me. Just need to add custom attribute parameter to the fields array.
//Display custom field
function custom_woocommerce_checkout_fields( $checkout_fields = array() ) {
$checkout_fields['order']['imei'] = array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('IMEI'),
'placeholder' => __('imei'),
'default' => $_GET['imei'],
'custom_attributes' => array( 'disabled' => true)
);
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_woocommerce_checkout_fields' );
// Save custom field
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['imei'] ) ) {
update_post_meta( $order_id, '_imei', sanitize_text_field( $_POST['imei'] ) );
}
}
You should try this text <imput> field with the readonly property (meaning not editable).
You should need to have your 'emei' set in the checkout url like:
http://www.example.com/checkout/?imei=3545454653 to make the field appear with the value as I have this condition: if( empty($_GET['imei'])) return;set in the function.
The code:
// Display
add_action( 'woocommerce_after_order_notes', 'custom_woocommerce_checkout_fields' );
function custom_woocommerce_checkout_fields( $checkout ) {
// Only display field if the 'emei' is set in the checkout url
if( empty($_GET['imei'])) return;
echo '<p class="form-row my-field-class form-row-wide woocommerce-validated" id="imei_field">
<label for="imei" class="">'.__('IMEI').'</label>
<input type="text" class="input-text " name="imei" id="imei" placeholder="'.__('IMEI').'" value="'.$_GET['imei'].'" readonly>
</p>';
}
// Save
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['imei'] ) ) {
update_post_meta( $order_id, '_imei', sanitize_text_field( $_POST['imei'] ) );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and work on WooCommerce versions 3+
To get the value (for a defined $order_id):
$imei = get_post_meta( $order_id, '_imei', true );
This question already has an answer here:
Admin product pages custom field displayed in Cart and checkout
(1 answer)
Closed 5 years ago.
I have a seemingly simple task, that I can't, for the life of me, seem to figure out.
I have a custom field on the admin page of my Woocommerce products. I just need the value I set here to display in the Woocommerce Checkout page and email.
I have managed to get the field to display on the product page using the following code:
add_action( 'woocommerce_product_options_general_product_data', 'create_shipdate_custom_field' );
function create_shipdate_custom_field() {
woocommerce_wp_text_input( array(
'id' => '_shipdate',
'type' => 'text',
'label' => __('Shipping Date', 'woocommerce' ),
'description' => '',
'desc_tip' => 'true',
'placeholder' => __('i.e. 16 December 2017', 'woocommerce' ),
) );
}
// save the data value from this custom field on product admin tab
add_action( 'woocommerce_process_product_meta', 'save_shipdate_custom_field' );
function save_shipdate_custom_field( $post_id ) {
$wc_text_field = $_POST['_shipdate'];
if ( !empty($wc_text_field) ) {
update_post_meta( $post_id, '_shipdate', esc_attr( $wc_text_field ) );
}
}
function product_date() {
echo get_post_meta( get_the_ID(), '_shipdate', true );
}
add_action('woocommerce_single_product_summary', 'product_date', 30);
I am able to get static text to display on the checkout page, using the following code, but it just won't work with my custom field:
function emaildate() { echo "This will display fine"; }
add_action('woocommerce_order_item_meta_start', 'emaildate', 10, 1);
Thanks in advance for the help!
You are missing a couple of steps to have your custom value display on the cart/checkout pages, namely, storing the custom field in the cart and in the order as meta data. Here is the full code which should work as expected:
// create the custom field on product admin tab
add_action( 'woocommerce_product_options_general_product_data', 'create_shipping_custom_field' );
function create_shipping_custom_field() {
// Create a custom text field
woocommerce_wp_text_input( array(
'id' => '_shipdate',
'type' => 'text',
'label' => __('Shipping Date', 'woocommerce' ),
'description' => '',
'desc_tip' => 'true',
'placeholder' => __('i.e. 16 December 2017', 'woocommerce' ),
) );
}
// save the data value from this custom field on product admin tab
add_action( 'woocommerce_process_product_meta', 'save_shipping_custom_field' );
function save_shipping_custom_field( $post_id ) {
$wc_text_field = $_POST['_shipdate'];
if ( !empty($wc_text_field) ) {
update_post_meta( $post_id, '_shipdate', esc_attr( $wc_text_field ) );
}
}
// Store custom field in Cart
add_action( 'woocommerce_add_cart_item_data', 'store_shipping_custom_field', 10, 2 );
function store_shipping_custom_field( $cart_item_data, $product_id ) {
$ship_date = get_post_meta( $product_id , '_shipdate', true );
if( !empty($ship_date) ) {
$cart_item_data[ '_shipdate' ] = $ship_date;
$cart_item_data['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'date_shipping', $ship_date );
}
return $cart_item_data;
}
// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {
$custom_items = array();
// Woo 2.4.2 updates
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['_shipdate'] ) ) {
$custom_items[] = array( "name" => __( "Shipping Date", "woocommerce" ), "value" => $cart_item['_shipdate'] );
}
return $custom_items;
}
// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_shipping_to_order_item_meta', 1, 3 );
function add_shipping_to_order_item_meta( $item_id, $values, $cart_item_key ) {
$prod_id = wc_get_order_item_meta( $item_id, '_product_id', true );
$shipdate = get_post_meta( $prod_id, '_shipdate', true );
wc_add_order_item_meta( $item_id, 'Shipping Date', $shipdate, true );
}
I'm new to WooCommerce.I'm unable to figure out where is problem here is my code
I have added a select field in billing form of checkout page.
Problem
records are not saving or updating on submitting. Problem is in Update the order meta with field value.value is not updating in database
// checkout page customization start
global $post, $woocommerce;
// Account select field
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields($fields) {
$fields['billing']['Account'] = array(
'type' => 'select',
'class' => array('billing form-row-wide'),
'label' => __('Choose an Account'),
'placeholder' => _x('Account', 'placeholder', 'woocommerce'),
'options' => array(
'' => __( 'Select Account','' ),
),
'required' => true,
);
return $fields;
}
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
if (!$_POST['Account'])
$woocommerce->add_error( __('Please enter your Account.'.$_POST['Account']));
}
///**
Problem area
value is not updating in database
//* 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['Account']) update_post_meta( $order_id, 'Account', esc_attr($_POST['Account']));
}
/**
* 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['Account']) update_user_meta( $user_id, 'Account', esc_attr($_POST['Account']) );
}
The functions look pretty accurate to me. Are you sure they aren't updating? Or perhaps you are having difficulty is displaying them.
I've tweaked them a bit. First, to use the $posted variabled that WooCommerce sends to the function, though this is trivial as $_POST should be the same. And secondly, you are using esc_attr() when you should be using sanitize_text_field(). The former is for displaying the data in an HTML attribute while the latter is for sanitizing before saving.
//* Update the order meta with field value
//**/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order_id, $posted ) {
if ( isset( $posted['Account'] ) ){
update_post_meta( $order_id, 'Account', sanitize_text_field( $posted['Account'] ) );
}
}
/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta', 10, 2 );
function my_custom_checkout_field_update_user_meta( $user_id, $posted ) {
if ( $user_id && isset( $posted['Account'] ) ){
update_user_meta( $user_id, 'Account', sanitize_text_field( $posted['Account'] ) );
}
}