I use conditional checkout fields as given at Conditionally unset checkout field in woocommerce .
But, It doesn't remove required validation fields?
How can I pass conditional statement within "if (true)" to remove required validation ?
At the other words, how can I check which option is selected?
Regards
if( true ){ // pass conditional statement here
unset($fields['billing']['add_house_name']); // remove field
$fields['billing']['add_building_name']['required'] = false; // remove required validation
}
return $fields;
You can override checkout fields using this code:
// 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 ) {
$address_fields['address_1']['required'] = false;
return $address_fields;
}
You can add this hook to a condition where you check inputs based on which you want to trigger validation.
Related
I am using Woocommerce register form.
My requirement for the email field is optional therefore may I ask is there any hook I can use to remove the built in email field validation?
I have another hook to do the validation for that.
List of available hooks
https://woocommerce.github.io/code-reference/hooks/hooks.html
I've used https://www.businessbloomer.com/woocommerce-checkout-customization/ as a reference in order to customize a checkout field.
add_filter( 'woocommerce_checkout_fields', 'bbloomer_required_woo_checkout_fields' );
function bbloomer_required_woo_checkout_fields( $fields ) {
$fields['billing']['billing_email']['required'] = false;
return $fields;
}
When logged in as an admin and looking at an Order in Woocommerce, there's a section with all the Custom Fields. Out of the whole list I only want it to display two of them. How do I hide the rest from this view? I don't want to delete them, but just hide from this view.
For every custom field you want hidden, add the following 4 lines of code to functions.php or using Snippets plugin:
add_filter('is_protected_meta', 'my_is_protected_meta_filter1', 10, 2);
function my_is_protected_meta_filter1($protected, $meta_key) {
return $meta_key == 'automatewoo_cart_id' ? true : $protected;
}
If you want to hide more than one, add the lines above again and change 'my_is_protected_meta_filter1' to 'my_is_protected_meta_filter2', etc
if you’re using ACF pro, there is a hook you can use to remove the field on the back end, but it’s not something that’s documented..
You could use a hook to remove specific field if is_admin() returns true.
You may need to play with this a bit to get it to work, the ACF hook is
acf/get_fields
So, for example:
add_filter('acf/get_fields', 'your_function_name', 20, 2);
function your_function_name($fields, $parent) {
// remove the fields you don't want
return $fields;
}
$fields can be a nested array of fields => sub_fields.
You need to set the priority > 10 to run after the internal ACF filter
For orders in Woocommerce the post type is 'shop_order', so your code should be:
add_action( 'add_meta_boxes', 'remove_shop_order_meta_boxe', 90 );
function remove_shop_order_meta_boxe() {
remove_meta_box( 'postcustom', 'shop_order', 'normal' );
}
I am trying to customise the Woocommerce myaccount page, in particular the edit address page.
I want to display both the shipping + billing address forms on a single page. Ideally, in a single form with a one save button. I also need to remove a lot of the fields, so that it's a much simpler form of just an address (no name, company, etc).
I have implemented the code found on This Answer. It works nicely in that it shows both forms. However, I cannot remove the fields from the forms. If I try code like this:
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_billing_fields( $fields ) {
unset($fields['billing_country']);
unset($fields['billing_company']);
unset($fields['billing_first_name']);
unset($fields['billing_last_name']);
unset($fields['billing_phone']);
unset($fields['billing_email']);
return $fields;
}
function custom_override_shipping_fields( $fields ) {
unset($fields['shipping_country']);
unset($fields['shipping_company']);
unset($fields['shipping_first_name']);
unset($fields['shipping_last_name']);
return $fields;
}
It doesn't work, the fields are no longer shown but the form does not save on click... it just redirects to /my-account/edit-address/billing/ - and doesn't save. (the same form shown on this page doesn't save either).
I've also tried:
foreach ( $billing_fields as $key => $field ) :
if($key != 'billing_first_name' && $key != 'billing_last_name') :
woocommerce_form_field( $key, $field, $userMeta[$key][0] );
endif;
endforeach;
This removes the field from displaying, BUT the validation still exists - and any filter code I add to functions using
woocommerce_checkout_fields to remove the validation doesn't seem to affect this form at all.
Is there a way to either:
Remove fields from this form generated by woocommerce_form_field including the validation?
Create a custom form that allows me to set the input fields manually in the code, and update any fields that are there, ignoring the validation from Woocommerce completely?
This should work 100%. You need to state whether the fields you are removing is from billing or shipping and this is done by adding the ['billing'] or ['shipping'], whichever it is.
After this, adding the function directly to woocommerce_checkout_fields will apply both for billing and shipping.
For phone and company fields you can disable it in admin panel itself, do it.
Edit: And yes, all validation that was involved with the fields in the past will be removed. You can then apply any validation you need.
add_filter( 'woocommerce_checkout_fields' , 'brandimagemarketer_remove_billing_fields_checkout' );
function brandimagemarketer_remove_billing_fields_checkout( $fields ) {
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_email']);
unset($fields['shipping']['shipping_country']);
unset($fields['shipping']['shipping_first_name']);
unset($fields['shipping']['shipping_last_name']);
unset($fields['shipping']['shipping_email']);
return $fields;
}
I'm using Contact Form 7 on a site and need to alter the checkbox submitted data. The checkbox has a label called 'Tick here if you dont want to receive further marketing', when this is checked the value sent in the admins notification email displays the checkbox label.
So it looks like:
Tick here if you dont want to receive further marketing: Tick here if you dont want to receive further marketing
I want to alter it so when its checked the value posted is No.
I believe I can use the following action hook to achieve this but I dont know how to check if the checkbox has been ticked within this function and modify its value.
Any help much appreciated.
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
// make action magic happen here...
};
// add the action
add_action( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1 );
I believe you can just use:
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
//'checkbox-name' is the name that you gave the field in the CF7 admin.
$value = $array['checkbox-name'];
if( !empty( $value ) ){
$array['checkbox-name'] = "New Value";
}
return $array;
};
add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1 );
in some content type form I added a checkbox. When this checkbox is checked I want to remove some of the submitted data.
To do so I created a custom module (my_module.module):
function my_module_form_alter(&$form, &$form_state) {
// ...
$form['#submit'][] = 'my_module_form_alter_submit';
}
function my_module_form_alter_submit($form_id, $form_values) {
drupal_set_message(t('Submit Function Executed!'));
}
How can I tell this module to refer only to the form of a certain containt type? And how can I remove data when it is submitted?
Assuming you are altering a node edit form, you can either conditionally add the submit callback such as (in your hook_form_alter):
if(isset($form['#node']) && $form['type']['#value'] == 'page') {
$form['#submit'][] = 'my_module_form_alter_submit';
}
or, you could check the $form argument in the submit callback in a similar fashion.
You are missing the third argument to the hook_form_alter which should be $form_id, and your submit callback should take the arguments such as:
function my_module_form_alter_submit($form, &$form_state) { ... }
See also:
http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_form_alter/6
http://api.drupal.org/api/drupal/developer%21topics%21forms_api.html/6
To remove data after the submit, in your form_alter function, just use unset() on the form field. unset($form['my-field']);