Created
October 4, 2018 20:10
-
-
Save codeagencybe/6494c8ee1640361b6b4588f8e68e3c4a to your computer and use it in GitHub Desktop.
custom checkout fee
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
| /** | |
| * @snippet Custom Checkout Fee for WooCommerce | |
| * @author Fabio Tielen / Code Agency / https://codeagency.be | |
| * @compatible WooCommerce 3.4.5 | |
| */ | |
| // Display Buttons | |
| // Uses woocommerce_form_field() | |
| add_action( 'woocommerce_review_order_before_payment', 'ca_checkout_radio_choice' ); | |
| function ca_checkout_radio_choice() { | |
| $chosen = WC()->session->get('radio_chosen'); | |
| $chosen = empty( $chosen ) ? WC()->checkout->get_value('radio_choice') : $chosen; | |
| $chosen = empty( $chosen ) ? 'no_option' : $chosen; | |
| $args = array( | |
| 'type' => 'radio', | |
| 'class' => array( 'form-row-wide' ), | |
| 'options' => array( | |
| 'no_option' => 'Standaard 1 verzending', | |
| 'option_1' => 'Kaartjes/enveloppen apart verzenden (€2.50)', | |
| ), | |
| 'default' => $chosen | |
| ); | |
| echo '<div id="checkout-radio">'; | |
| echo '<h3>Apart verzenden</h3>'; | |
| woocommerce_form_field( 'radio_choice', $args, $chosen ); | |
| echo '</div>'; | |
| } | |
| // Add Fee and Re-calculate Total order price | |
| // Based on session's "radio_chosen" | |
| add_action( 'woocommerce_cart_calculate_fees', 'ca_checkout_radio_choice_fee', 20, 1 ); | |
| function ca_checkout_radio_choice_fee( $cart ) { | |
| if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; | |
| $radio = WC()->session->get( 'radio_chosen' ); | |
| if ( "option_1" == $radio ) { | |
| $fee = 2.5; | |
| } | |
| $cart->add_fee( __('Extra verzending', 'woocommerce'), $fee ); | |
| } | |
| // Refresh Checkout if Radio Changes | |
| // Uses jQuery | |
| add_action( 'wp_footer', 'ca_checkout_radio_choice_refresh' ); | |
| function ca_checkout_radio_choice_refresh() { | |
| if ( ! is_checkout() ) return; | |
| ?> | |
| <script type="text/javascript"> | |
| jQuery( function($){ | |
| $('form.checkout').on('change', 'input[name=radio_choice]', function(e){ | |
| e.preventDefault(); | |
| var p = $(this).val(); | |
| $.ajax({ | |
| type: 'POST', | |
| url: wc_checkout_params.ajax_url, | |
| data: { | |
| 'action': 'woo_get_ajax_data', | |
| 'radio': p, | |
| }, | |
| success: function (result) { | |
| $('body').trigger('update_checkout'); | |
| } | |
| }); | |
| }); | |
| }); | |
| </script> | |
| <?php | |
| } | |
| // Add Radio Choice to Session | |
| // Uses Ajax | |
| add_action( 'wp_ajax_woo_get_ajax_data', 'ca_checkout_radio_choice_set_session' ); | |
| add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'ca_checkout_radio_choice_set_session' ); | |
| function ca_checkout_radio_choice_set_session() { | |
| if ( isset($_POST['radio']) ){ | |
| $radio = sanitize_key( $_POST['radio'] ); | |
| WC()->session->set('radio_chosen', $radio ); | |
| echo json_encode( $radio ); | |
| } | |
| die(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment