Add custom field data to WooCommerce order - php

I have a custom field on my WooCommerce single product. It sends to the cart fine, it displays on checkout fine, it shows in the order in the dashboard fine.
What I am now trying to do is set the value as a custom field in the order page so I am able to amend the text when I need to. For some reason when I submit the form this step isn't working.
The code that i use in my functions.phpfile:
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
echo '<label>fill in this field</label> <input type="text" name="my_field_name">';
echo '</div>';
}
// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['my_field_name'] ) ) {
$cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['my_field_name'] ) ) {
$custom_items[] = array( "name" => 'My Field', "value" => $cart_item['my_field_name'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['my_field_name'] ) ) {
wc_add_order_item_meta( $item_id, "my_field_name", $values['my_field_name'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3 );
/** THIS IS WHERE I'M STUCK **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ($_POST['billing_company'])
if (!$_POST['my_field_name'])
$woocommerce->add_error( __('Please enter your XXX.') );
}
// 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['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}
// 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['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}
Screenshot of what currently happens:
What I would like to happen:
Any help would be greatly appreciated.

Updated: compatibility with Woocommerce version 3+
You have missing the function to display this custom field value on the order edit page:
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<p><strong>'.__('My Field Name').':</strong> ' . get_post_meta( $order_id, 'my_field_name', true ) . '</p>';
}
On the reference link below, you have all original wooThemes functional working code snippets. It's an excellent fully functional tutorial.
Reference: [Customizing checkout fields using actions and filters][1]
Edit: Get a custom label displayed with your custom field value in Order item meta
To get a custom label like "MY field name" with your custom field value (in order items meta) instead of a slug like my_field_name, refer to this treads:
Saving a product custom field and displaying it in cart page
Displaying product custom fields values in the order once processed
Adding user custom field value to order items details

I don't know if this still is relevant, I have tried to do this with code, unfortunately, I got stuck in the way. I have tried this woocommerce checkout field editor plugin, which did well to add custom field data to woocomemrce order.

Related

How to save from WooCommerce checkout a custom checkbox field state?

I have a problem with the update_post_meta function.
I have a user submitted value, which I pass via $_POST and then saving to post meta.
All is working fine, but when the value is '0' the post meta is not updated.
This is My code:
// Add custom checkout field: woocommerce_review_order_before_submit
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field_ritiro_sede' );
function my_custom_checkout_field_ritiro_sede() {
echo '<div class="cw_custom_class"><h3>'.__('Ritiro presso sede CER S.r.l. &nbsp').'</h3>';
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'ritiro_sede', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('SI'),
), WC()->checkout->get_value( 'ritiro_sede' ) );
echo '</div>';
}
// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta_ritiro_sede', 10, 1 );
function custom_checkout_field_update_order_meta_ritiro_sede( $order_id ) {
if ( ! empty( $_POST['ritiro_sede'] ) )
update_post_meta( $order_id, 'ritiro_sede', $_POST['ritiro_sede'] );
if ( isset( $_POST['ritiro_sede'] ) )
update_post_meta( $order_id, 'ritiro_sede', $_POST['0'] );
}
Does anyone have any idea what might be wrong?
Since WooCommerce 3, here below is the best way to save your custom checkout checkbox field value as order meta data (including when the checkbox is unchecked):
// Save the custom checkout checkbox field as the order meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
$value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
$order->update_meta_data( 'ritiro_sede', $value );
}
Now as user meta data is used by WC_Checkout get_value() method in your first function on:
WC()->checkout->get_value( 'ritiro_sede' )
So if you want the submitted value to be displayed on checkout page for the next purchase, you will need to save that custom checkout field also as user meta data using instead the following:
// Save the custom checkout checkbox field as the order meta and user meta
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
$value = isset($_POST['ritiro_sede']) ? '1' : '0'; // Set the correct values
// Save as custom order meta data
$order->update_meta_data( 'ritiro_sede', $value );
// Save as custom user meta data
if ( get_current_user_id() > 0 ) {
update_user_meta( get_current_user_id(), 'ritiro_sede', $value );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Add a custom field as a default value in WooCommerce backend and populate previous orders

I know that the first part of my question is possible but haven't found how to add a custom field to all orders in the back end and then populate it with a default value.
I'm looking to create a custom field called "Merchant Identifier" and then populate that with a default name e.g "Company X".
I looked at this code which adds an input value at the checkout and then shows in an order summary, but I only need a field adding as a custom field to every order in the back end.
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['billing_phone_new'] )
wc_add_notice( __( 'Phone 2 is compulsory. Please enter a value' ), 'error' );
}
/**
* 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['billing_phone_new'] ) ) {
update_post_meta( $order_id, 'billing_phone_new', sanitize_text_field( $_POST['billing_phone_new'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Phone 2').':</strong> <br/>' . get_post_meta( $order->get_id(), 'billing_phone_new', true ) . '</p>';
}
Once that custom field appears on all new and old orders I can then add this add this as a column in a scheduled CSV export (that's for later - I just need to achieve the first part).
I'm not sure whether I need to have a hidden field in the checkout first with a default value OR whether I can just add a custom field that shows on all the orders in the back end using a different method.
Anyone able to help?
Thanks
For new orders you can use the following
// Update the order meta with value
function action_woocommerce_checkout_update_order_meta( $order_id ) {
// Meta value
$meta_value = 'Company X';
update_post_meta( $order_id, 'merchant_identifier', $meta_value );
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 );
// OPTIONAL (will still work without this code, this is just to show it visually)
// Display field value on the order edit page
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
echo '<p><strong>' . __( 'Merchant Identifier', 'woocommerce') . ':</strong> ' . $order->get_meta( 'merchant_identifier' ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );
For existing orders you can perform the following function, after it has been executed (view any page - frontend) it may be removed.
// Run once, delete afterwards
function set_meta_for_old_orders () {
// Get ALL orders where meta key not exists
$orders = wc_get_orders( array(
'limit' => -1, // Query all orders
'meta_key' => 'merchant_identifier', // Post meta_key
'meta_compare' => 'NOT EXISTS', // Comparison argument
));
if ( ! empty ( $orders ) ) {
// Meta value
$meta_value = 'Company X';
foreach ( $orders as $order ) {
$order->update_meta_data( 'merchant_identifier', $meta_value );
$order->save();
}
echo 'Done!';
}
}
// Call function
add_action( 'wp_footer', 'set_meta_for_old_orders' );

Custom post type data and order item data in WooCommerce

I am using a plugin, WooCommerce Custom Post Type Manager, along with WooCommerce that enables the use of custom post types as products. This is working pretty well for the most part, but I want to be able to control inventory and I am able to see the problem. In the database under the order_item_meta table, the product_id = 0. Because the product_id is empty, there is no way to update the stock on a completed purchase.
I know that WooCommerce made some changes to where it searched if a post_type was a 'product' and if not, certain things failed. I am wondering if there is a filter hook or another way to add the product id with a custom function at checkout?
This is the function I am trying to create to control a custom type of inventory, we are selling event "seats". And no, even the regular "stock" does not work, probably because of the same reason.
function update_course_seats( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id(); // this returns 0
if($product_id != 0) {
wc_update_order_item_meta( $item, '_seats', 10 ); // example number
}
}
//add_action( 'woocommerce_payment_complete', 'update_course_seats');
First you should use woocommerce_add_cart_item_data action hook, to store your Custom Post Type (CTP) Id on add to cart… But for that you will need to display, on your CTP pages, inside the add to cart form, a hidden field with the CTP Id like:
<input type="hidden" name="ctpost_id" value="<?php echo get_the_id(); ?>">
Now you can add this hooked function, that will add as custom cart item data your CTP Id:
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 3 );
function add_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ){
if( isset( $_POST['ctpost_id'] ) ) {
$cart_item_data['ctpost_id'] = wc_clean( $_POST['ctpost_id'] );
}
return $cart_item_data;
}
The Second hook to be used is woocommerce_checkout_create_order_line_item action hook, to add custom order item meta data (or to make changes to order item data). This hook is triggered before payment gateways process during order creation.
You can use WC_data add_meta_data() method to save your CTP Id as a custom CTP ID like:
add_action( 'woocommerce_checkout_create_order_line_item', 'save_cpt_id_to_order_item_data', 10, 4 );
function save_cpt_id_to_order_item_data( $item, $cart_item_key, $cart_item, $order ){
if( isset($cart_item['ctpost_id']) && $cart_item['ctpost_id'] > 0 ) {
// Add the custom CTP post ID
$item->add_meta_data('_ctpost_id', $cart_item['ctpost_id'] );
}
// And here for example you add seats as custom cart item data
if( isset($cart_item['quantity']) ) {
$item->add_meta_data( 'Seats', $cart_item['quantity'], true );
}
}
When the order will get paid, you will be able to update everything required, as you will have your CTP post ID as custom order item data.
Then finally, you can use woocommerce_payment_complete as follow:
add_action( 'woocommerce_payment_complete', 'action_payment_complete_callback', 10, 1 );
function action_payment_complete_callback( $order_id ){
$order = wc_get_order();
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
$ctp_id = $item->get_meta('_ctpost_id'); // Get the CTP post ID
// Your code goes here
}
}
Or woocommerce_order_status_changed hooks like:
add_action( 'woocommerce_order_status_changed', 'action_order_status_changed_callback', 10, 4 );
function action_order_status_changed_callback( $order_id, $status_from, $status_to, $order ){
if( in_array( $status_to, ['processing','completed'] ) ) {
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
$ctp_id = $item->get_meta('_ctpost_id'); // Get the CTP post ID
// Your code goes here
}
}
}
Related: Get Order items and WC_Order_Item_Product in Woocommerce 3.

Woo-commerce 3.0 single product price is not changing properly

I made a new post type named "sub_products" containing the meta tag "unit_price".
After assigning a new field for every Woocommerce product containing a list of all the "sub_products" posts, the goal was to update every product price based on the selected "sub_products" meta "unit_price".
function kulcskron_edit_post( $p1, $p2 )
{
if ( !is_admin() )
return;
if ( get_post_type() != 'product' )
return;
$sub_product_ids = $p2->get_meta( 'sub_products' );
if ( empty($sub_product_ids) )
return;
$product_regular_price = 0;
foreach ( $sub_product_ids as $id )
$product_regular_price += get_post_meta( $id, 'unit_price', true );
if ( $p1 == $product_regular_price )
return;
$p2->set_regular_price( $product_regular_price );
$p2->save();
}
add_action( 'woocommerce_product_get_price', 'kulcskron_edit_post', 10, 2 );
I tried every possible hook to make this work:
add_action( 'the_post', 'kulcskron_edit_post', 9, 1 );
add_action( 'edit_post', 'kulcskron_edit_post', 10, 2 );
add_action( 'pre_get_posts', 'kulcskron_edit_post' );
add_action( 'save_post', 'kulcskron_edit_post' );
This code updates the price but in a strange way:
Admin single product edit view: The price is not updated right away, just after I revisit the edit screen.
Admin product listing view: Every price is 0.
Front-end product page view: The displayed price is 0.
How do I update the product price based on the assigned "sub_products" "unit_price" post meta when a single product is saved in admin view?
You may use the hook "woocommefrce_get_price_html" it should work
I managed to solve the price update problem.
I used the save_post action:
save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email.
The final code:
function kulcskron_update_product( $product_obj )
{
if ( !is_admin() )
return;
if ( get_post_type() != 'product' )
return;
$product = wc_get_product( $product_obj );
if ( !$product->meta_exists( 'sub_products' ) )
return;
$sub_product_ids = $product->get_meta( 'sub_products' );
if ( empty($sub_product_ids) )
return;
_update_product_price( $product_obj, $sub_product_ids );
}
add_action( 'save_post', 'kulcskron_update_product' );
And for the sake of completeness here is the rest of code:
function _update_product_price( $product_obj, $sub_product_ids )
{
$product = wc_get_product( $product_obj );
$product_regular_price = 0;
foreach ( $sub_product_ids as $id )
$product_regular_price += get_post_meta( $id, 'kulcskron_unit_price', true );
$product->set_regular_price( $product_regular_price );
$product->set_price( $product_regular_price );
$product->save();
}
This way basically everything can be edited and updated. Example:
function _update_product_stock( $product_obj, $sub_product_ids )
{
$product = wc_get_product( $product_obj );
$sub_product_stocks = array();
foreach ( $sub_product_ids as $id )
$sub_product_stocks[] = get_post_meta( $id, 'kulcskron_free_stock_quantity', true );
$product->set_manage_stock( true );
$product->set_stock_quantity( min($sub_product_stocks) );
$product->save();
}
I would love to post a link to all the methods to edit a product but I have no rep for that.
But sadly this is not all...
In order to fully update the price, we need to filter the price HTML to return the newly updated price.
function kulcskron_price_html( $priceHtml, $product )
{
$symbol = get_woocommerce_currency_symbol();
$price = $product->get_regular_price();
$html = '<span class="woocommerce-Price-amount amount">'. $price .' <span class="woocommerce-Price-currencySymbol">'. $symbol .'</span></span>';
return $html;
};
add_filter( 'woocommerce_get_price_html', 'kulcskron_price_html', 10, 2 );
A little backstory. We need to sync all the products from an external database through an XML file. But there is a catch, the XML file only contains product parts and they are not displayed, searched, filtered and certainly can't be ordered individually. None of the Woocommerce build in functionality meet these requirements.
In order to solve this, I registered a new post type and imported all the product parts into this newly created post type. After that, I registered a new field for every WC Product with the Advanced Custom Fields plugin.
It looks like this:
Newly registered custom post type with the custom fields
The code above makes easy to update the WC Products (e.g. Prices) based on the selected Sub products.

Add cart meta value for a product after an order request is submitted

I have a multi vendor restaurant WooCommerce site. And one person can order max from one restaurant.On a restaurant when adding product to cart I have added an extra meta-field restaurant_id which is the id of that restaurant. In a cart , all the products have same restaurant_id.
i.e : I have two items in cart having product_id : 12 and 13 but they have same restaurant_id : 366.
I need to add this restaurant_id as meta-field in order-complete or order-process action.because i need to show restaurant name on customers account page.
Or any easy way to do that?
I have tried below code as test
add_action( 'woocommerce_checkout_update_order_meta', 'add_field_to_order' );
function add_field_to_order( $order_id ) {
update_post_meta( $order_id, 'new_field', 'new_value' );
}
But it does not add any meta-field and meta-value to order
— Update —
As I understand now, this custom field already exist and you get this restaurant_id value in cart. So you would like to display that in your view order (thank you and my account pages) and may be on emails…
Here is that code:
//
// ADD HIDDEN IMPUT FIELDS TO THE CHECKOUT
//
add_action( 'woocommerce_after_order_notes', 'checkout_custom_hidden_imput_field' );
function checkout_custom_hidden_imput_field( $checkout ) {
foreach(WC()->cart->get_cart() as $item){
$restaurant_id = $item['restaurant_id'];
break;
}
echo '<div id="custom_checkout_fields" class="custom-hidden-checkout-field">
<input type="hidden" id="restaurant_id" name="restaurant_id" value="'.$restaurant_id.'" />
</div>';
}
//
// SAVE THE ORDER META WITH FIELD VALUE
//
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['restaurant_id'] ) ) {
add_post_meta( $order_id, '_restaurant_id', $_POST['restaurant_id'] );
}
}
//
// DISPLAY FIELD VALUE ON THE ORDER EDIT PAGE (NOT IN CUSTOM FIELDS METABOX)
//
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta($order){
$restaurant_id = get_post_meta( $order->id, '_restaurant_id', true );
if ( ! empty( $restaurant_id ) ) {
echo '<p><strong>'. __("Restaurant ID", "woocommerce").':</strong> ' . $restaurant_id . '</p>';
}
}
//
// ADD THE INFORMATION AS META DATA SO THAT IT CAN BE SEEN AS PART OF THE ORDER
//
add_action('woocommerce_add_order_item_meta','custom_add_values_to_order_item_meta', 1, 3 );
function custom_add_values_to_order_item_meta( $item_id, $values, $cart_item_key ) {
$restaurant_id = get_post_meta( $order->id, '_restaurant_id', true );
// lets add the meta data to the order!
wc_add_order_item_meta($item_id, '_restaurant_id', $restaurant_id, true);
}
Code goes in any php file of your active child theme (or theme) or also in any plugin php files.
Code is tested and works.
You could utilize the hook woocommerce_order_status_XXX, where XXX is the order status.
For example:
add_action( 'woocommerce_order_status_completed', 'my_order_status_change_function' );
function my_order_status_change_function( $order_id ) {
$order = new WC_Order($order_id);
//your logic for updating order meta, the meta is stored in wp_postmeta.
update_post_meta( $order_id, 'new_field', 'new_value' );
}
To trigger the function when order status is changed to processing,
change the action name to woocommerce_order_status_processing.

Categories