I want display the external table value in WooCommerce checkout from company name field. We can create external table and insert some of company name.
When user signup our service in checkout field, customer should select the company name in a custom select options field. This values should be displayed from a custom database table.
How can I achieve this?
Thanks in Advance.
We are going to first unset the field type of company, and then to change it to a type 'select' (selector). Then we will maque a query to get all options key/values from your custom database table (Let's say is called 'wp_companies'):
(ONLY FOR TESTING) NO database query here (with 3 options for companies):
add_filter( 'woocommerce_default_address_fields' , 'set_custom_company_checkout_field' );
function set_custom_company_checkout_field( $address_fields ) {
unset($fields['company']['type']);
$address_fields['company']['type'] = 'select';
$address_fields['company']['options'] = array(
'option_1' => 'Company 1',
'option_2' => 'Company 2',
'option_3' => 'Company 3'
);
// (optional)
// $address_fields['company']['default'] = 'Company 1';
return $address_fields;
}
Paste this code in function.php file located in your active child theme (or theme).
This code is tested and works…
THE REAL CODE: Making the query from database custom table (to be adapted):
add_filter( 'woocommerce_default_address_fields' , 'set_custom_company_checkout_field' );
function set_custom_company_checkout_field( $address_fields ) {
// Unset company field type
unset($fields['company']['type']);
global $wpdb;
$select_options = array();
// ### you will need to replace names table and columns and adapt this !!!
$query = "SELECT id, company FROM table";
$companies_name = $wpdb->get_results($query);
// Storing object $company_name keys/values in $select_options array
foreach ( $companies_name as $company_name )
{
$key = 'option_'. $company_name->ID;
$value = $company_name->custom_column_value;
$select_options[$key] = $value ;
}
$address_fields['company']['type'] = 'select';
$address_fields['company']['options'] = $select_options;
// (optional)
// $address_fields['company']['default'] = $select_options['option_1'];
return $address_fields;
}
Paste this code in function.php file located in your active child theme (or theme).
References:
WooThemes - Customizing checkout fields using actions and filters
WooCommerce - Overriding billing state and post code on existing checkout fields
Related
This is my current recent order
I wish to add another column "Tracking Number" and it will show woocommerce "note to customer" inside.
result is like : Display last WooCommerce admin order note in customers order history
The difference is without clicking view order and my customer can get to known their tracking number.
But I totally no idea how this work because not familiar with php..
hope to make this done and learn something.
Thanks!
the result will
You Need to create a new column in My order page first
function order_note_in_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// Your Column Name : Change Tracking Number with the Column Heading you Want
if ( 'order-status' === $key ) {
$new_columns['track-number'] = __( 'Tracking Number', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'order_note_in_column' );
Once you create a new column now the second step is to display Data in the column.keep type as internal to show private note, In this way customer notes will not be displayed at frontend
function order_note_value_in_column( $order ) {
//Get Notes by order ID & Here keep type as internal to show private note. In this way customer notes will not be displayed at frontend
$note = wc_get_order_notes([
'order_id' => $order->get_id(),
'type' => 'internal',
]);
// Displaying the latest Note. If no tracking number entered then order status will be displayed in column
print_r($note[0]->content);
}
add_action( 'woocommerce_my_account_my_orders_column_track-number', 'order_note_value_in_column' );
code goes in functions.php tested & works
I'm trying to make an SQL query to run upon cancellation of a booking, via a custom plugin, updating custom user meta data.
Here's my code:
function wporg_callback() {
global $wpdb;
$wpdb->query("UPDATE usermeta SET meta_value = 15 WHERE umeta_id = 131");
}
add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback');
But It's not working.
Is there something wrong with the query? Is the right action not being used?
Edit - I Also tried the following without success:
add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback');
function wporg_callback( $booking_id ) {
// Add/Update custom user meta data
update_user_meta( 2, 'download_credits', 17 );
}
Updated:
The correct hook to be used is woocommerce_booking_cancelled (a composite hook) that will allow you to retrieve the user ID and to Add/update custom user meta data like below:
add_action('woocommerce_booking_cancelled', 'booking_cancelled_transition_to_callback', 10, 2 );
function booking_cancelled_transition_to_callback( $booking_id, $booking ) {
// Get the user ID from the Booking ID
$user_id = get_post_field ('post_author', $booking_id);
$download_credits = get_post_meta( $booking_id, '_booking_cost', true );
// Add/Update custom user meta data
update_user_meta( $user_id, 'download_credits', $download_credits );
}
Code goes in functions.php file of your active child theme (or theme). Tested and works.
How to get the booking data (from the booking ID displayed in the order):
Go to your database under wp_postmeta table to get the desired meta_key from the post_id (that is the booking ID)
Use that meta key in get_post_meta() WordPress function like:
$meta_value = get_post_meta($booking_id, 'the_meta_key', true);
Notes:
An SQL query is not needed to add/update custom user meta data, as you can use instead the dedicated Wordpress function update_user_meta().
I couldn't make it work using woocommerce_bookings_cancelled_booking hook.
WooCommerce Bookings action and filters hooks developer documentation
Email notification of New order process has 'My Blog' title.
I look into Woocommerce setting but could not find it.
Any Idea how to change 'My Blog' to 'X Company'
red underline text in attached images.
Plateform: Wordpress + Woocommerce
Update:
What you want to change is the "From name" and it can be changed using:
add_filter('woocommerce_email_from_name', 'change_new_order_email_from_name', 10, 2 );
function change_new_order_email_from_name( $from_name, $email ){
if( $email->id === 'new_order' )
$from_name = __("ACME corp");
return $from_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Addition: To add custom placeholders for the email subject (for woocommerce 3.2+):
// Only for woocommerce versions 3.2 + (up to 3.2)
add_filter( 'woocommerce_email_format_string' , 'custom_email_format_string', 20, 2 );
function custom_email_format_string( $string, $email ) {
// Get the instance of the WC_Order object
$order = $email->object;
// Additional wanted placeholders in the array of find / relace pairs
$additional_placeholders = array(
'{shop_company}' => __("ACME corp"),
);
// return the clean string with new replacements
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). Tested and works.
Then in your email settings, on the Subject field of an email notification you will be able to replace for example:
Your {site_title} order receipt from {order_date}
by
Your {shop_company} order receipt from {order_date}
Please use this WooCommerce hook woocommerce_email_subject_new_order to change new order email title.
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 );
function customizing_new_order_subject( $formated_subject, $order ){
// Get an instance of the WC_Email_New_Order object
$email = WC()->mailer->get_emails()['WC_Email_New_Order'];
// Get unformatted subject from settings
$subject = $email->get_option( 'subject', $email->get_default_subject() );
// Loop through order line items
$product_names = array();
foreach( $order->get_items() as $item )
$product_names[] = $item->get_name(); // Set product names in an array
// Set product names in a string with separators (when more than one item)
$product_names = implode( ' - ', $product_names );
// Replace "{product_name}" by the product name
$subject = str_replace( '{product_name}', $product_names, $subject );
// format and return the custom formatted subject
return $email->format_string( $subject );
}
For more details see this link
Go to
WooCommerce > Settings > Emails > Processing Orders.
Here you will find a field called "Email Subject". Here, change {site_title} to whatever you want it to appear.
Alternatively, if you want to change the value of {site_title} itself, then head over to Settings > General.
Here you will find the field called Site Title. Change it to whatever you want it to appear.
Let me know if it works!
I found the solution after digging, its very easy.
WooCommerce>Setting>Emails
At the bottom, there is section where you can header and footer text.
Simple.
Really appreciate your help #LoicTheAztec
I am looking for a drop down of country/state/city. I am getting country state auto populated, but cities are not going through well. By default, I am able to get the countries.
// State-Country additions
/**
* Code goes in functions.php or a custom plugin.
*/
add_filter('woocommerce_states', 'SA_woocommerce_states');
function SA_woocommerce_states($states) {
$states['ZA'] = array(
'EC' => __('Eastern Cape', 'woocommerce'),
);
return $states;
}
// Change "city" checkout billing and shipping fields to a dropdown
add_filter('woocommerce_checkout_fields', 'override_checkout_city_fields');
function override_checkout_city_fields($fields) {
// Define here in the array your desired cities (Here an example of cities)
$option_cities = array(
'' => __('Select your city'),
'a' => 'a',
);
$fields['billing']['billing_city']['type'] = 'select';
$fields['billing']['billing_city']['options'] = $option_cities;
$fields['shipping']['shipping_city']['type'] = 'select';
$fields['shipping']['shipping_city']['options'] = $option_cities;
return $fields;
}
With your actual code you are replacing all existing states of "South Africa" (ZA) by one state. So you are getting something like:
To add this state you should need to change your code a little bit this way:
add_filter('woocommerce_states', 'sa_woocommerce_states');
add_filter('woocommerce_countries_allowed_country_states', 'sa_woocommerce_states');
function SA_woocommerce_states( $states ) {
$states['ZA']['EC'] = __('Eastern Cape', 'woocommerce');
return $states;
}
The code goes in the function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. You will get that instead this time:
Now to get the cities auto populated you should use this:
add_filter( 'woocommerce_default_address_fields', 'override_checkout_city_fields', 10, 1 );
function override_checkout_city_fields($fields) {
// Define here in the array your desired cities (Here an example of cities)
$option_cities = array(
'' => __( 'Select your city' ),
'a' => 'a',
);
$fields['city']['type'] = 'select';
$fields['city']['options'] = $option_cities;
return $fields;
}
The code goes in the function.php file of your active child theme (or theme) or also in any plugin file.
It was tested ad worked…
But you will not get cities by states as this is a real development and too broad for Stack Overflow
In Woocommerce, I would like to change Town/City text field in a select field option field.
What should i do?
Here is a screenshot:
Thanks
You need first to change the field type from 'text' to 'select' using the dedicated hook woocommerce_default_address_fields. Then you have also to change the label and to and an options argument where you are going to set your towns in an array of key/values.
In this array, you will have a line by town separated by a coma.
Here is the code:
add_filter( 'woocommerce_default_address_fields' , 'customize_checkout_city_field' );
function customize_checkout_city_field( $address_fields ) {
// Set HERE the cities (one line by city)
$towns_cities_arr = array(
'0' => __('Select your city', 'my_theme_slug'),
'paris' => 'Paris',
'versailles' => 'Versailles',
'cannes' => 'Cannes',
);
// Customizing 'billing_city' field
$address_fields['city']['type'] = 'select';
$address_fields['city']['class'] = array('form-row-last', 'my-custom-class'); // your class here
$address_fields['city']['label'] = __('Town / city', 'my_theme_slug');
$address_fields['city']['options'] = $towns_cities_arr;
// Returning Checkout customized fields
return $address_fields;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and fully functional.
Update: To add your custom class, replace in $address_fields['city']['class']… the class 'my-custom-class' by yours.
References:
Customizing checkout fields using actions and filters - Overriding core fields
You can customize the checkout fields by actions and filters.
Please refer the official documentation here
// Add these code in your theme's function.php
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $fields) {
$fields['billing']['your_field']['options'] = array(
'option_1' => 'Option 1 text',
'option_2' => 'Option 2 text'
);
return $fields;
}
In case you are looking for a plugin you can use checkout field editor from woocommerce team.