In Woocommerce, I have added a custom field to the checkout page using the following code:
add_action( 'woocommerce_checkout_fields', 'woo_add_conditional_checkout_fields' );
function woo_add_conditional_checkout_fields( $fields ) {
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
$fields['billing']['billing_field_newfield'] = array(
'label' => __('New Field', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => false
);
}
return $fields;
}
Now, I would like to include the value that user enters in this field in the 'New Order' woocommerce email subject using {billing_field_newfield} custom placeholder.
However when I go to Woocommerce > Settings > Emails > New Order and put {billing_field_newfield} in the subject, I just get {billing_field_newfield} in the email and not its actual value.
How to add a custom dynamic placeholder to email subject in Woocommerce?
To add a custom active placeholder {billing_field_newfield} in woocommerce email notifications, you will use the following hooked function.
You will check before that _billing_field_newfield is the correct post meta key used to save the checkout field value to the order (check in wp_postmeta database table for the order post_id).
The code:
add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
function add_custom_email_format_string( $string, $email ) {
// The post meta key used to save the value in the order post meta data
$meta_key = '_billing_field_newfield';
// Get the instance of the WC_Order object
$order = $email->object;
// Get the value
$value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';
// Additional subject placeholder
$new_placeholders = array( '{billing_field_newfield}' => $value );
// Return the clean replacement value string for "{billing_field_newfield}" placeholder
return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}
Code goes in function.php file of your active child theme (or active theme). It will works.
Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {billing_field_newfield}…
Related
Is there a way to display my custom SKU under each product on the WooCommerce order page?
The custom sku displays fine when I edit the product, but it does not display in the order page for the product. I need this information to show on the order so that Zapier can match it with the Visma Account Software ArticleID of the product.
This attempt is based on the solution How to add a (second) custom sku field to WooCommerce products?
// Add Custom SKU Field
function my_add_custom_sku() {
$args = array(
'label' => __( 'ArticleID', 'woocommerce' ),
'placeholder' => __( 'Enter Visma ArticleID Here', 'woocommerce' ),
'id' => 'articleid',
'desc_tip' => true,
'description' => __( 'Visma ArticleID is for integration with Zapier.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'my_add_custom_sku' );
// Save
function my_save_custom_meta( $product ){
if( isset($_POST['articleid']) ) {
$product->update_meta_data( 'articleid', sanitize_text_field( $_POST['articleid'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'my_save_custom_meta', 10, 1 );
You could use the hook woocommerce_checkout_create_order_line_item to save this product custom field as custom order Item data, when order is placed, as follows (and display it everywhere on orders and emails):
// Save as custom order item meta data and display on orders and email notifications
add_action( 'woocommerce_checkout_create_order_line_item', 'add_articleid_on_orders_and_emails', 10, 4 );
function add_articleid_on_orders_and_emails( $item, $cart_item_key, $values, $order ) {
$articleid = $values['data']->get_meta('articleid'); // Get product "articleid"
// For product variations when the "articleid" is not defined
if ( empty($articleid) && $values['variation_id'] > 0 ) {
$product = wc_get_product( $values['product_id'] ); // Get the parent variable product
$articleid = $product->get_meta( 'articleid' ); // Get parent product "articleid"
}
if ( ! empty($articleid) ) {
$item->add_meta_data( 'articleid', $articleid ); // add it as custom order item meta data
}
}
And the following to change "articleid" displayed label slug with "ArticleID" readable label name (on customer orders and email notifications):
// Replace the label (slug) by a readable label name on orders and emails
add_filter( 'woocommerce_display_item_meta', 'filter_order_item_articleid_displayed_label', 100, 3 );
function filter_order_item_articleid_displayed_label( $html, $item, $args ) {
// Not on admin
if ( ! is_admin() && $item->get_meta('articleid') ) {
$html = str_replace('articleid', __('ArticleID', 'woocommerce'), $html);
}
return $html;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related to this thread:
Change order item displayed meta key label in WooCommerce admin order pages
Display a product custom field only in WooCommerce Admin single orders for Manual Orders
Change order item displayed meta key label in WooCommerce admin order pages
How do I set the value of a custom field on an order, upon checkout submit, to a selected value in an HTML component?
I've implemented a custom HTML component, inside the checkout form, whose selected value I want to insert in a custom field on my orders. But I can't seem to figure out how to hook these things up.
I've arrived at somthing like:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['order']['special_delivery'] = 'The value from HTML';
return $fields;
}
But I'm not even sure that's the right way to go.
Any ideas?
Using woocommerce_after_checkout_billing_form allows you to add custom fields to your checkout form. You also need to save the field in the order meta using woocommerce_checkout_update_order_meta.
In the following code there is a custom text field used for saving VAT ID. Feel free to get rid of the "mrank" prefixes. Also rename the field as you need it.
It will show you how to add a field and to save in order meta.
/**
* VAT Number in WooCommerce Checkout
*/
function mrank_vat_field( $checkout ) {
echo '<div id="mrank_vat_field">';
woocommerce_form_field( 'vat_number', array(
'type' => 'text',
'class' => array( 'vat-number-field form-row-wide') ,
'label' => __( 'VAT-ID' ),
'placeholder' => __( 'Enter number' ),
'description' => __( 'Please enter your VAT-ID' ),
'required' => true,
), $checkout->get_value( 'vat_number' ));
echo '</div>';
}
add_action( 'woocommerce_after_checkout_billing_form', 'mrank_vat_field' );
/**
* Save VAT Number in the order meta
*/
function mrank_checkout_vat_number_update_order_meta( $order_id ) {
if ( ! empty( $_POST['vat_number'] ) ) {
update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'mrank_checkout_vat_number_update_order_meta' );
In WooCommerce, at checkout, I am able to create or update all the user and billing information while placing an order. However, I want to set/update the user "display name" (alias) as well.
So based on "Capture custom checkout field value in Woocommerce" answer code, I have added the following code to my active theme's functions.php file:
// Display a custom checkout field
add_action( 'woocommerce_before_checkout_billing_form', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'display_name', array(
'type' => 'text',
'class' => array('my-custom-field form-row-wide'),
'label' => __('Alias'),
'placeholder' => __('Nickname to show in my account and comments'),
'required' => true,
), $checkout->get_value( 'display_name' ));
echo '</div>';
}
// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_update_user_meta', 'save_order_custom_meta_data', 10, 2 );
function save_order_custom_meta_data( ) {
if ( isset($_POST['display_name']) )
$user->update_meta_data('display_name', sanitize_text_field( $_POST['display_name'] ) );
}
But this is not working as I get "Internal Server Error" message.
Any help to solve this will be appreciated.
The problem comes from your second function… The function arguments are missing and $user variable is null and you can't use on it the WC_Data method update_meta_data().
Also the display_name is not user meta data, but simply user data. So you need to use dedicated WordPress function wp_update_user() in order to set/update the user display name.
Replace your 2nd hooked function with the following instead:
// Save/update user data from custom checkout field value
add_action( 'woocommerce_checkout_update_user_meta', 'checkout_update_user_display_name', 10, 2 );
function checkout_update_user_display_name( $customer_id, $data ) {
if ( isset($_POST['display_name']) ) {
$user_id = wp_update_user( array( 'ID' => $customer_id, 'display_name' => sanitize_text_field($_POST['display_name']) ) );
}
}
Code goes in function.php file of your active child theme (or active theme). It should works now.
Following the WooCommerce checkout fields customization docs:
Customizing checkout fields using actions and filters
I have added a custom field to woocommerce checkout page, through functions.php.
I'm worried if I have to sanitize user input for that custom field?
I think it doesn't need sanitization since it's passed into billing fields as in: $fields['billing'], is that correct?
If not how do I sanitize this custom field?
Creating this custom field is meant to accept text strings(latin) and integers combined no longer than 50 in length.
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
//Adding custom text field
$fields['billing']['billing_username'] = array(
'type' => 'text',
'label' => __('Your Username', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-first'),
'clear' => true
);
return $fields;
}
If you look to the related Official Documentation linked in your question, you've got this snippet:
/**
* 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['my_field_name'] ) ) {
update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
}
}
In your case you don't need that as address fields are already processed by Woocommerce.
For custom special fields: The answer is yes (which is not your case)
As you can see in this code they use sanitize_text_field() WordPress function, when saving the submitted data to database with update_post_meta() function…
This is only for custom checkout fields and not for existing checkout fields, that already get their own process…
I've created a custom field for warranty in my products pages, via function.php.
add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );
function test_custom_fields() {
// Print a custom text field
woocommerce_wp_text_input( array(
'id' => '_warranty',
'label' => 'i.e. 15 years',
'description' => '',
'desc_tip' => 'true',
'placeholder' => 'i.e. 15 years'
) );
}
add_action( 'woocommerce_process_product_meta', 'test_save_custom_fields' );
function test_save_custom_fields( $post_id ) {
if ( ! empty( $_POST['_warranty'] ) ) {
update_post_meta( $post_id, '_warranty', esc_attr( $_POST['_warranty'] ) );
}
}
I would like to "duplicate" this custom field with key and value, in an self-generated custom field on the admin order page depending on products in cart/order (without plugin).
So, with this custom field on order page, I will finally be able to display "warranty" in my pdf invoice with WooCommerce PDF Invoice plugin.
Another explanation :
As admin, I fill _warranty value in "product1" page
When "product1" is in an order, on the admin order view, I would like to see a custom field containing "_warranty + value" from product1 page.
So, as admin, I could set {{_warranty}} in WooCommerce PDF Invoice plugin to display "Warranty : 15 years"
Many thanks for your help.
I have just tested the following case: show product meta in order items table in Order Details
But this does not give me a custom field, so I couldn't get my {{_warranty}} value width it.
What I am doing wrong?
How can I achieve this?
Thanks.
First: "Duplicating this custom field with key and value, in an self-generated custom field on the admin order page" is not the good approach.
To achieve what you are expecting, you have missed just some little things. You need to:
Store custom field in Cart (when product is added to cart)
Render this on cart and checkout pages
Add the information in the order as meta data (to make it part of the order)
With point 3 you will be able to get this on WooCommerce PDF Invoice plugin to display "Warranty : 15 years".
So the code you need is:
// create the custom field on product admin tab
add_action( 'woocommerce_product_options_general_product_data', 'create_warranty_custom_field' );
function create_warranty_custom_field() {
// Create a custom text field
woocommerce_wp_text_input( array(
'id' => '_warranty',
'type' => 'text',
'label' => __('Warranty', 'woocommerce' ),
'description' => '',
'desc_tip' => 'true',
'placeholder' => __('i.e. 15 years', 'woocommerce' ),
) );
}
// save the data value from this custom field on product admin tab
add_action( 'woocommerce_process_product_meta', 'save_warranty_custom_field' );
function save_warranty_custom_field( $post_id ) {
$wc_text_field = $_POST['_warranty'];
if ( !empty($wc_text_field) ) {
update_post_meta( $post_id, '_warranty', esc_attr( $wc_text_field ) );
}
}
// Store custom field in Cart
add_filter( 'woocommerce_add_cart_item_data', 'store_warranty_custom_field', 10, 2 );
function store_warranty_custom_field( $cart_item_data, $product_id ) {
$warranty_item = get_post_meta( $product_id , '_warranty', true );
if( !empty($warranty_item) ) {
$cart_item_data[ '_warranty' ] = $warranty_item;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'days_manufacture', $warranty_item );
}
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['_warranty'] ) ) {
$custom_items[] = array( "name" => __( "Warranty", "woocommerce" ), "value" => $cart_item['_warranty'] );
}
return $custom_items;
}
// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3 );
function add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {
// Retrieving the product id for the order $item_id
$product_id = wc_get_order_item_meta( $item_id, '_product_id', true );
// Getting the warranty value for this product Id
$warranty = get_post_meta( $product_id, '_warranty', true );
// Add the meta data to the order
wc_add_order_item_meta($item_id, 'Warranty', $warranty, true);
}
Naturally, this goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
References:
WooCommerce : Add custom Metabox to admin order page
Admin product pages custom field displayed in Cart and checkout
There is an error at the end of the code, you call a variable $prod_id at first, and then $product_id. Correct and working code is:
// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3 );
function add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {
// Retrieving the product id for the order $item_id
$prod_id = wc_get_order_item_meta( $item_id, '_product_id', true );
// Getting the warranty value for this product Id
$warranty = get_post_meta( $prod_id, '_warranty', true );
// Add the meta data to the order
wc_add_order_item_meta($item_id, 'Warranty', $warranty, true);
}