Show custom field on order in woocommerce - woocommerce

Im working on a webshop and follwoing this tutorial http://wcdocs.woothemes.com/snippets/tutorial-customising-checkout-fields-using-hooks-and-filters/ to add some customes fields to my billing.
// 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 ) {
$fields['billing']['billing_gls_name'] = array(
'label' => __('Name for pickup person', 'woocommerce'),
'placeholder' => _x('Name', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
This adds my field. So far so good. So my problem is:
How can I view this new field in the orders view? Details for billing only show the usual billing fields.

The first answer (Cesar) was CLOSE to being correct. In case anyone ever comes across this old post trying to find out the same thing, below is the code needed to insert into your functions.php file after the code given by the original poster, tailored to his/her variables as provided. Note that they use the field name "billing_gls_name" and that this is referenced in our new function as "_billing_gls_name". The extra "_" at the beginning is necessary. This works on Wordpress 3.5.1 running WooCommerce 2.0.3.
function your_custom_field_function_name($order){
echo "<p><strong>Name of pickup person:</strong> " . $order->order_custom_fields['_billing_gls_name'][0] . "</p>";
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'your_custom_field_function_name', 10, 1 );

After defining your custom field (that you did in your code mentioned above), add the code mentioned below to:
Process your field
Save it in the database as Order meta data
Display it in the 'Order details' in the Woocommerce->Orders section
Process your field:
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
if (!$_POST['billing']['billing_gls_name']) {
wc_add_notice(__('Please tell us the Location Type that we are billing to.'), 'error');
}
}
Save Field in the DB as Order Meta Data:
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']['billing_gls_name'])) {
update_post_meta($order_id, 'Billing Gls Name', esc_attr($_POST['billing']['billing_gls_name_type']));
}
}
And finally display it in the Order details screen:
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_billing_fields_display_admin_order_meta', 10, 1);
function my_custom_billing_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Billing Gls Name') . ':</strong><br> ' . get_post_meta($order->id, '_billing_gls_name', true) . '</p>';
}

Adding the action woocommerce_admin_order_data_after_billing_address you can insert some data after billing info. Custom fields are under $order->order_custom_fields array.
function display_rfc_in_order_metabox($order){
echo "<p><strong>RFC:</strong> {$order->order_custom_fields['_billing_rfc'][0]}</p>";
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'rxm_details_to_order', 10, 1 );

Related

How to add UTM parameters to WooCommerce order email?

I'm running some Facebook-Ads for a WooCommerce store and would like to add/pass the UTM parameters (utm_source, utm_medium and utm_campaign) to the WooCommerce order email (for the admin, not the customer). I did not find anything in the Woo documentation nor a plugin that is able to do that, maybe I am missing something.
I found a way how to add a custom field for the email, eg:
/**
* 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['hear_about_us'] = array(
'label' => __( 'Hear About Us' ),
'value' => get_post_meta( $order->id, 'hear_about_us', true ),
);
return $fields;
}
but I don't know how I'd be able to pass the parameter contents. Any help is greatly appreciated!

How to change field's label on checkout page in woocommerce?

I am using wordpress for the first time. There is overwhelming information available about How to do stuff in WordPress. I want to simply change the label of postal code/zip field on check out page. Here are the steps i followed:
1>Installed woocommerce plugin
2>Imported Dummy Data
3>Installed Astra Starter Template Plugin
4>Installed Brandstore theme
5>Created my own child theme as per guidelines
Tested site. At this point everything working fine.
Now i want to simply change the lable for "Postalcode/Zip" on chekout page
So as per the woocommerce guidelines i added the following code in child theme's functions.php
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields',100 );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_postcode']['label'] = 'Postal Code';
return $fields;
}
When i refresh page it did not change the label.
Q1> How do i change field lable on checkout page.
Q2> where is template located for checkout page? Look like checkout page is using
[woocommerce_checkout] code
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
visit Official Documentation

Wordpress: save custom billing field in account Billing address

Using Wordpress/Woocommerce.
I have this code which adds a custom billing field in checkout page called: "NIF/CIF". It works fine but its value it not saved in the customer account "Billing Address" data.
Once the customer makes a first order all billing address values are saved in his account: Address, State, Country, etc. But my custom field is not saved.
I guess that in my function is missing a line of code to save its value in the database, but I don't know how to start with this.
/*******************************
CUSTOM BILLING FIELD
*********************************/
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['nif_cif'] = array(
'label' => __('NIF/CIF', 'woocommerce'), // Add custom field label
'placeholder' => _x('NIF/CIF', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name
);
return $fields;
}
Here is an example of how to save your custom field:
add_action( 'woocommerce_checkout_order_processed', 'prefix_save_field_on_checkout', 11, 2 );
function checkout_order_processed_add_referral_answer( $order_id, $posted ) {
if ( ! isset( $_POST['nif_cif'] ) ) {
return;
}
$order = wc_get_order( $order_id );
// WC < 3.0
update_post_meta( $order->id, 'order_meta_field_name', wc_clean( $_POST['nif_cif'] ) );
// WC > 3.0
$order->add_meta_data( 'order_meta_field_name', wc_clean( $_POST['nif_cif'] ), true );
$order->save();
}
Adding an extra field through 'woocommerce_billing_fields' hook is not enough. you are missing out two things.
Proper validation of NIF/CIF field using
'woocommerce_after_checkout_validation' hook
Saving data in order
using 'woocommerce_checkout_order_processed' hook

