unsetting custom fields in checkout - woocommerce - php

I can unset fields from billing and shipping but why I can't unset fields from additional field section. I am adding a condition on these fields. Maybe I am using wrong meta keys.
function wc_remove_checkout_field( $fields ) {
unset( $fields['billing']['test_field'] ); //this one working
unset( $fields['additional']['delivery_time'] ); //this one not even if I replace additional with order.
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_remove_checkout_field' );
Creating fields usings this plugin WooCommerce Checkout Field Editor

You can use this filter to remove the order notes field:
add_filter('woocommerce_enable_order_notes_field', '__return_false');
If you want to stick with your method using unset, you need to replace "additional" with "order":
unset($fields['order']['order_comments']);

Related

How to clear WooCommerce billing email and billing phone checkout fields after place order?

How do I clear a specfic checkout fields in WooCommerce after placing an order?
For example Billing email and Billing Phone. So that when the registered customer makes his/her next order, he/she has to fill that fields again?
I see this code, but this clean all the fields, i need only a specific field. Any advice?
add_filter('woocommerce_checkout_get_value','__return_empty_string',10);
The woocommerce_checkout_get_value hook has 2 arguments:
$value argument that is returned
$input argument to target checkout field(s)
So in your case, you get:
function filter_woocommerce_checkout_get_value( $value, $input ) {
// Target checkout fields. Multiple fields can be added, separated by a comma
if ( in_array( $input, array( 'billing_phone', 'billing_email' ) ) ) {
$value = '';
}
return $value;
}
add_filter( 'woocommerce_checkout_get_value' , 'filter_woocommerce_checkout_get_value' , 10, 2 );
You could probably achieve this with some custom javascript:
document.getElementById('fieldID').value = ''
Or jQuery:
$('#fieldID').val('');

Get custom field value in woocommerce_checkout_create_order hook

What am I doing wrong with this code? I am not able to retrieve the post_meta value. I know it is saved correctly because the values are displayed in the order edit page. I am just not able to retrieve in this function:
add_action( 'woocommerce_checkout_create_order', 'rev_change_total_on_checkout', 20, 1 );
function rev_change_total_on_checkout( $order ) {
$new_total_price = get_post_meta( $order->id, '_rev_fee_estimate', true);
$order->set_total($new_total_price );
}
Basically, I am trying to change the total order value after the checkout is submitted based on the values programmatically generated in a custom field value I added to the checkout form. If I hardcode the value of $new_total_price, I am able to achieve this, so I know this function works. I only need to retrieve the saved value of the custom field to finish this.
In think that _rev_fee_estimate is not saved to database yet in this hook as it's a custom field. So you can get anything related.
Instead you need to get the posted value. I will use the key _rev_fee_estimate but it can be something else (you can check for it, inspecting the generated html code for this custom field on checkout page. The necessary key is the attribute "name" value in this field)…
The code:
// Save an additional coverstart value in in the order post meta dat
add_action( 'woocommerce_checkout_create_order', 'initial_coverstart_custom_field_save', 20, 1 );
function initial_coverstart_custom_field_save( $order ) {
if( ! isset($_POST['_rev_fee_estimate']) ) return;
if( ! empty($_POST['_rev_fee_estimate']) )
{
$new_total_price = (float) sanitize_text_field( $_POST['_rev_fee_estimate'] )
$order->set_total( $new_total_price );
}
}
Code goes in function.php file of your active child theme (or active theme). It should work once you will have checked that you have the correct key for this custom field.

Hide or Remove the Additional tab on Woocommerce checkout

I want to remove the additional tab from checkout step.I tried the followoing code ,its remove the notes field but the tab still exist.
add_filter( 'woocommerce_checkout_fields' , 'bbloomer_remove_checkout_order_notes' );
function bbloomer_remove_checkout_order_notes( $fields ) {
unset($fields['order']['order_comments']);
return $fields;
}
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
I tried by using css.That hide the tab and its data but didn't move the tab to next on click.So i think it will be good if i found some filter type code.Thanks

Removing the password field in woocommerce checkout page

I am trying to remove the password field in the checkout page.
Here is what I have tried (code is in my functions.php theme file):
// Hook in
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( $address_fields ) {
$checkout_fields['account']['required'] = false;
return $checkout_fields;
}
But it didn't work.
What is the hook for removing the password field in woocommerce checkout page?
Thanks.
The hook woocommerce_default_address_fields is only used for default address fields and not for the account fields.
To make the passwords fields optional you should need using woocommerce_checkout_fields hook, this way:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
// There is 2 password fields
fields['account']['account_password']['required'] = false;
fields['account']['account_password-2']['required'] = false;
return $fields;
}
To remove those passwords fields you should need to use unset() PHP function this way:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['account']['account_password']);
unset($fields['account']['account_password-2']);
return $fields;
}
But I am not sure that is really possible, as it's something mandatory in WooCommerce checkout process, when you have enabled the option to register in checkout page…
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Official Reference: Customizing checkout fields using actions and filters

Remove a checkout field if cart items are from some specific product categories

I use WooCommerce Checkout Manager to add a custom field in my billing section, but I need to show this field only if I have some product from a specified category. The fields is required.
I wrote this code :
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field');
function wc_ninja_remove_checkout_field( $fields ) {
$categories = array( 'prodotti-in-polvere-e-bustine', 'gel-e-creme', 'prodotti-in-capsule', 'prodotti-plantari', 'prodotti-liquidi', 'area-riservata' );
if ( is_product_category( array( $categories ) ) ) {
unset( $fields['billing']['billing_myfield12'] );
}
return $fields;
}
This function just only set to display:none the field, in fact if I click on checkout there is an error like "the field myfield is required", but I need to remove my field not set to display none.
Please any idea?
I have the latest version of WooCommerce.
Thanks.

Categories