-2

I am trying to add a Phone Number mandatory field to the Shipping Details page of the Woocommerce checkout but it should only be mandatory if an alternative shipping address is selected.

Woocommerce see this as a bespoke modification which is outside of their support remit and have suggested I post the question here.

I have made some progress after following the article http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ which has got me almost there, but I am having some problems.

I only want the field to be mandatory if the customer is shipping to a different address, whereas it says that it needs to be completed even if alternative shipping details are not selected.

I've had to back out the changes as this is a live site, but I had updated functions.php with the following:

//Add Tel Number to Shipping Address 
// 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['shipping']['shipping_phone'] = array( 
'label' => __('Phone', 'woocommerce'), 
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'), 
'required' => false, 
'class' => array('form-row-wide'), 
'clear' => true 
);

return $fields; 
}

/** 
* Process the checkout 
*/ 
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() { 
// Check if set, if its not set add an error. 
if ( ! $_POST['shipping_phone'] ) 
wc_add_notice( __( 'Please enter a phone number for the recipient.' ), 'error' ); 
}

This works, in that the field is displayed but I have two problems.

1) How can I get it to only require mandatory completion if ship to a different address is selected?

2) Even though the field is mandatory, how can I get a red asterisk to appear next to the Phone label like it does for the other fields?

If someone could help, I'd really appreciate it.

Thanks

Rob

closed as off-topic by Nathan Johnson, EAMann Mar 3 '18 at 0:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2

I went back through this myself and I have answered my own question.

By following the Woocommerce documentation, I actually confused myself and the second mandatory check isn't applicable as we can use this primary function to set required from false to true. This automatically adds the red asterisk and makes the field mandatory without having to perform the additional check.

Quite simply:

//Add Tel Number to Shipping Address 
// 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['shipping']['shipping_phone'] = array( 
'label' => __('Phone', 'woocommerce'), 
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'), 
'required' => true, 
'class' => array('form-row-wide'), 
'clear' => true 
);

return $fields; 
}

/** 
* Process the checkout 
*/ 
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

I hope this helps someone else.

Regards,

Rob

0
add_filter( 'woocommerce_checkout_fields', 'billing_fields_altration', 9999 );

function billing_fields_altration( $woo_checkout_fields_array ) {


    // unset( $woo_checkout_fields_array['billing']['billing_first_name'] );
     unset( $woo_checkout_fields_array['billing']['billing_last_name'] );
    // unset( $woo_checkout_fields_array['billing']['billing_phone'] );
    // unset( $woo_checkout_fields_array['billing']['billing_email'] );
    // unset( $woo_checkout_fields_array['order']['order_comments'] );

    // and to remove the fields below
    unset( $woo_checkout_fields_array['billing']['billing_company'] );
    //unset( $woo_checkout_fields_array['billing']['billing_country'] );
    //unset( $woo_checkout_fields_array['billing']['billing_address_1'] );
    unset( $woo_checkout_fields_array['billing']['billing_address_2'] );//REMOVE FIELDS
    //unset( $woo_checkout_fields_array['billing']['billing_city'] );
    //unset( $woo_checkout_fields_array['billing']['billing_state'] );
    //unset( $woo_checkout_fields_array['billing']['billing_postcode'] );
 $woo_checkout_fields_array['billing']['billing_phone']['required'] = true;//ENABLE VALIDATION
 unset( $woo_checkout_fields_array['shipping']['shipping_last_name'] );
 //unset( $address_fields_array['postcode']['validate']);//DISABLE VALIDATION
    return $woo_checkout_fields_array;
}
  • 2
    Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Feb 28 '18 at 14:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.