Created
January 28, 2026 10:08
-
-
Save xlplugins/775573f2653d63bba22d83808a76d8ba to your computer and use it in GitHub Desktop.
Funnelkit Checkout: Hide Billing Address Fields When Cart Total is Zero
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class WFACP_Hide_Billing_Fields_On_Zero_Cart { | |
| protected $field_key = 'billing'; | |
| public function __construct() { | |
| add_action( 'wfacp_before_process_checkout_template_loader', [ $this, 'init' ] ); | |
| add_action( 'wfacp_after_checkout_page_found', [ $this, 'init' ] ); | |
| } | |
| /** | |
| * Initialize hooks | |
| */ | |
| public function init() { | |
| add_action( 'wfacp_checkout_fields', [ $this, 'remove_billing_validation' ], 99999 ); | |
| add_action( 'wfacp_internal_css', [ $this, 'enqueue_scripts' ] ); | |
| add_action( 'wp_footer', [ $this, 'add_inline_css' ] ); | |
| } | |
| /** | |
| * Check if cart total is zero | |
| * | |
| * @return bool | |
| */ | |
| protected function is_cart_total_zero() { | |
| if ( ! WC()->cart || WC()->cart->is_empty() ) { | |
| return false; | |
| } | |
| return (float) WC()->cart->get_total( '' ) === 0.0; | |
| } | |
| /** | |
| * Remove required and validation from billing fields when cart total is zero | |
| * | |
| * @param array $fields Checkout fields | |
| * @return array | |
| */ | |
| public function remove_billing_validation( $fields ) { | |
| if ( ! $this->is_cart_total_zero() ) { | |
| return $fields; | |
| } | |
| if ( ! isset( $fields[ $this->field_key ] ) ) { | |
| return $fields; | |
| } | |
| foreach ( $fields[ $this->field_key ] as $key => &$field ) { | |
| unset( $field['required'], $field['validate'] ); | |
| } | |
| return $fields; | |
| } | |
| /** | |
| * Add inline CSS to hide billing section on initial page load | |
| */ | |
| public function add_inline_css() { | |
| if ( ! $this->is_cart_total_zero() ) { | |
| return; | |
| } | |
| ?> | |
| <style> | |
| .wfacp-section:has(.wfacp_divider_billing) { | |
| display: none !important; | |
| } | |
| </style> | |
| <?php | |
| } | |
| /** | |
| * Enqueue JavaScript for dynamic show/hide functionality | |
| */ | |
| public function enqueue_scripts() { | |
| ?> | |
| <script> | |
| (function ($) { | |
| 'use strict'; | |
| /** | |
| * Get cart total from various sources | |
| * | |
| * @param {object} actionData Optional action data from AJAX response | |
| * @return {number} | |
| */ | |
| function getCartTotal( actionData ) { | |
| // Method 1: From action data | |
| if ( actionData && typeof actionData.cart_total !== 'undefined' ) { | |
| return parseFloat( actionData.cart_total ) || 0; | |
| } | |
| // Method 2: From order summary total price | |
| var $totalPrice = $( '.wfacp_order_summary .wfacp_order_total .wfacp_order_total_price' ); | |
| if ( $totalPrice.length ) { | |
| var totalText = $totalPrice.text().replace( /[^0-9.-]/g, '' ); | |
| return parseFloat( totalText ) || 0; | |
| } | |
| // Method 3: From order summary data attribute | |
| var $orderSummary = $( '.wfacp_order_summary' ); | |
| if ( $orderSummary.length ) { | |
| var orderTotal = $orderSummary.data( 'total' ); | |
| if ( orderTotal !== undefined ) { | |
| return parseFloat( orderTotal ) || 0; | |
| } | |
| } | |
| return 0; | |
| } | |
| /** | |
| * Toggle billing section visibility based on cart total | |
| * | |
| * @param {object} actionData Optional action data from AJAX response | |
| */ | |
| function toggleBillingSection( actionData ) { | |
| var $section = $( '.wfacp_divider_billing' ).parents( '.wfacp-section' ); | |
| if ( ! $section.length ) { | |
| return; | |
| } | |
| var cartTotal = getCartTotal( actionData ); | |
| $section.toggle( cartTotal > 0 ); | |
| } | |
| // Initialize on page load | |
| $( document ).ready( function() { | |
| toggleBillingSection(); | |
| } ); | |
| // Initialize when WFACP checkout loads | |
| window.addEventListener( 'bwf_checkout_load', function() { | |
| toggleBillingSection(); | |
| } ); | |
| // Update on AJAX responses | |
| if ( typeof wfacp_frontend !== 'undefined' && wfacp_frontend.hooks ) { | |
| wfacp_frontend.hooks.addAction( 'wfacp_ajax_response', function( fragments, actionData ) { | |
| toggleBillingSection( actionData ); | |
| } ); | |
| wfacp_frontend.hooks.addAction( 'wfacp_update_order_review_response', function( fragments, actionData ) { | |
| toggleBillingSection( actionData ); | |
| } ); | |
| } | |
| })( jQuery ); | |
| </script> | |
| <?php | |
| } | |
| } | |
| new WFACP_Hide_Billing_Fields_On_Zero_Cart(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment