I am trying to create a custom checkout radio button that calculates restaurant tip in percentage.
For the radio button, the static value is working fine.
However, I want to get the subtotal and calculate certain percentage on custom radio button click.
This is my code
add_action( 'woocommerce_after_checkout_billing_form', 'add_box_option_to_checkout' );
function add_box_option_to_checkout( $checkout ) {
$chosen = WC()->session->get( 'tip' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'tip' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
$total = WC()->cart->get_subtotal();
$fivetip = $total * 0.05;
woocommerce_form_field( 'tip', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
$fivetip => '5%',
'10.00' => '10%',
'15.00' => '15%',
),
), $chosen );
woocommerce_form_field( 'add_tip', array(
'type' => 'text',
'class' => array('add_tipform-row-wide'),
'placeholder' => __('Enter Custom Tip Amount')
), $checkout->get_value( 'add_tip' ));
}
add_action( 'woocommerce_cart_calculate_fees', 'checkout_tip_fee', 20, 1 );
function checkout_tip_fee( $cart ) {
if ( $radio = WC()->session->get( 'tip' ) ) {
$cart->add_fee( 'Tip', $radio );
}
}
add_action( 'woocommerce_checkout_update_order_review', 'checkout_tip_choice_to_session' );
function checkout_tip_choice_to_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['tip'] ) ){
WC()->session->set( 'tip', $output['tip'] );
}
}
My Answer:
>session->get( 'chosen_tip' );_echo '
Rate this post
3 of 5 based on 4654 votesComments