eMikkelsen
eMikkelsen

Reputation: 411

WooCommerce required fields error

I have made a custom design for my WooCommerce checkout and for the longest time it worked just fine, however it seems recently I can no longer checkout. Even if I fill out all the fields I get an error saying that I must fill out certain fields. This is the code that I use and I am looking to either fix it so that it only requires the fields that I have in this code or worst case to just write a code so it never demands anything to be filled out.

I have done some further testing and I am using only billing fields, but the errors are appearing for shipping fields. I need to make shipping and billing fields one and the same or to just no longer have shipping fields required as the billing fields seem to work perfectly.

function custom_override_checkout_fields( $fields ) {
    unset($fields['billing']['billing_company']);
    unset($fields['billing']['billing_country']);
    unset($fields['billing']['billing_address_2']);

    return $fields;
}

add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );


// for billing fields

    add_filter("woocommerce_checkout_fields", "new_order_fields");

        function new_order_fields($fields) {

            $order = array(            
                "billing_first_name",
                "billing_last_name",
                "billing_address_1",
                "billing_city",
                "billing_postcode",
                "billing_phone",
		        "billing_email"

            );
            foreach($order as $field)
            {
                $ordered_fields[$field] = $fields["billing"][$field];
            }

            $fields["billing"] = $ordered_fields;
            return $fields;

        }

          // for shipping fields
        add_filter("woocommerce_checkout_fields", "new_shiping_order_fields");

        function new_shiping_order_fields($fields) {

            $order = array(  
                "shipping_city",
                "shipping_postcode",
                "shipping_country",   
                "shipping_first_name",
                "shipping_last_name",
                "shipping_company",
                "shipping_address_1",
                "shipping_address_2"

            );
            foreach($order as $field)
            {
                $ordered_fields[$field] = $fields["shipping"][$field];
            }

            $fields["shipping"] = $ordered_fields;
            return $fields;

        }
// WooCommerce Checkout Fields Hook
add_filter( 'woocommerce_checkout_fields' , 'custom_wc_checkout_fields' );
 
// Change order comments placeholder and label, and set billing phone number to not required.
function custom_wc_checkout_fields( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'Fornavn';
$fields['billing']['billing_first_name']['label'] = false;
$fields['billing']['billing_last_name']['placeholder'] = 'Efternavn';
$fields['billing']['billing_last_name']['label'] = false;
$fields['billing']['billing_address_1']['placeholder'] = 'Adresse';
$fields['billing']['billing_address_1']['label'] = false;
$fields['billing']['billing_postcode']['placeholder'] = 'Postnummer';
$fields['billing']['billing_postcode']['label'] = false;
$fields['billing']['billing_city']['placeholder'] = 'By';
$fields['billing']['billing_city']['label'] = false;
$fields['billing']['billing_phone']['placeholder'] = 'Telefon';
$fields['billing']['billing_phone']['label'] = false;
$fields['billing']['billing_email']['placeholder'] = 'E-mail';
$fields['billing']['billing_email']['label'] = false;

return $fields;
}
function ra_change_translate_text_multiple( $translated ) {
    $text = array(
        'Send til en anden adresse?' => 'Tryk her for at vælge en alternativ leveringsadresse',
        'Billing details' => '1. Kundeinformationer',
        'Din ordre' => 'Deres bestilling',
        'Opret en konto?' => 'Gem mine informationer til næste køb',
        'Afgiv ordre' => 'Gå til sikker betaling',
        'Bemærkninger om Deres bestilling, f.eks. særlige bemærkninger for levering.' => 'Indtast kommentar til ordre eller specielle ønsker her.'
    );
    $translated = str_ireplace(  array_keys($text),  $text,  $translated );
    return $translated;
}
add_filter( 'gettext', 'ra_change_translate_text_multiple', 20 );
/**
 * Add the field to the checkout page
 */
add_action('billing_email', 'customise_checkout_field');

function customise_checkout_field($checkout)
{
    echo '<div id="customise_checkout_field"><h2>' . __('Heading') . '</h2>';
    woocommerce_form_field('customised_field_name', array(
        'type' => 'text',
        'class' => array(
        'my-field-class form-row-wide'
        ) ,
        'label' => __('Customise Additional Field') ,
        'placeholder' => __('Guidence') ,
        'required' => true,
    ) , $checkout->get_value('customised_field_name'));
    echo '</div>';
}

Upvotes: 0

Views: 2395

Answers (1)

eMikkelsen
eMikkelsen

Reputation: 411

I found a solution:

//make shipping fields not required in checkout
add_filter( 'woocommerce_shipping_fields', 'wc_npr_filter_shipping_fields', 10, 1 );
function wc_npr_filter_shipping_fields( $address_fields ) {
	$address_fields['shipping_first_name']['required'] = false;
	$address_fields['shipping_last_name']['required'] = false;
	$address_fields['shipping_address_1']['required'] = false;
	$address_fields['shipping_city']['required'] = false;
	$address_fields['shipping_postcode']['required'] = false;
		return $address_fields;
}

Upvotes: 1

Related Questions