Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/294129d1f8684b8d59a03a9a1c03a644 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/294129d1f8684b8d59a03a9a1c03a644 to your computer and use it in GitHub Desktop.
Hide Shipping Address Fields if local pickup is selected Final Code 7 jan 2026
class WFACP_Hide_Shipping_Fields_By_Method {
public function __construct() {
add_filter( 'woocommerce_update_order_review_fragments', [ $this, 'attach_fragments' ], 1000 );
add_action( 'wfacp_before_process_checkout_template_loader', [ $this, 'register_hooks' ] );
add_action( 'wfacp_after_checkout_page_found', [ $this, 'register_hooks' ] );
add_action( 'wfacp_internal_css', [ $this, 'enqueue_assets' ] );
add_filter( 'woocommerce_checkout_posted_data', [ $this, 'clear_hidden_field_data' ] );
}
private function get_field_configuration() {
return apply_filters( 'wfacp_conditional_fields_config', [
'local_pickup:13' => [
'wfacp_divider_shipping',
'shipping_same_as_billing',
'shipping_address_1',
'shipping_address_2',
'shipping_phone',
'shipping_city',
'shipping_country',
'shipping_state',
'shipping_postcode',
'shipping_wc_custom_field',
'wfacp_divider_shipping_end',
'wfacp_divider_billing',
'billing_same_as_shipping',
'billing_first_name',
'billing_last_name',
'billing_phone',
'billing_address_1',
'billing_address_2',
'billing_city',
'billing_country',
'billing_state',
'billing_postcode',
'billing_wc_custom_field',
'wfacp_divider_billing_end'
],
] );
}
private function get_current_shipping_method() {
if ( is_null( WC()->session ) ) {
return null;
}
$chosen_methods = WC()->session->get( 'chosen_shipping_methods', [] );
return ! empty( $chosen_methods ) ? $chosen_methods[0] : null;
}
private function get_fields_to_hide() {
$config = $this->get_field_configuration();
$current_method = $this->get_current_shipping_method();
$fields_to_hide = [];
foreach ( $config as $method => $fields ) {
if ( $current_method === $method ) {
$fields_to_hide = array_merge( $fields_to_hide, $fields );
}
}
return array_values( array_unique( $fields_to_hide ) );
}
public function attach_fragments( $fragments ) {
$fragments['wfacp_conditional_fields'] = wp_json_encode( $this->get_fields_to_hide() );
return $fragments;
}
public function register_hooks() {
add_filter( 'wfacp_checkout_fields', [ $this, 'modify_field_requirements' ] );
}
public function modify_field_requirements( $fields ) {
$fields_to_hide = $this->get_fields_to_hide();
if ( empty( $fields_to_hide ) ) {
return $fields;
}
foreach ( $fields_to_hide as $field_name ) {
if ( strpos( $field_name, 'billing_' ) === 0 ) {
$group = 'billing';
} elseif ( strpos( $field_name, 'shipping_' ) === 0 ) {
$group = 'shipping';
} else {
$group = 'advanced';
}
if ( isset( $fields[ $group ][ $field_name ]['required'] ) ) {
unset( $fields[ $group ][ $field_name ]['required'] );
}
}
return $fields;
}
public function clear_hidden_field_data( $data ) {
$fields_to_hide = $this->get_fields_to_hide();
if ( empty( $fields_to_hide ) ) {
return $data;
}
foreach ( $fields_to_hide as $field_name ) {
if ( isset( $data[ $field_name ] ) ) {
unset( $data[ $field_name ] );
}
}
return $data;
}
public function enqueue_assets() {
?>
<script>
(function($) {
function checkAndHideTitle() {
// Find all sections with wfacp-comm-title
$('.wfacp-section').each(function() {
var $section = $(this);
var $title = $section.find('.wfacp-comm-title');
var $formDetail = $section.find('.wfacp-comm-form-detail');
if ($title.length === 0 || $formDetail.length === 0) {
return; // Skip if title or form detail not found
}
// Find all form fields (excluding dividers and hidden fields by default)
var $allFields = $formDetail.find('p.form-row[id$="_field"]').not('.wfacp_divider_field');
// Also find collapse labels (for collapsible address_2 fields)
var $collapseLabels = $formDetail.find('p.form-row[id$="_collapse_label"]');
// Check if all fields are hidden
var allFieldsHidden = true;
var hasVisibleFields = false;
$allFields.each(function() {
var $field = $(this);
// Check if field is visible (not hidden by wfacp_hide_field class and not already hidden by other means)
if (!$field.hasClass('wfacp_hide_field') && $field.is(':visible')) {
allFieldsHidden = false;
hasVisibleFields = true;
return false; // Break the loop
}
});
// Also check collapse labels - if a field is hidden, hide its collapse label too
$collapseLabels.each(function() {
var $collapseLabel = $(this);
var collapseLabelId = $collapseLabel.attr('id');
if (collapseLabelId) {
// Extract field name from collapse label ID (e.g., "shipping_address_2_collapse_label" -> "shipping_address_2")
var fieldName = collapseLabelId.replace('_collapse_label', '');
// Scope the search to the current section for better performance
var $relatedField = $formDetail.find('#' + fieldName + '_field');
// Fallback to global search if not found in section
if ($relatedField.length === 0) {
$relatedField = $('#' + fieldName + '_field');
}
// If the related field is hidden, hide the collapse label too
if ($relatedField.length > 0 && $relatedField.hasClass('wfacp_hide_field')) {
$collapseLabel.addClass('wfacp_hide_field');
} else {
// If field is visible, make sure collapse label is visible (unless it's in collapsible state)
if (!$collapseLabel.hasClass('wfacp_hidden_class')) {
$collapseLabel.removeClass('wfacp_hide_field');
}
}
}
});
// Hide or show the title based on field visibility
if (allFieldsHidden && hasVisibleFields === false && $allFields.length > 0) {
// All fields are hidden, hide the title
$title.addClass('wfacp_hide_field');
} else {
// At least one field is visible, show the title
$title.removeClass('wfacp_hide_field');
}
});
}
$(document.body).on('updated_checkout', function(e, data) {
if (!data || !data.fragments || !data.fragments.wfacp_conditional_fields) {
// Even if no fragments, check title visibility
checkAndHideTitle();
return;
}
var fieldsToHide = JSON.parse(data.fragments.wfacp_conditional_fields);
console.log("fieldsToHide",fieldsToHide);
$('p.form-row.wfacp_hide_field').removeClass('wfacp_hide_field');
// Also remove hide class from collapse labels
$('p.form-row[id$="_collapse_label"].wfacp_hide_field').removeClass('wfacp_hide_field');
if (Array.isArray(fieldsToHide)) {
fieldsToHide.forEach(function(fieldName) {
var $field = $('#' + fieldName + '_field');
$field.addClass('wfacp_hide_field');
// Also hide collapse label if it exists (for collapsible address_2 fields)
var $collapseLabel = $('#' + fieldName + '_collapse_label');
if ($collapseLabel.length > 0) {
$collapseLabel.addClass('wfacp_hide_field');
}
});
}
// Check and hide/show titles after fields are updated
setTimeout(checkAndHideTitle, 100);
});
// Also check on initial page load
$(document).ready(function() {
setTimeout(checkAndHideTitle, 500);
});
})(jQuery);
</script>
<style>
p.wfacp_hide_field {
display: none !important;
}
.wfacp-comm-title.wfacp_hide_field {
display: none !important;
}
p.form-row[id$="_collapse_label"].wfacp_hide_field {
display: none !important;
}
</style>
<?php
}
}
new WFACP_Hide_Shipping_Fields_By_Method();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment