


How to Implement Dynamic Fee Adjustments Based on Radio Button Selections in WooCommerce Checkout?
Update Fee Dynamically Based on Radio Buttons in WooCommerce Checkout
Introduction
Enhance your WooCommerce checkout experience by enabling dynamic fee updates based on radio button selection. This allows you to offer different packing options and fees, catering to customers' preferences.
Code Implementation
Implement the following code in your functions.php file to achieve the desired functionality:
<code class="php">// Add a custom dynamic packaging fee add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 ); function add_packaging_fee( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee $fee = $packing_fee === 'box' ? 9.00 : 3.00; $cart->add_fee( __( 'Packaging fee', 'woocommerce' ), $fee ); } // Add a custom radio fields for packaging selection add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_form_packing_addition', 20 ); function checkout_shipping_form_packing_addition() { $domain = 'woocommerce'; echo '<tr class="packing-select"><th>' . __('Packing options', $domain) . '</th><td>'; $chosen = WC()->session->get('chosen_packing'); $chosen = empty($chosen) ? WC()->checkout->get_value('radio_packing') : $chosen; $chosen = empty($chosen) ? 'bag' : $chosen; // Add a custom checkbox field woocommerce_form_field( 'radio_packing', array( 'type' => 'radio', 'class' => array( 'form-row-wide packing' ), 'options' => array( 'bag' => __('In a bag '.wc_price(3.00), $domain), 'box' => __('In a gift box '.wc_price(9.00), $domain), ), 'default' => $chosen, ), $chosen ); echo '</td></tr>'; }</code>
JavaScript and AJAX Script
To ensure seamless updates upon radio button selection, implement this JavaScript and AJAX script:
<code class="javascript">// jQuery - Ajax script add_action( 'wp_footer', 'checkout_shipping_packing_script' ); function checkout_shipping_packing_script() { if ( ! is_checkout() ) return; // Only checkout page ?> <script type="text/javascript"> jQuery( function($){ $('form.checkout').on('change', 'input[name=radio_packing]', function(e){ e.preventDefault(); var p = $(this).val(); $.ajax({ type: 'POST', url: wc_checkout_params.ajax_url, data: { 'action': 'woo_get_ajax_data', 'packing': p, }, success: function (result) { $('body').trigger('update_checkout'); console.log('response: '+result); // just for testing | TO BE REMOVED }, error: function(error){ console.log(error); // just for testing | TO BE REMOVED } }); }); }); </script> <?php } // Php Ajax (Receiving request and saving to WC session) add_action( 'wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data' ); add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data' ); function woo_get_ajax_data() { if ( isset($_POST['packing']) ){ $packing = sanitize_key( $_POST['packing'] ); WC()->session->set('chosen_packing', $packing ); echo json_encode( $packing ); } die(); // Alway at the end (to avoid server error 500) }</code>
Notes
- Ensure this code is added to an active child theme or directly to your theme's functions.php file.
- The provided code considers non-logged-in users and is compatible with the latest WooCommerce version (3.7.x).
- If encountering any issues, remove all previous customizations related to radio buttons and packing options.
The above is the detailed content of How to Implement Dynamic Fee Adjustments Based on Radio Button Selections in WooCommerce Checkout?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
