I am writing a WordPress plugin in which I need to add two radio buttons before the order total in the WooCommerce Order review section. I figured out how to add custom radio buttons to the order review section but I am unable to get the idea how to move delivery options just before the order total.
Please check the screenshot to understand what exactly I want to achieve.
Here's my code:
// Part 1 - Display Radio Buttons
add_action( 'woocommerce_review_order_before_payment', 'custom_checkout_radio_choice' );
function custom_checkout_radio_choice() {
$chosen = WC()->session->get( 'radio_chosen' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'2.95' => '60 MINUTES: €2.95',
'0' => '24 - 48 HOURS',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<h3>Delivery Options</h3>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
}
// Part 2 - Add Fee and Calculate Total
add_action( 'woocommerce_cart_calculate_fees', 'custom_checkout_radio_choice_fee', 20, 1 );
function custom_checkout_radio_choice_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$radio = WC()->session->get( 'radio_chosen' );
if ( $radio ) {
$cart->add_fee( 'Delivery Fee', $radio );
}
}
// Part 3 - Add Radio Choice to Session
add_action( 'woocommerce_checkout_update_order_review', 'custom_checkout_radio_choice_set_session' );
function custom_checkout_radio_choice_set_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['radio_choice'] ) ){
WC()->session->set( 'radio_chosen', $output['radio_choice'] );
}
}
Please help me out with this.
checkout/review-order.php
template and how this is applied for fees. Also, do not forget that these are tables.<div>
used in a table is printed above or below it, so usetable
tags for the output instead – 7uc1f3r