WooCommerce reorder account passwort field on checkout - php

I try to reorder the WooCommerce account password field on the checkout page with the code below. However, it creates a new field but does not move the account password field.
I followed the instructions from that page here: https://jeroensormani.com/ultimate-guide-to-woocommerce-checkout-fields/#sorting-checkout-fields
How can I reorder the account password filed on WooCommerce checkout?
Goal: The Account password field should be after the email field.
function js_sort_checkout_fields( $fields ) {
$fields['billing']['account_password']['priority'] = 111;
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'js_sort_checkout_fields' );

We was able to solve it.
function move_password_field($checkout_fields){
// Move Account Password into Billing
$checkout_fields['billing']['account_password'] = $checkout_fields['account']['account_password'];
// Remove Password from Billing
unset($checkout_fields['account']['account_password']);
return $checkout_fields;
}
add_filter('woocommerce_checkout_fields', 'move_password_field', 999);

Related

WooCommerce place an order without filling in email address

I am trying to place an order without filling in the email field at the checkout form.
I used this code to remove all field values (except country) :
add_filter( 'woocommerce_checkout_get_value' , 'clear_checkout_fields' , 10, 2 );
function clear_checkout_fields( $value, $input ){
if( $input != 'billing_country' )
$value = '';
return $value;
}
When placing the order without filling in the email field the order gets placed with the email of the account it is placed with. I want to be able to place an order without filling in an email address so it shows no email address on the orders page on the WooCommerce dashboard.
Any suggestions?
Set default value for this field to admin email.
add_filter( 'woocommerce_checkout_fields', 'woo_default_email' );
function woo_default_email($fields) {
$fields['billing']['billing_email']['default'] = get_option('admin_email');
return $fields;
}
Then just remove field from output in your template and email message.

Woocommerce set billing field value equal to other billing field value

I want the billing field 'company email' to be equal to Billing E-mail address.
I thought this would work but I only get an empty field:
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1 );
function custom_admin_billing_fields( $billing_fields ) {
$billing_fields['tlr_company_email']['value'] = $billing_fields['email']['value'];
return $billing_fields;
}
I think the proper way of doing it is to update once the payment is completed, after the checkout. The following code is hooked to the payment complete trigger. It will copy the billing email to the custom company billing email.
add_action('woocommerce_payment_complete', 'replicate_billing_email', 100, 1);
function replicate_billing_email($order_id){
$order = new WC_Order($order_id); //obatin the completed order as object
$billing_email = $order->get_billing_email(); //get the billing email address
update_post_meta($order_id, 'tlr_company_email', $billing_email); //update the company email
update_post_meta($order_id, '_tlr_company_email', $billing_email); //it maybe saved like that, with underscore, ommit if not needed
}
To test it, complete a test order.

Set Woocommerce checkout field optional for a specific payment gateway

I was setting up WooCommerce Checkout and I wanted to make the checkout fields optional for PayPal checkout as PayPal also require such data.
While I still want other gateways like Stripe to require checkout field being filled.
I am new to PHP. Here's the code I tried, but it didn't work:
add_filter( 'woocommerce_default_address_fields' ,'custom_override_default_address_fields' );
$answer = $_POST['payment_method'];
if ( $answer == "cppec_paypal" ) {
function custom_override_default_address_fields( $address_fields ) {
$address_fields['address_1']['required'] = false;
return $address_fields;
}
...
I want the field to be optional for PayPal, but required for Stripe.
The second option is that we somehow bring the paypal button from cart here as it didnt required the data but the checkout does
Thanks

Remove phone from billing/shipping fields everywhere in Woocommerce?

I don't need to collect customer phone numbers in woocommerce so I've used the following code to remove them from the checkout process:
add_filter( 'woocommerce_checkout_fields', 'pbj_woocommerce_checkout_fields' );
function pbj_woocommerce_checkout_fields( $fields ) {
unset($fields['billing']['billing_phone']);
unset($fields['shipping']['shipping_phone']);
$fields['billing']['billing_email']['class'] = array('form-row-wide');
return $fields;}
(sorry for hideous code pasting, first time!)
That works great on the checkout page but once in the "my account" area, the user is still required to add a phone number if they edit their billing or shipping address. I have added this code:
function pbj_remove_billingphone($fields) {
unset( $fields ['billing_phone'] );
return $fields;}
add_filter( 'woocommerce_billing_fields', 'pbj_remove_billingphone' );
which has removed it from billing, but I can't get anything to work to remove it from shipping. Any help on either something that universally removes the phone number requirement everywhere, or tips on what to filter/unset to remove the shipping phone number on the account page? Thank you!
Here is the complete way to do it in account and checkout pages (for both pages):
// Remove billing phone (and set email field class to wide)
add_filter( 'woocommerce_billing_fields', 'remove_billing_phone_field', 20, 1 );
function remove_billing_phone_field($fields) {
$fields ['billing_phone']['required'] = false; // To be sure "NOT required"
$fields['billing_email']['class'] = array('form-row-wide'); // Make the field wide
unset( $fields ['billing_phone'] ); // Remove billing phone field
return $fields;
}
// Remove shipping phone (optional)
add_filter( 'woocommerce_shipping_fields', 'remove_shipping_phone_field', 20, 1 );
function remove_shipping_phone_field($fields) {
$fields ['shipping_phone']['required'] = false; // To be sure "NOT required"
unset( $fields ['shipping_phone'] ); // Remove shipping phone field
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Normally shipping email and phone fields doesn't exist by default in WooCommmerce

Woocommerce Override Email Label

I edited the Order Notes field in the checkout to be:
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['placeholder'] = 'Enter Purchase Order Number if applicable';
$fields['order']['order_comments']['label'] = 'Purchase Order #';
return $fields;
}
However, the confirmation and notification emails still say "Note" as the field label. I just want to change the label in the emails.
There are a whole different set of filters for the emails that are generated by woocommerce. Changing the label for the checkout field only changes the note in the checkout area! the label isn't passed through to the order emails, or to paypal for that matter.
You'll have to add another filter to override the field name in the email. Something like this:
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'My Field';
return $keys;
}

Categories