How to add short description in WooCommerce checkout page

Thanks for reading, Just had a issue regarding WooCommerce, I want to add a short description checkout page of below billing field.
How to add short description in WooCommerce checkout page of below billing field?
I tried add function, custom code but failed with error.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$post_data = get_post( $cart_item['product_id'] );
$other_data[] = array( 'name' => 'description', 'value' => $post_data->post_excerpt );
return $other_data;
}
I was used this code but it is showing inner product info table.
There's no real reason to call get_post(). The $product object is stored in the $cart_item array and the $post object is stored inside the $product. This gets the product's excerpt (aka the short description) to show up in the cart and in the checkout. Now, it isn't likely the make the description show up on the order received page, or in the my account area, or in emails, etc since the only place that the woocommerce_get_item_data filter appears is in the cart class.
One thing to take note of, WooCommerce 2.7 is a major rewrite of WooCommerce and $_product->post->post_excerpt will result in PHP notices about directly accessing product properties. So I've suggested both the 2.6 and 2.7 compatible approaches.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$_product = $cart_item['data'];
// Use this for WC2.7
//$other_data[] = array( 'name' => 'description', 'value' => $_product->get_short_description() );
// Use this for WC2.6
$other_data[] = array( 'name' => 'description', 'value' => $_product->post->post_excerpt );
return $other_data;
}

Add Tax Exempt form on checkout in woocommerce

