John Davidson

php - Set a dynamic fee based on custom checkout radio buttons and text field in WooCommerce

0 comments
Message:


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 '

' . __('Restaurant Tip', 'woocommerce') . '

_

_

_

_

_
';}add_action( 'woocommerce_checkout_update_order_review', 'update_custom_tip' );function update_custom_tip( $post_data ) {if( isset( $post_data['post_data']['custom_tip'] ) ) {WC()->session->set( 'chosen_tip', esc_attr( $post_data['post_data']['custom_tip'] ) );if( $post_data['post_data']['custom_tip'] == 'custom' ) {WC()->session->set( 'custom_tip_amount', esc_attr( $post_data['post_data']['custom_tip_amount'] ) );} else {WC()->session->set( 'custom_tip_amount', '' );}}}_add_action( 'woocommerce_cart_calculate_fees', 'add_custom_tip_fee' );function add_custom_tip_fee() {if ( WC()->session->get( 'chosen_tip' ) ) {global $woocommerce;foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {$tip_percentage = WC()->session->get( 'chosen_tip' );$tip_amount = 0;if( $tip_percentage == 'custom' ) {$tip_amount = WC()->session->get( 'custom_tip_amount' );} else {$_product = $values['data'];$subtotal = $_product->get_price() * $values['quantity'];$tip_amount = ( $subtotal * $tip_percentage ) / 100;}$woocommerce->cart->add_fee( 'Restaurant Tip', $tip_amount, true, 'standard' );}}}_This code adds a custom radio button field for selecting the tip percentage at the checkout page. It also includes a text field for entering a custom tip amount. The code calculates the tip amount based on the selected percentage or custom amount and adds it as a fee to the order.

Rate this post

4 of 5 based on 2889 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us