How to display a $_SESSION variable in custom woocommerce checkout field? - php

I am able to verify by viewing the "Application" tab in the dev tools that my session variable has carried over to the checkout page; and I have the WC hook setup that allows me to change the "default"/value of the field I want to insert data into.
However, I am unable to get them to read each other.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['label'] = "Custom Neon Details (If included)";
$fields['order']['order_comments']['default'] = $_SESSION["customtext"];
var_dump( $fields );
return $fields;
}
As you can see from the attached image, my session variable is present on that page. How now, do I get it to add that session variable to $fields['order']['order_comments']['default']?
Any help is appreciated! Thanks.

Use session_start(); before accessing the $_SESSION variable:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
session_start();
$fields['order']['order_comments']['label'] = "Custom Neon Details (If included)";
$fields['order']['order_comments']['default'] = $_SESSION["customtext"];
var_dump( $fields );
return $fields;
}
Alternatively you can use jQuery with sessionStorage:
// set the value of the order comment field via session storage
add_action( 'wp_footer', 'set_value_order_comments_field_via_session_storage' );
function set_value_order_comments_field_via_session_storage() {
?>
<script type="text/javascript">
jQuery(function($){
var customtext = sessionStorage.getItem("customtext");
if ( customtext.length ) {
$('#order_comments').val(customtext);
}
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.

Related

How to clear specific billing field value from WooCommerce checkout

I am trying to clear the billing_po_no field value from my WooCommerce checkout billing Form, by adding the following code into my functions.php file:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_po_no'] = '';
return $fields;
}
But it does not appear to be working. Can someone point me in the right direction?
Try the following instead:
add_filter( 'woocommerce_checkout_get_value' , 'clear_specific_checkout_field' , 10, 2 );
function clear_specific_checkout_field( $value, $input ){
if( $input === 'billing_po_no' )
$value = '';
return $value;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Set maxlength for checkout postcode field in WooCommerce

I'm trying to delete or increase the default maxlength (nowadays set to 4) on a WordPress site I developed using Woocommerce.
Up to now I've tried all these functions on my childtheme's functions.php
#OPTION 1
function wpse215677_checkout_fields ( $fields ) {
$fields['postcode']['maxlength'] = 5;
return $fields;
}
add_filter('woocommerce_default_address_fields', 'wpse215677_checkout_fields');
#OPTION 2 - WITH AND WITHOUT ['custom_attributes']
add_filter( 'woocommerce_checkout_fields' , 'my_override_checkout_fields');
function my_override_checkout_fields( $fields ) {
$fields['billing']['postcode']['custom_attributes']['maxlength'] = 5;
$fields['billing']['billing_postcode']['custom_attributes']['maxlength'] = 5;
return $fields;
}
#OPTION 3 - WITH AND WITHOUT ['custom_attributes']
function my_wc_custom_billing_fields( $fields ) {
$fields['billing_postcode']['custom_attributes']['maxlength'] = 5;
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'my_wc_custom_billing_fields' );
function my_wc_custom_shipping_fields( $fields ) {
$fields['shipping_postcode']['custom_attributes']['maxlength'] = 5;
return $fields;
}
add_filter( 'woocommerce_shipping_fields', 'my_wc_custom_shipping_fields' );
All of them worked fine on the cart calculator (now I can write any number over 4) but when I go to the checkout page and try to write a number over 4 characters on the postcode (shipping or billing), the input still remains with a maxlength of 4 (I've inspected it with Chrome's tools).
Is there any way I can overwrite the entire input on the checkout to allow me to write more than 4 characters on this input?
Or am i doing something wrong with these functions and that is why they are not working on the checkout page?
One way is to do it via jQuery, although the code you have already posted here works without any problems
function action_wp_footer() {
// Returns true on the checkout page, when false, return
if ( ! is_checkout() ) return;
?>
<script>
jQuery(document).ready(function($){
$( '#billing_postcode' ).attr( 'maxlength', '5' );
$( '#shipping_postcode' ).attr( 'maxlength', '5' );
});
</script>
<?php
}
add_action( 'wp_footer', 'action_wp_footer' );

WooCommerce My Account Dashboard: Direct link to edit billing address

As I sell virtual products I don’t need the WooCommerce shipping address fields. I've already removed them from the checkout page and also want to remove them from the “My Account” page.
In the dashboard under the „Edit address“ tab the user should be directed directly to the page where he can edit the billing address. The page that shows the billing / shipping address and where the user has to click on "Edit address" is not longer needed.
Is there a way to achieve this? Any help is appreciated.
You can unset the Address endpoint and create a new one for Billing. For example, in your functions.php add the following code.
//1
function add_d_endpoint() {
add_rewrite_endpoint( 'billing', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'add_d_endpoint' );
//2
function d_query_vars( $vars ) {
$vars[] = 'billing';
return $vars;
}
add_filter( 'query_vars', 'd_query_vars', 0 );
//3
function add_d_link_my_account( $items ) {
$items[ 'billing' ] = 'Billing Address'; //The title of new endpoint
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_d_link_my_account' );
//4
function d_content() {
echo WC_Shortcode_My_Account::edit_address( 'billing' ); //The content of new endpoint
}
//5
add_action( 'woocommerce_account_billing_endpoint', 'd_content' );
// Note: add_action must follow 'woocommerce_account_{your-endpoint-slug}_endpoint' format
//6
/** Remove Address from My Account Menu **/
add_filter( 'woocommerce_account_menu_items', 'dsx_remove_my_account_dashboard' );
function dsx_remove_my_account_dashboard( $menu_links ) {
unset( $menu_links[ 'edit-address' ] ); //remove the address from endpoint
return $menu_links;
}
Then go to your Dashboard > Settings > Permalinks, then click the button Save, that should do it.
Alternatively, you can override the my-address.php located on woocommerce/templates/myaccount, just use WC_Shortcode_My_Account::edit_address( 'billing' );.
Or you can redirect into Billing when user(s) is trying to access the Edit-Address endpoint, try using the below code in your functions.php.
function redirect_to_billing( $wp ) {
$current_url = home_url(add_query_arg(array(),$wp->request));
$billing = home_url('/account/edit-address/billing');
/** If user is accessing edit-address endpoint and it's not the billing address**/
if(is_wc_endpoint_url('edit-address') && $current_url !== $billing){
wp_redirect($billing);
exit();
}
}
add_action( 'parse_request', 'redirect_to_billing' , 10);

Hide address if already completed WooCommerce

I'm looking for a way to hide the billing address on the checkout page of my woocommerce theme if the user has already filled up the billing form (from a previous order or if the user has done it previously from the "my account" page).
I've found ways to hide the billing / shipping form completely on the checkout page if the user is logged in (see below), however I can't find a way to do the above.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
if( is_user_logged_in() ){
unset($fields['billing']);
$fields['billing'] = array();
}
return $fields;
}
Any idea?
Thank you!
It will depend what you consider to be a fully completed address i made snippet function you can use to go further with.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
if( is_user_logged_in() && !has_billing()){
unset($fields['billing']);
$fields['billing'] = array();
}
return $fields;
}
// Check the meta of Postcode and Country if they are entered.
function has_billing($user_id = false){
if(!$user_id)
$user_id = get_current_user_id();
$shipping_postcode = get_user_meta( $user_id, 'billing_postcode', true );
$shipping_country = get_user_meta( $user_id, 'billing_country', true );
// Fetch more meta for the condition has needed.
if($shipping_postcode && $shipping_country){
return true;
}
return false;
}
Note: the prefix shipping_ there is one for billing ( billing_ ).
Edit: Here the meta key billing_address_1 and billing_address_2 always the prefix can be either billing_ or shipping_
Also if for some reason you don't have a shipping or billing address associated in the user meta keys but the customer once did an order you can check this code to fetch order address.
Woocommerce WC_Order get_shipping_address() not returning as array (old post might not be valid anymore)

Pre-filing checkout post code with a custom value

I have been using this custom function below in the previous versions of WooCommerce in order to pre-fill the City and ZIP code fields:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city']['default'] = 'Beverly Hills';
$fields['billing']['billing_postcode']['default'] = '90210';
return $fields;
}
It has been working great until the new WC updates.
The city still works, but the default ZIP code field doesn't seem to work anymore. It doesn't automatically pre-polulate the value.
Anything changed? Is there any other workaround for this?
Thanks
Setting a value for 'post-code' fields doesn't work anymore as there is an autocomplete feature. Even when disable "autocomplete", this doesn't work. So the workaround is to use jQuery in this case.
So your code is going to be:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields', 10, 1 );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city']['default'] = 'Beverly Hills';
$fields['billing']['billing_postcode']['autocomplete'] = "off"; // Removing autocomplete
return $fields;
}
add_action( 'woocommerce_after_checkout_form' , 'my_custom_checkout_field_postcode' );
function my_custom_checkout_field_postcode( ) {
?>
<script>
(function($){
$('#billing_postcode').val('90210');
})(jQuery);
</script>
<?php
}
This will set correctly your the desired value in "post-code" checkout billing field.
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Not necessary have to use java-script when you use the woocommerce callback made for the job: 'woocommerce_checkout_get_value'.
The 'woocommerce_after_checkout_form' call is made for setting the attributes of the check out fields, such as disabling the autocomplete.
The point to note is this function will be repeatedly called for each and every field within the check out form. So you switch on the field and return the value you wish assigned to the check-out form:
Based on your above code. here ya go...
function populating_checkout_fields ($fields, $input)
{
global $woocommerce;
switch($input)
{
case 'billing_city':
$FieldValue = 'Beverly Hills';
return $FieldValue;
break;
}
return $fields; // return the default value
}
add_filter( 'woocommerce_checkout_get_value', 'populating_checkout_fields', 10, 2 );
function ModifyAutoComplete($fields)
{
$fields['billing']['billing_postcode']['autocomplete'] = null;
}
add_filter( 'woocommerce_checkout_fields' , 'ModifyAutoComplete', 10, 1 );

Categories