I came across with this How to blank all WooCommerce checkout fields by default except country? similar question which has an answer code that meet my needs, but this code will blank all woocommerce shipping and also billing checkout field when customer click checkout button.
if I want the shipping fields only to be blank, how the code will looks like?
To blank all WooCommerce shipping checkout fields, you will use the following:
add_filter('woocommerce_checkout_get_value', 'checkout_get_value_filter', 10, 2 );
function checkout_get_value_filter( $value, $input ) {
if ( strpos($input, "shipping_") === 0 ) {
return '';
}
return $value;
}
Or also this specifying each related shipping field key:
add_filter('woocommerce_checkout_get_value', 'checkout_get_value_filter', 10, 2 );
function checkout_get_value_filter( $value, $input ) {
$key_fields = array(
'shipping_first_name',
'shipping_last_name',
'shipping_company',
'shipping_country',
'shipping_address_1',
'shipping_address_2',
'shipping_city',
'shipping_country',
'shipping_state',
'shipping_postcode',
);
if ( in_array($input, $key_fields) ) {
return '';
}
return $value;
}
Code goes on functions.php file of your active child theme (or active theme). It should works.
Related
I would like to ask you for help to create a custom field.
How to create, without a plugin, a custom field "frontier_agent" for WooCommerce orders based on created_via property as follows:
If created_via value is "amazon" then frontier_agent value should be set to 7000.
If created_via value is "checkout" then frontier_agent value should be set to 7500.
Any help is appreciated.
The following that will create a frontier_agent custom field based on create_via order property:
add_action( 'woocommerce_order_status_changed', 'create_frontier_agent_order_custom_field', 10, 4 );
function create_frontier_agent_order_custom_field( $order_id, $old_status, $new_status, $order ) {
if( ! $order->get_meta('frontier_agent') ) {
if( $order->get_created_via() === 'checkout' ) {
$value = '7500';
} elseif( $order->get_created_via() === 'amazon' ) {
$value = '7000';
}
if( isset($value) ) {
$order->update_meta_data('frontier_agent', $value);
$order->save();
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I have conditionally unset the billing address fields on the woocommerce checkout page, but when submitting place order, woocommerce displays error:
Please enter an address to continue.
I then tried adding a filter on woocommerce_default_address_fields to make the fields optional - which only seems to work if the fields aren't unset.
// make address fields optional - this works fine without the next filter
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
function filter_default_address_fields( $address_fields ) {
// Only on checkout page
if( ! is_checkout() ) return $address_fields;
// All field keys in this array
$fields = array('country','company','address_1','address_2','city','state','postcode');
// Loop through each address fields (billing and shipping)
foreach( $fields as $key_field )
$address_fields[$key_field]['required'] = false;
return $address_fields;
}
//conditionally unset fields
add_filter( 'woocommerce_checkout_fields' , 'simplify_checkout' );
function simplify_checkout( $fields ) {
$customField = false;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if cart item has attribute
if ( ! empty ($cart_item['custom_attribute']) ) $registry = true;
}
if( $customField ) {
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
}
return $fields;
}
I'm hoping to still be able to conditionally hide / unset the fields along with a successful checkout submission.
I had this problem with the validation message two days ago. I only sell to one country, so I created following workaround. Maybe if you have fewer countries to sell this could work for you too:
Uncomment/Remove unset($fields['billing']['billing_country']);
Go to WooCommerce -> Settings -> Sell to specific countries and
enter your countries.
Now the validation message is inactive, as the Country field is
mandatory. You can remove it now with CSS.
#billing_country_field {
display: none;
}
Just change the action name and try it:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
add_action( 'woocommerce_after_shop_loop_item_title', 'filter_default_address_fields' );
On Woocommerce, we need to remove the shipping-methods from the Cart section an add it to the Checkout page only.
Any track or help should be really appreciated?
There will be multiple ways to do it depending on the "why?" and on "for what?" you need this:
1) Hide shipping related from cart - The easiest way;
add_filter( 'woocommerce_cart_ready_to_calc_shipping', 'disable_shipping_on_cart' );
add_filter( 'woocommerce_cart_needs_shipping', 'disable_shipping_on_cart' );
function disable_shipping_on_cart( $enabled ){
return is_checkout() ? true : false;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
But it will not remove the shipping methods (or shipping packages) from session…
2) Remove all shipping methods (and shipping packages) everywhere except in checkout page:
// Shipping methods
add_filter( 'woocommerce_package_rates', 'keep_shipping_methods_on_checkout', 100, 2 );
function keep_shipping_methods_on_checkout( $rates, $package ) {
if ( ! is_checkout() ) {
// Loop through shipping methods rates
foreach( $rates as $rate_key => $rate ){
unset($rates[$rate_key]); // Remove
}
}
return $rates;
}
// Shipping packages
add_filter( 'woocommerce_shipping_packages', 'keep_shipping_packages_on_checkout', 20, 1 );
add_filter( 'woocommerce_cart_shipping_packages', 'keep_shipping_packages_on_checkout', 20, 1 );
function keep_shipping_packages_on_checkout( $packages ) {
if ( ! is_checkout() ) {
foreach( $packages as $key => $package ) {
WC()->session->__unset('shipping_for_package_'.$key); // Remove
unset($packages[$key]); // Remove
}
}
return $packages;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
It will remove all shipping methods and all shipping packages from cart and WC_Session.
The related registered WC_Session data will be something like:
WC_Session_Handler Object
(
[_data:protected] => Array
(
[previous_shipping_methods] => a:1:{i:0;a:3:{i:0;s:16:"free_shipping:10";i:1;s:12:"flat_rate:14";i:2;s:15:"local_pickup:13";}}
[shipping_method_counts] => a:1:{i:0;i:3;}
[chosen_shipping_methods] => a:1:{i:0;s:16:"free_shipping:10";}
)
)
without shipping package…
It will only keep the previous shipping methods and the previous chosen shipping method for customers that have already purchased something before.
I am trying to add mycustomfield21 (added to checkout with woo checkout manager) on bulk order details on woocommerce dashboard. Here is a screenshot to show what I need:
EDITED QUESTION
I already instert 2 fields, wich are first name (myfield21) and surname (myfield22), How can I do to display a white space between them? because it shows like "first namesurname"
Update - Nov. 2018 - Added compatibility with Woocommerce version 3.3+
This is possible hooking a custom function in manage_shop_order_posts_custom_column action hook. Below I am adding the billing phone to the existing Order ("Pedido") column data:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 10, 2 );
function custom_orders_list_column_content( $column, $post_id ) {
if ( $column == 'order_title' || $column == 'order_number' )
{
// The billing phone for example (to be repaced by your custom field meta_key)
$custom_field_value = get_post_meta( $post_id, '_billing_phone', true );
if( ! empty( $custom_field_value ) )
echo $custom_field_value;
}
}
*Code goes in function.php file of your active child theme (or active theme) . Tested and works
I'm setting up a store that only sells to one city. I'm trying to figure out how I can restrict the city option to a pre-filled field.
I've already limited the country and using the following code in my functions.php:
add_filter( 'default_checkout_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_state', 'change_default_checkout_state' );
function change_default_checkout_country() {
return 'PK'; // country code
}
function change_default_checkout_state() {
return 'SD'; // state code
}
And I've pre-selected the state using the following:
add_filter( 'woocommerce_states', 'bbloomer_custom_woocommerce_states' );
function bbloomer_custom_woocommerce_states( $states ) {
$states['PK'] = array(
'SD' => 'Sindh',
);
return $states;
}
But... Now comes the city and I am lost. I've tried using $fields['billing']['billing_city'] as well as $fields['billing']['billing_city']['value'] to no avail.
I want the customer to be limited to Karachi. I am not very familiar with WooCommerce filters, so I need help on achieving this.
Since woocommerce 3, default_checkout_country and default_checkout_state filter hooks are now deprecated and replaced.
Also in your last function, if you want to restrict just to one city, you should not include your city in the array of states. Instead you should return only one possible value.
So this will be your code:
// default checkout country
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country' );
function change_default_checkout_country() {
return 'PK'; // country code
}
// default checkout state
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_state' );
function change_default_checkout_state() {
return 'SD'; // state code
}
// Setting one state only
add_filter( 'woocommerce_states', 'custom_woocommerce_state', 10, 1 );
function custom_woocommerce_state( $states ) {
// Returning a unique state
return array('PK' => array('SD' => 'Sindh'));
}
// Only one city at checkout
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields', 10, 1 );
function custom_checkout_fields( $fields ) {
$fields['billing']['billing_city']['type'] = 'select';
$fields['billing']['billing_city']['options'] = array('Karachi' => 'Karachi');
$fields['shipping']['shipping_city']['type'] = 'select';
$fields['shipping']['shipping_city']['options'] = array('Karachi' => 'Karachi');
return $fields;
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for wooCommerce versions 3+
Once added the code above and saved, you will need in WooCommerce > Settings > General to set locations this way:
Then you will get something like this on checkout:
Both dropdown have only one value… So you get what you are expecting.