Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Last active December 13, 2019 18:00
Show Gist options
  • Select an option

  • Save pavlo-bondarchuk/91da027d6d501e867d7050afe30a734c to your computer and use it in GitHub Desktop.

Select an option

Save pavlo-bondarchuk/91da027d6d501e867d7050afe30a734c to your computer and use it in GitHub Desktop.
Create Custom Primary Contact Information Fields if Cart contains Virtual Products @ WooCommerce Checkout
add_action( 'woocommerce_after_checkout_billing_form', 'primary_contact_fields' );
function primary_contact_fields( $checkout ){
if( is_virtual() ) {
echo '</div>';
echo '<div class="woocommerce-primary_contact-fields" style="float:left;">';
echo '<h3>Primary Contact Information</h3>';
echo '<div class="woocommerce-primary_contact-fields__field-wrapper">';
woocommerce_form_field( 'primarycontactname', array(
'type' => 'text', // text, textarea, select, radio, checkbox, password, about custom validation a little later
'required' => true, // actually this parameter just adds "*" to the field
'class' => array('primary-contact-name-class', 'form-row-wide'), // array only, read more about classes and styling in the previous step
'label' => 'Contact Name',
'label_class' => 'primary-contact-name-label', // sometimes you need to customize labels, both string and arrays are supported
),
$checkout->get_value( 'primarycontactname' ) );
woocommerce_form_field( 'primarycontactphone', array(
'type' => 'text', // text, textarea, select, radio, checkbox, password, about custom validation a little later
'required' => true, // actually this parameter just adds "*" to the field
'class' => array('primary-contact-name-class', 'form-row-wide'), // array only, read more about classes and styling in the previous step
'label' => 'Contact Phone',
'label_class' => 'primary-contact-phone-label', // sometimes you need to customize labels, both string and arrays are supported
),
$checkout->get_value( 'primarycontactphone' ) );
woocommerce_form_field( 'primarycontactemail', array(
'type' => 'email', // text, textarea, select, radio, checkbox, password, about custom validation a little later
'required' => true, // actually this parameter just adds "*" to the field
'class' => array('primary-contact-email-class', 'form-row-wide'), // array only, read more about classes and styling in the previous step
'label' => 'Contact Email',
'validate' => 'validate-email',
'label_class' => 'primary-contact-email-label', // sometimes you need to customize labels, both string and arrays are supported
),
$checkout->get_value( 'primarycontactemail' ) );
echo '</div>';
}
}
function is_virtual(){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['data']->is_virtual() ){
return true;
}
} return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment