When people enter the my woocommerce shop following a link in an sales email with email and name as parameters I would like to prefill the name and email in the checkout page.
Therefore I created an action and filter. This works as expected but only if I do a hard refresh on the sales page (ctrl + f5)
I've excluded the sales page and the checkout page from the cache and varnish cache but this didn't fix the issue.
Am I missing something here? Do you have any idea why this only works with a hard refresh?
Any help is greatly appreciated.
Code:
function save()
{
if ( is_page( 'sales-page' ) )
{
if ( isset( $_GET['tu_em'] ) ) {
global $woocommerce;
$woocommerce->session->set( 'tu_em', $_GET['tu_em'] );
}
if ( isset( $_GET['tu_name'] ) ) {
global $woocommerce;
$woocommerce->session->set( 'tu_name', $_GET['tu_name'] );
}
}
}
add_action( 'wp_enqueue_scripts', 'save_email' , 1100);
function override_checkout_email_field( $fields ) {
global $woocommerce;
$email = $woocommerce->session->get('tu_em');
if(!is_null($email)) {
$fields['billing']['billing_email']['default'] = $email;
}
$name = $woocommerce->session->get('tu_name');
if(!is_null($name)) {
$fields['billing']['billing_first_name']['default'] = $name;
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'override_checkout_email_field' );
Here below, you will find the correct working code, to save user data from an URL (GET) into WooCommerce sessions and to autofill with that data the related checkout fields.
URL will be like: http://example.com/sales-page/?tu_em=name#example.com&tu_name=theFirstName
This can be done from any URL as the code will detect the URL variable, when they are set.
The code;
// Save user data from URL to Woocommerce session
add_action( 'template_redirect', 'set_custom_data_wc_session' );
function set_custom_data_wc_session () {
if ( isset( $_GET['tu_em'] ) || isset( $_GET['tu_name'] ) ) {
$em = isset( $_GET['tu_em'] ) ? esc_attr( $_GET['tu_em'] ) : '';
$name = isset( $_GET['tu_name'] ) ? esc_attr( $_GET['tu_name'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'email' => $em, 'name' => $name ) );
}
}
// Autofill checkout fields from user data saved in Woocommerce session
add_filter( 'woocommerce_billing_fields' , 'prefill_billing_fields' );
function prefill_billing_fields ( $address_fields ) {
// Get the session data
$data = WC()->session->get('custom_data');
// Email
if( isset($data['email']) && ! empty($data['email']) )
$address_fields['billing_email']['default'] = $data['email'];
// Name
if( isset($data['name']) && ! empty($data['name']) )
$address_fields['billing_first_name']['default'] = $data['name'];
return $address_fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Shipping information(send to another address) by default is auto we use that for autofill billing fildes
add_filter( 'woocommerce_checkout_fields' , 'ahmadyani_checkout_field_defaults', 20 );
function ahmadyani_checkout_field_defaults( $fields ) {
$user = get_user_meta(get_current_user_id());
$first_name = $user ? $user['shipping_first_name'][0] : '';
$last_name = $user ? $user['shipping_last_name'][0] : '';
$company = $user ? $user['shipping_company'][0] : '';
$shipping_address_1 = $user ? $user['shipping_address_1'][0] : '';
$shipping_address_2 = $user ? $user['shipping_address_2'][0] : '';
$shipping_city = $user ? $user['shipping_city'][0] : '';
$shipping_state = $user ? $user['shipping_state'][0] : '';
$shipping_postcode = $user ? $user['shipping_postcode'][0] : '';
$fields['billing']['billing_first_name']['default'] = $first_name;
$fields['billing']['billing_last_name']['default'] = $last_name;
$fields['billing']['billing_company']['default'] = $company;
$fields['billing']['billing_address_1']['default'] = $shipping_address_1;
$fields['billing']['billing_address_2']['default'] = $shipping_address_2;
$fields['billing']['billing_city']['default'] = $shipping_city;
$fields['billing']['billing_state']['default'] = $shipping_state;
$fields['billing']['billing_postcode']['default'] = $shipping_postcode;
return $fields;
}
Related
I have a Wordpress site with WooCommerce webshop and a client (hotel) that wants customers to order products in my webshop. I want those customers not to have to fill in the hotel information during checkout (the products are delivered to their room).
I have build a custom page for the hotel where people can place certain products in the cart, and when they want to checkout they press a custom checkout button that prefills the checkout fields. I am using this technique to achieve this: https://newpulselabs.com/prefill-checkout-fields-cartflows/
// Autofill checkout fields from URL
add_filter( 'woocommerce_checkout_fields' , 'prefill_billing_fields' );
function prefill_billing_fields ( $address_fields ) {
// Get the data from the URL
if ( isset( $_GET['fname'] ) || isset( $_GET['lname'] ) || isset( $_GET['email'] ) || isset( $_GET['company'] ) )
{
// wp_die();
$fname = isset( $_GET['fname'] ) ? esc_attr( $_GET['fname'] ) : '';
$lname = isset( $_GET['lname'] ) ? esc_attr( $_GET['lname'] ) : '';
$em = isset( $_GET['email'] ) ? esc_attr( $_GET['email'] ) : '';
$company = isset( $_GET['company'] ) ? esc_attr( $_GET['company'] ) : '';
// First Name
if( isset($_GET['fname']) && ! empty($_GET['fname']) ){
if( isset( $address_fields['billing']['billing_first_name'] ) ){
$address_fields['billing']['billing_first_name']['default'] = $fname;
}
}
// Last Name
if( isset($_GET['lname']) && ! empty($_GET['lname']) ){
if( isset( $address_fields['billing']['billing_last_name'] ) ){
$address_fields['billing']['billing_last_name']['default'] = $lname;
}
}
// Email
if( isset($_GET['email']) && ! empty($_GET['email']) ){
if(isset( $address_fields['billing']['billing_email'] )){
$address_fields['billing']['billing_email']['default'] = $em;
}
}
// Company
if( isset($_GET['company']) && ! empty($_GET['company']) ){
if(isset( $address_fields['billing']['billing_company'] )){
$address_fields['billing']['billing_company']['default'] = $company;
}
}
}
return $address_fields;
}
The url looks like this:
https://www.yourweburl.com/checkout-page/?email=name#example.com&fname=John&lname=Smith&company=ABC Company
And it works perfectly… for new visitors. If a visitor happened to have ordered before in the webshop (and used a different billing address) then that is stored locally in the browser and auto filled.
This technique will not overwrite existing/previous autofill values. I think because it is checking if the cell is empty or not:
if( isset($_GET['fname']) && ! empty($_GET['fname']) ){
if( isset( $address_fields['billing']['billing_first_name'] ) ){
$address_fields['billing']['billing_first_name']['default'] = $fname;
}
}
I tried disabling autofill on the checkout page with this:
add_filter('woocommerce_checkout_get_value','__return_empty_string', 1, 1);
Again that works so well that it removes all prefilled text. So it also removes the information I want to prefill. It works too good ;)
I prefer to alter the first code so that it always overwrites existing values, regardless of what is stored in the browser.
Can anyone help point me in the right direction of how to change the code?
Thanks a lot!
Koen
I'm trying to create a session variable in shop page and then I want to show this variable values in checkout page.
I wrote this code in woocommerce function page:
// Creating session variables
add_action( 'template_redirect', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
if ( isset( $_GET['konumu'] ) || isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
// Show session variables in checkout page
add_action('woocommerce_before_checkout_form', 'yeni_alanlar');
function yeni_alanlar($checkout){
$data = WC()->session->get('custom_data');
echo '<div id="custom_checkout_field">';
if( isset($data['konum']) && ! empty($data['konum']) && isset($data['masa']) && ! empty($data['masa']) ){
echo '<p><strong>Konumunuz : </strong>' . $data['konum'] .'<br/><strong>Masanız : </strong>' . $data['masa'] .'</p>';
}
echo '</div>';
}
URL for session variables: www.domain.com/shop/?konumu=newyork&masa_no=12
But this code working when user's second visit.
For example; user use this custom URL and visit the shop page and continue to cart and checkout page. But, my code is not working. Then, user going to this custom URL again and continue to cart & checkout page and code is working this time.
It is not working when first visit.
Why is that? How can I solve this issue?
You need to early init customer session, so we will change template_redirect to init hook using the WC_Session_Handler method set_customer_session_cookie() like:
// Creating session variables
add_action( 'init', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
// Early initialize customer session
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( isset( $_GET['konumu'] ) && isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
// Show session variables in checkout page
add_action('woocommerce_before_checkout_form', 'yeni_alanlar');
function yeni_alanlar(){
if( ( $data = WC()->session->get('custom_data') )
&& isset($data['konum']) && isset($data['masa']) ) {
echo '<div id="custom_checkout_field">
<p><strong>Konumunuz : </strong>' . $data['konum'] .'<br/>
<strong>Masanız : </strong>' . $data['masa'] .'</p>
</div>';
}
}
Now it should better work.
I would like to prevent (make these fields readonly, for example) users from changing their billing information on the WooCommerce checkout form.
I'm currently using this code snippet:
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
function mycustom_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_email']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_phone']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}
But the problem is: If the user has not filled in any of these fields in the registration, he is unable to insert his data in the checkout form because these fields are not editable.
My question is:
If the fields are not empty, how to make them readonly (or disabled)
Someone who can help me with this?
The answer of 7uc1f3r certainly works getting the user data… But since WooCommerce 3, you can use WC_Checkout get_value() dedicated method as follow:
add_filter( 'woocommerce_billing_fields', 'filter_wc_billing_fields', 10, 1 );
function filter_wc_billing_fields( $fields ) {
// On checkout and if user is logged in
if ( is_checkout() && is_user_logged_in() ) {
// Define your key fields below
$keys_fields = ['billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone'];
// Loop through your specific defined fields
foreach ( $keys_fields as $key ) {
// Check that a value exist for the current field
if( ( $value = WC()->checkout->get_value($key) ) && ! empty( $value ) ) {
// Make it readonly if a value exist
$fields[$key]['custom_attributes'] = ['readonly'=>'readonly'];
}
}
}
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
If you want this code to be also active in My account > Addresses > edit…, you will just have to remove is_checkout() && from the first IF statement.
I believe this is what you are looking for, comment with explanation added in code
function mycustom_woocommerce_billing_fields( $fields ) {
// Get current user
$user = wp_get_current_user();
// User id
$user_id = $user->ID;
// User id is found
if ( $user_id > 0 ) {
// Fields
$read_only_fields = array ( 'billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone' );
// Loop
foreach ( $fields as $key => $field ) {
if( in_array( $key, $read_only_fields ) ) {
// Get key value
$key_value = get_user_meta($user_id, $key, true);
if( strlen( $key_value ) > 0 ) {
$fields[$key]['custom_attributes'] = array(
'readonly'=>'readonly'
);
}
}
}
}
return $fields;
}
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
I am using "Pre-fill Woocommerce checkout fields with Url variables saved in session" answer code, trying to populate Woocommerce login username and password with variables saved in session.
This is my customized code in functions.php so far:
// Save user data from URL to Woocommerce session, this works fine
add_action( 'template_redirect', 'set_custom_data_wc_session' );
function set_custom_data_wc_session () {
if ( isset( $_GET['sliced_client_email'] ) || isset( $_GET['tu_name'] ) ) {
$email = isset( $_GET['sliced_client_email'] ) ? esc_attr( $_GET['sliced_client_email'] ) : '';
$pw = isset( $_GET['password'] ) ? esc_attr( $_GET['password'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'email' => $email, 'password' => $pw ) );
}
}
// Autofill checkout fields from user data saved in Woocommerce session, this is my problem
add_filter( 'woocommerce_login_form' , 'prefill_login_fields' );
function prefill_login_fields ( $xxx ) {
// Get the session data
$data = WC()->session->get('custom_data');
// Email
if( isset($data['email']) && ! empty($data['email']) )
$xxx['username']['default'] = $data['email'];
// Password
if( isset($data['password']) && ! empty($data['password']) )
$xxx['password']['default'] = $data['password'];
}
But of course I can't find the parameters to populate. On the checkout page there is $address_fields['billing_email'] but I can't find a similar parameter for the login page. Not sure what to put in $xxx and $xxx['username']['default'] and $xxx['password']['default'].
Thanks for the help!!
Alright so instead of using variables stored in session, I found a simple jQuery solution.
add_action('woocommerce_login_form','woocommerce_js_2');
function woocommerce_js_2()
{ // break out of php
?>
<script>
// Setup a document ready to run on initial load
jQuery(document).ready(function($) {
var r = /[?|&](\w+)=(\w+)+/g; //matches against a kv pair a=b
var query = r.exec(window.location.href); //gets the first query from the url
while (query != null) {
//index 0=whole match, index 1=first group(key) index 2=second group(value)
$("input[name="+ query[1] +"]").attr("value",query[2]);
query = r.exec(window.location.href); //repeats to get next capture
}
});
</script>
<?php } // break back into php
Trying to always have the "Ship to a different adress" cleared when a user is buying in WooCommerce Checkout. The following code is though doing aboslutely nothing. Does it have todo with cookies?
add_filter( 'woocommerce_checkout_get_value', 'remove_clear_checkout_shipping_fields', 10, 2 );
function remove_clear_checkout_shipping_fields( $value, $input ) {
if ( strpos( $input, 'shipping_' ) !== FALSE ) {
$value = '';
}
}
This way you can modify each field:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_first_name'] = '';
$fields['shipping']['shipping_last_name'] = '';
$fields['shipping']['shipping_company'] = '';
$fields['shipping']['shipping_address_1'] = '';
$fields['shipping']['shipping_address_2'] = '';
$fields['shipping']['shipping_city'] = '';
$fields['shipping']['shipping_postcode'] = '';
$fields['shipping']['shipping_country'] = '';
$fields['shipping']['shipping_state'] = '';
return $fields;
}
Just remove unnecessary fields.