Make Billing Phone required on Checkout Field Based on Shipping Method - Woocommerce - php

I want to set the billing phone number required or not required based on the shipping method.
I have set the shipping method as an array and checked it from the session selected shipping method but it not working
add_filter('woocommerce_checkout_fields', 'fire_remove_billing_checkout_fields');
function fire_remove_billing_checkout_fields($fields) {
global $woocommerce;
$methods = array('flat_rate:2', 'flat_rate:3', 'flat_rate:17', 'flat_rate:20');// Set shipping method to hide the checkout field(s).
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if(in_array($chosen_shipping, $methods)){
$fields['billing']['billing_phone'][ 'required' ] = true;
}else{
$fields['billing']['billing_phone'][ 'required' ] = false;
}
return $fields;
}

Your code works well for me. I've added my shipping rate IDs and it works as expected, however refresh of the page is necessary, because WooCommerce doesn't refresh the billing fields natively on update_checkout trigger.
You should either reload the page using Javascript/jQuery on updated_checkout event, or reload only the billing fields using AJAX like so:
add_action( 'wp_footer', 'reload_billing_fields_on_updated_checkout' );
function reload_billing_fields_on_updated_checkout() {
?>
<script>
jQuery(document.body).on('updated_checkout', () => {
jQuery('.woocommerce-billing-fields').load(`${location.href} .woocommerce-billing-fields>*`, '');
});
</script>
<?php
}
Keep in mind that it might take a short while for those fields to reload, so you might want to add some loading CSS classes, or maybe a spinner.

Related

Woocommerce autofocus on input fields "billing_first_name"

I have custom checkout page. First I'm showing content with added to cart items and than using this action to show billing form
<?php do_action( 'woocommerce_checkout_billing' ); ?>
Page scrolls down because of the autofocus on first name field.
Tried to use filters like this one
add_filter('woocommerce_billing_fields','disable_autofocus_billing_firstname');
function disable_autofocus_billing_firstname($fields) {
$fields['billing_first_name']['autofocus'] = false;
return $fields;
}
Also tried to blur the field with jQuery and vanila JS no effect. Is there is some updates for Woo that I'm missing? Thanks!
For overriding core fields need to use the hook 'woocommerce_checkout_fields'
You need to use the snippet below in order to disable the autofocus on checkout field:
/* Disable autofocus at checkout */
function custom_checkout_disable_autofocus_firstname( $fields ) {
$fields['billing']['billing_first_name']['autofocus'] = false;
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_disable_autofocus_firstname' );

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

Hide shipping address at woocommerce checkout based on a shipping table rate choice

I have been working to hide my woocommerce checkout shipping address if a user selects a shipping table rate option that has a Label of "Local Pickup". Most code snippets I have found trigger based on shipping_method. I tried a shipping-method like `table_rate:10:1', to no avail. Any ideas?
Also, I am not completely clear on how to designate the specific row in the table - I inspected the code and found the value above, but am unsure if its correct.
Shipping related plug-ins: Woocommerce Table Rate Shipping
Trying to use the following code as a base:
add_filter( 'woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields' );
function xa_remove_billing_checkout_fields( $fields ) {
global $woocommerce;
// Set the desired shipping method to hide the checkout field(s).
$shipping_method = 'table_rate:10:1';
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( $chosen_shipping == $shipping_method ) {
// Add/change field name to be hide
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
}
return $fields;
}
website: camp4coffee.com

Woocommerce show / hide required checkout fields with using of Local pickup plus plugin

Woocommerce Local pickup plus plugin is used with "Hide the shipping address" option. When some required shipping address fields are hidden they are still set as required and the form cannot be completed.
So how to unset the required shipping fields option in case of local pickup is selected?
Had the same issue, fixed it adding this code to functions.php:
add_filter('woocommerce_checkout_fields', 'plus_remove_shipping_checkout_fields');
function plus_remove_shipping_checkout_fields($fields) {
$shipping_method ='local_pickup_plus'; // Set the desired shipping method to hide the checkout field(s).
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
unset($fields['shipping']['shipping_address_1']); // Add/change filed name to be hide
unset($fields['shipping']['shipping_address_2']);
}
return $fields;
}

How to access WooCommerce custom billing field in hooks

I'm writing a custom WordPress function that will change the flat_rate shipping when a customer changes the "State" field from a select menu. Currently I'm doing it in my theme's functions.php
I have created a custom field to represent "State" field as a drop down menu in the billing fields. I used "WooCommerce Checkout Manager" plugin to setup the custom field and disabled the default "State" field.
Now I want to change the shipping cost depending on the value of my custom "State" field. I'm unable to retrieve the data of the field. Also I want to know what hook I can use to change the flat rate shipping once this field's value is changed.
I've used this filter hook (woocommerce_package_rates) and it doesn't work.
Here is my code to do it which I got it from another tutorial then made my customization
function wc_ninja_change_flat_rates_cost( $rates, $package ) {
$destination = $package['destination'];
$city = $destination['myfield12']; // getting the city field value
// Make sure flat rate is available
if ( isset( $rates['flat_rate'] ) ) {
if ( $city == 'Alex' || $city == 'الإسكندرية' ) {
// Set flat rate to cost $10 more
$rates['flat_rate']->cost = 30;
}
else {
$rates['flat_rate']->cost = 20;
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_ninja_change_flat_rates_cost', 10, 2 );
I found this:
How to update shipping cost in cart dynamically (ajax) based on a custom field in WooCommerce
Basically it captures the field data through JS and send an Ajax request to the server which then stores the value in the session. Then adds an additional fee. It is not exactly what I was seeking to do but its a functional workaround.

Categories