I am trying to add a form to my checkout page so when a user clicks the 'Tax Exempt' checkbox, a textbox will popup and ask the user what the Tax Exempt Id number is.
I got all of that working great, and I even added the update_totals_on_change class to my form field so it will update the totals.
My next step was to add an action/filter on a method so when the update_totals_on_change executes, I can set the tax to 0 and then it will finish calculating the total.
Does anyone know which functions I can hook on to?
Looking at the checkout.js file in WooCommerce, they set the action to woocommerce_update_order_review for the ajax operation.
I tried following that but soon got lost.
I was thinking I could add some post data by hooking in to woocommerce_checkout_update_order_review
and then hooking in to woocommerce_before_calculate_totals to modify the tax stuff, but I have no idea what I need to modify.
Am I even on the right path?
Alright, I finally figured it out in case anyone is interested.
In my plugin, I made a form after the order notes by hooking in to this function: 'woocommerce_before_order_notes'
add_action('woocommerce_before_order_notes', array(&$this, 'taxexempt_before_order_notes') );
my 'taxexempt_before_order_notes' function contained:
function taxexempt_before_order_notes( $checkout ) {
echo '<div style="clear: both"></div>
<h3>Tax Exempt Details</h3>';
woocommerce_form_field( 'tax_exempt_checkbox', array(
'type' => 'checkbox',
'class' => array('tiri taxexempt'),array( 'form-row-wide', 'address-field' ),
'label' => __('Tax Exempt'),
), $checkout->get_value( 'tax_exempt_checkbox' ));
woocommerce_form_field( 'tax_exempt_name', array(
'type' => 'text',
'class' => array('form-row-first', 'tiri', 'taxexempt', 'textbox', 'hidden'),
'label' => __('Tax Exempt Name'),
), $checkout->get_value( 'tax_exempt_name' ));
woocommerce_form_field( 'tax_exempt_id', array(
'type' => 'text',
'class' => array('form-row-last', 'tiri', 'taxexempt', 'textbox', 'hidden', 'update_totals_on_change'),
'label' => __('Tax Exempt Id'),
), $checkout->get_value( 'tax_exempt_id' ));
}
Then the most important woocommerce function to hook was: 'woocommerce_checkout_update_order_review'
add_action( 'woocommerce_checkout_update_order_review', array(&$this, 'taxexempt_checkout_update_order_review' ));
function taxexempt_checkout_update_order_review( $post_data ) {
global $woocommerce;
$woocommerce->customer->set_is_vat_exempt(FALSE);
parse_str($post_data);
if ( isset($tax_exempt_checkbox) && isset($tax_exempt_id) && $tax_exempt_checkbox == '1' && !empty($tax_exempt_id))
$woocommerce->customer->set_is_vat_exempt(true);
}
I simply parsed out the $post_data that is the serialized form data from the checkout.js file in woocommerce and checked if my part of the form was filled out correctly.
If it was, then I would set the tax exempt for the user.
The accepted solution didn't work for me, but I modified it to use the following:
//=============================================================================
// ADD TAX EXEMPT CHECKMARK
// =============================================================================
add_action( 'woocommerce_after_order_notes', 'qd_tax_exempt');
function qd_tax_exempt( $checkout ) {
echo '<div id="qd-tax-exempt"><h3>'.__('Tax Exempt').'</h3>';
woocommerce_form_field( 'shipping_method_tax_exempt', array(
'type' => 'checkbox',
'class' => array(),
'label' => __('My organization is tax exempt.'),
'required' => false,
), $checkout->get_value( 'shipping_method_tax_exempt' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_review', 'taxexempt_checkout_update_order_review');
function taxexempt_checkout_update_order_review( $post_data ) {
global $woocommerce;
$woocommerce->customer->set_is_vat_exempt(FALSE);
parse_str($post_data);
if ( isset($shipping_method_tax_exempt) && $shipping_method_tax_exempt == '1')
$woocommerce->customer->set_is_vat_exempt(true);
}
The key here is understanding that any field with a name that starts with shipping_method is going to inherit this updating order functionality (which was the part that didn't work for me). I found this answer at http://www.affectivia.com/blog/have-a-checkbox-on-the-checkout-page-which-updates-the-order-totals/
After a long search I found that there is a method for the cart object called remove_taxes() .
So, after setting a user meta for the tax exempt users, this cancels the tax totals.
function remove_tax_for_exempt( $cart ) {
global $current_user;
$ok_taxexp = get_the_author_meta( 'granted_taxexempt', $current_user->ID );
if ($ok_taxexp){ // now 0 the tax if user is tax exempt
$cart->remove_taxes();
}
return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );
Because $cart->remove_taxes(); is deprecated. This is what I used instead.
I didn't have a form on the frontend, but have a user roll that is tax exempt. This was my solution.
Also worth noting that set_is_vat_exempt(true) also works in the US to set as tax exempt.
/**
* Set customer as tax exempt if user is a wholesale customer
*/
function remove_tax_for_exempt( $cart ) {
global $woocommerce;
if ( is_user_logged_in() && current_user_can( 'wholesale_customer' ) ) {
$woocommerce->customer->set_is_vat_exempt(true);
}
return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );
Since this answer still pops up on google, I thought I'd share that setting the customer as tax exempt only works during checkout, if you need to edit the order on the back-end after it is placed and use the "recalculate" button, the taxes will still appear. Fortunately there is a hook for this as well:
function remove_tax_for_exempt($exempt, $order){
return $exempt || user_can($order->get_user_id(), 'wholesale_customer');
}
add_filter('woocommerce_order_is_vat_exempt', 'remove_tax_for_exempt', 10, 2);

Resources