How can I add to the order meta from input boxes? - php

I'm creating a plugin for wordpress and woocommerce. In my plugin, I've inserted an input box above the checkout form using code using 'woocommerce_before_checkout_form'. On completion of the order, I'd like to be able to add the value from that input to the order's meta data. to that end, I created this code in the functions.php file of my plugin:
add_action( 'woocommerce_checkout_update_order_meta', 'add_input_meta', 1, 2 );
function add_input_meta( $order_id, $posted ) {
$inputsData = $_POST['InputBox'];
update_post_meta( $order_id, 'my_key', $inputsData);
}
The problem is, it returns NULL every time. I created the code below to see what the value of $_POST and it came up with 'array(0) { }'
function debugthing( $content ) {
$content .=var_dump($_POST);
return $content;
die();
}
add_filter( 'the_content', 'debugthing' );
I've exhausted every idea I could ahve about what is causing this. can anyone help?
$_POST, even php://input return an empty array or absolutely nothing.

woocommerce_before_checkout_form is not the right hook to add input fields. For this hook is outside the form. This explains why you are getting null on $_POST
use any of the hook inside the <form in form-checkout.php#L35
call woocommerce_form_field to add fields...
next is you need to hook inside process_checkout() function.
a. woocommerce_after_checkout_validation - for input validations...
b. woocommerce_checkout_order_processed - order created, do your meta data addition...
// add form fields
add_action( 'woocommerce_checkout_before_customer_details', 'woocommerce_checkout_before_customer_details' );
function woocommerce_checkout_before_customer_details() {
$args = array(
'type' => 'text',
'label' => 'My Custom field',
'description' => 'This is custom field',
'placeholder' => '',
'required' => true,
);
woocommerce_form_field( 'InputBox' , $args ); // you can call woocommerce_form_field as many as you want...
}
// validate your form field(s)
add_action( 'woocommerce_after_checkout_validation', 'woocommerce_after_checkout_validation' );
function woocommerce_after_checkout_validation() {
// $_POST['InputBox'] will be visible here...
// do your validations here... forget this hook if you don't need to validate...
// wc_add_notice( __( 'Invalid message!', 'woocommerce' ), 'error' );
// call wc_add_notice if you want to invalidate the form.
}
add_action( 'woocommerce_checkout_order_processed', 'woocommerce_checkout_order_processed' );
function woocommerce_checkout_order_processed( $order_id ) {
// we now have $order_id, you can now add your meta data....
}

change your hook priority like this.
add_action( 'woocommerce_checkout_update_order_meta', 'add_input_meta', 99, 2 );

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.

Insert a custom field value as a new row on email order totals table in Woocommerce

