Add a custom fee
-
Hello.
I add optional additional service on checkout by this code:
//////////////// Add a custom checkbox fields after billing fields add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_checkout_checkbox', 20 ); function add_custom_checkout_checkbox(){ // Add a custom checkbox field woocommerce_form_field( 'klient_gift', array( 'type' => 'checkbox', 'label' => __(' ?My Custom service? </br><span style="font-weight:100;font-size:11px;">description!</span></br>'), 'class' => array( 'form-row-wide' ), ), '' ); } // Remove "(optional)" label on "Installement checkbox" field add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 ); function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) { // Only on checkout page for Order notes field if( 'klient_gift' === $key && is_checkout() ) { $optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>'; $field = str_replace( $optional, '', $field ); } return $field; } // jQuery - Ajax script add_action( 'wp_footer', 'checkout_fee_script' ); function checkout_fee_script() { // Only on Checkout if( is_checkout() && ! is_wc_endpoint_url() ) : if( WC()->session->__isset('enable_fee') ) WC()->session->__unset('enable_fee') ?> <script type="text/javascript"> jQuery( function($){ if (typeof wc_checkout_params === 'undefined') return false; $('form.checkout').on('change', 'input[name=klient_gift]', function(e){ var fee = $(this).prop('checked') === true ? '1' : ''; $.ajax({ type: 'POST', url: wc_checkout_params.ajax_url, data: { 'action': 'enable_fee', 'enable_fee': fee, }, success: function (result) { $('body').trigger('update_checkout'); }, }); }); }); </script> <?php endif; } // Get Ajax request and saving to WC session add_action( 'wp_ajax_enable_fee', 'get_enable_fee' ); add_action( 'wp_ajax_nopriv_enable_fee', 'get_enable_fee' ); function get_enable_fee() { if ( isset($_POST['enable_fee']) ) { WC()->session->set('enable_fee', ($_POST['enable_fee'] ? true : false) ); } die(); } // Add a custom 7hrn fee add_action( 'woocommerce_cart_calculate_fees', 'custom_percetage_fee', 20, 1 ); function custom_percetage_fee( $cart ) { // Only on checkout if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() ) return; $fix_price = 7; if( WC()->session->get('enable_fee') ) $cart->add_fee( __( 'Подарок клиенту', 'woocommerce')." ($fix_price Грн)", ($fix_price) ); }
All works fine….
1) But in order some time exist – https://i.imgur.com/06wICQU.png
2) In case when I add discount to role:add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role_and_payment', 20, 1 ); function discount_based_on_user_role_and_payment( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Exit // Only on checkout page for 'vendor' user role if ( ! ( is_checkout() && current_user_can('vendor') ) ) return; // Exit $percentage = 5; // Calculation $discount = $cart->get_subtotal() * $percentage / 100; // Applying discount $cart->add_fee( sprintf( __("Discount vendor (%s)", "woocommerce"), $percentage . '%'), -$discount, true ); }
‘fee’ row additional service not exist – https://i.imgur.com/xlUxl7f.png
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Add a custom fee’ is closed to new replies.