I'm helping my mother create her website for her store i Denmark.
It has gone okay, but now i'm stuck with a problem.
I need to be able to make a custom field in the order complete email, that displays a track & trace number, i've inserted on the admin order page. Everything i've done up until now, haven't worked, so therefore i seek your help.
I've added a custom field already called Track & Trace Pakkenr. (see screenshot 1)
But the problem is getting this in the order complete email, under shipping (forsendelse in danish, see screenshot 2)
I am also a complete and utter beginner in coding so if any of you can give any help or advice, please make it almost foolproof to follow.
Here are the screenshot 1 and screenshot 2.
To get this order custom field value displayed in order totals table on email notifications, use the following:
add_filter( 'woocommerce_get_order_item_totals', 'insert_custom_line_order_item_totals', 10, 3 );
function insert_custom_line_order_item_totals( $total_rows, $order, $tax_display ){
// Only on emails notifications
if( is_wc_endpoint_url() ) return $total_rows; // Exit
$tracking_label = 'Track & Trace Pakkenr.'; // The tracking label name
$tracking_value = $order->get_meta( $tracking_label ); // Get the tracking value (custom field).
if( empty($tracking_value) ) return $total_rows; // Exit
$new_total_rows = array(); // Initializing
// Loop through total rows
foreach( $total_rows as $key => $value ){
if( 'payment_method' == $key && ! empty($tracking_value) ) {
$new_total_rows['tracking_parcel'] = array(
'label' => $tracking_label,
'value' => $tracking_value,
);
}
$new_total_rows[$key] = $total_rows[$key];
}
return sizeof($new_total_rows) > 0 ? $new_total_rows : $total_rows;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
You will get something like:
From this:
You can add custom fields to the order email, you need to use the field key where the code has meta_key.
/**
* 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( $fields, $sent_to_admin, $order ) {
$fields['meta_key'] = array(
'label' => __( 'Label' ),
'value' => get_post_meta( $order->id, 'meta_key', true ),
);
return $fields;
}
Ref: https://docs.woocommerce.com/document/add-a-custom-field-in-an-order-to-the-emails/
Below is a simple yet functional example of adding custom meta fields to order email
add_filter( 'woocommerce_email_order_meta_fields', 'woocommerce_email_order_meta_fields_func', 10, 3 );
function woocommerce_email_order_meta_fields_func( $fields, $sent_to_admin, $order ) {
$fields['Track_Field'] = array(
'label' => __( 'Track', 'woocommerce' ),
'value' => wptexturize( get_post_meta( $order->id, 'Track_Field', true ) )
);
//... more meta fields goes here
return $fields;
}
In email after order email table:
add_action( 'woocommerce_email_after_order_table', 'woocommerce_email_after_order_table_func' );
function woocommerce_email_after_order_table_func( $order ) {
?>
<h3>Track</h3>
<table>
<tr>
<td>Track Fields </td>
<td><?php echo wptexturize( get_post_meta( $order->id, 'Track_Field', true ) ); ?></td>
</tr>
<!--additional custom meta and HTML code goes here-->
</table>
<?php
}
For more help see this link : Click here

Save an empty value from a custom Text field in WooCommerce

I am working on a WordPress eCommerce website, with WooCommerce being the chosen Shopping Platform.
I have created a Custom Field, within the WooCommerce Product Dashboard by inserting the following code into the functions.php file:
function product_custom_fields_add(){
echo '<div class="product_custom_field">';
// Minimum Required Custom Letters
woocommerce_wp_text_input(
array(
'id' => '_minimum_engrave_text_option',
'name' => '_minimum_engrave_text_option',
'desc' => __('set custom minimum Lettering text field', 'woocommerce'),
'label' => __('Minimum Letters', 'woocommerce'),
'desc_tip' => 'true'
)
);
echo '</div>';
}
add_action('woocommerce_product_options_advanced', 'product_custom_fields_add');
In order to save the values, I have entered the following code into the functions.php file:
// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
if ( ! empty( $_POST['_minimum_engrave_text_option'] ) )
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
The above code works when a value is entered into the Custom Field. My problem is that I am unable to successfully save a Blank Value. Whenever I save a Product, the Custom Field either auto populates the Field with '1' or saves a previously entered number.
I tried applying the answer, from a similar question, but could not quote get it to work.
Does anyone know where I am going wrong here?
Try to replace empty() by isset() as a condition in the if statement of your hooked function:
// Save Minimum Required Custom Letters
function woocommerce_product_custom_fields_save1($post_id){
if ( isset( $_POST['_minimum_engrave_text_option'] ) )
update_post_meta($post_id, '_minimum_engrave_text_option', esc_attr( $_POST['_minimum_engrave_text_option'] ));
}
add_action( 'woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save1' );
Now this will works

Woocommerce Products Custom Field

I have added custom fields to my woocommerce products and have been able to enter and fill the custom field data on products. I am trying now to use the data from that custom field in a button I am adding on the woocommerce single product page.
The custom field is a URL for the product sample. I am then trying to add a "View Product Sample" button on the woocommerce single product page that navigates to the url entered int he custom field. Here is the code I have:
// Display Fields
add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );
// Save Fields
add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
// Text Field
woocommerce_wp_text_input(
array(
‘id’ => ‘product_sample’,
‘label’ => __( ‘Sample Product Link’, ‘woocommerce’ ),
‘placeholder’ => ‘http://’,
‘desc_tip’ => ‘true’,
‘description’ => __( ‘Enter the sample product link here.’, ‘woocommerce’ )
)
);
}
function woo_add_custom_general_fields_save( $post_id ){
// Textfield
$woocommerce_text_field = $_POST[‘product_sample’];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, ‘product_sample’, esc_html( $woocommerce_text_field ) );
}
add_action(‘woocommerce_after_add_to_cart_button’,’cmk_additional_button’);
function cmk_additional_button() {
echo '<a href="CONTENT OF CUSTOM FIELD - URL" target="_blank" button
type="submit" class="button sample">View Product Sample</a>';
}
I need help determining how to get my custom field meta data into the link for the button that is echoed at the end of the code.
Thanks for any help anyone can give.
You Can use ACF plugin for it and call the shortcode on single product page
Reference Url:http://blog.adlivetech.com/get-custom-fields-woocomerce/
You need to use get_post_meta() function this way:
add_action( 'woocommerce_after_add_to_cart_button', 'cmk_additional_button' );
function cmk_additional_button() {
global $post;
$url = get_post_meta( $post->ID, 'product_sample', true );
echo 'View Product Sample';
}
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
This should work.
In the code of your question you are using ’ and you should replace them by ' instead, to avoid php errors.

WooCommerce: Validate custom fields on My Account's edit page

I've added custom fields to my WooCommerce registration using this process.
I've made them available on the My Account's edit page using these actions:
// added custom fields here
add_action( 'woocommerce_edit_account_form', 'my_woocommerce_edit_account_form' );
// saved user meta here
add_action( 'woocommerce_save_account_details', 'my_woocommerce_save_account_details' );
In between the two, I need to validate these fields when editing. I tried using the woocommerce_process_myaccount_field_ filter (as mentioned here) but that didn't work. The code inside it doesn't trigger when I save the changes.
Any ideas on how can I validate?
Am I using the correct filter?
If yes, why doesn't it trigger?
Thanks.
You could try to use one of this 2 hooks for validating custom fields.
add_action( 'user_profile_update_errors','wooc_validate_custom_field', 10, 1 );
// or
add_action( 'woocommerce_save_account_details_errors','wooc_validate_custom_field', 10, 1 );
// with something like:
function wooc_validate_custom_field( $args )
{
if ( isset( $_POST['custom_field'] ) ) // Your custom field
{
if(strlen($_POST['custom_field'])<4 ) // condition to be adapted
$args->add( 'error', __( 'Your error message', 'woocommerce' ),'');
}
}

Categories