Add Delivery Radio Buttons before order total in WooCommerce checkout - php

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.

To move your radio buttons before order total you will need to use another hook. But you can't have that delivery radio buttons on the fee total line…
I have simplified and revisited the code:
add_action( 'woocommerce_review_order_before_order_total', 'checkout_delivery_radio_buttons' );
function checkout_delivery_radio_buttons() {
echo '<tr class="delivery-radio">
<th>'.__("Delivery Options").'</th><td>';
$chosen = WC()->session->get( 'delivery' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'delivery' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
woocommerce_form_field( 'delivery', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'2.95' => '60 MINUTES: €2.95',
'0' => '24 - 48 HOURS',
),
), $chosen );
echo '</td></tr>';
}
add_action( 'woocommerce_cart_calculate_fees', 'checkout_delivery_fee', 20, 1 );
function checkout_delivery_fee( $cart ) {
if ( $radio = WC()->session->get( 'delivery' ) ) {
$cart->add_fee( 'Delivery Fee', $radio );
}
}
add_action( 'woocommerce_checkout_update_order_review', 'checkout_delivery_choice_to_session' );
function checkout_delivery_choice_to_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['delivery'] ) ){
WC()->session->set( 'delivery', $output['delivery'] );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related

WooCommerce Update Custom Number Field at Checkout

I've added a number field to the checkout page but it doesn't update the totals when selected and allow the order to be placed.
add_action( 'woocommerce_after_checkout_billing_form', 'donation_field' );
function donation_field( $checkout ) {
woocommerce_form_field( 'donation_amount', array(
'type' => 'number',
'required' => true,
'label' => 'Donate',
'description' => 'Please enter a donation amount',
), $checkout->get_value( 'donation_amount' ) );
}
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['donation_amount'] ) ) {
update_post_meta( $order_id, 'donation_amount', sanitize_text_field( $_POST['donation_amount'] ) );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee', 10, 1 );
function add_custom_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$fee = WC()->checkout->get_value('donation_amount');
$cart->add_fee( __( 'Donate Value', 'woocommerce' ) , $fee, false );
}

How to add radio button in a custom checkout field group that update a fee in Woocommerce on the cart and order pages?

I’m new to woocommerce customization. I managed to add two installation options that update the order amount on the order page. I would like to have these two options on the cart page as well. I tried to add woocommerce_cart_totals_before_order_total and the options are displayed on the cart page but the amount is not updated on this cart page, it only works on the order page. Can you help me ?
add_action( 'woocommerce_cart_totals_before_order_total', 'checkout_installation_radio_buttons' );
add_action( 'woocommerce_review_order_before_order_total', 'checkout_installation_radio_buttons' );
function checkout_installation_radio_buttons() {
echo '<tr class="installation-radio">
<th>'.__("Options Installation").'</th><td>';
$chosen = WC()->session->get( 'installation' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'installation' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
woocommerce_form_field( 'installation', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'0' => 'Gratuit',
'200' => 'Installation 200€',
),
), $chosen );
echo '</td></tr>';
}
add_action( 'woocommerce_cart_calculate_fees', 'checkout_installation_fee', 20, 1 );
function checkout_installation_fee( $cart ) {
if ( $radio = WC()->session->get( 'installation' ) ) {
$cart->add_fee( 'Installation', $radio );
}
}
add_action( 'woocommerce_checkout_update_order_review', 'checkout_installation_choice_to_session' );
function checkout_installation_choice_to_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['installation'] ) ){
WC()->session->set( 'installation', $output['installation'] );
}
}

How to calculate and update sales price in woocommerce product page

I inserted two radio buttons as product add ons by using the following to calculate extra packaging cost. But I have two problems.
First instead of getting the value of my custom field eg 4.35 it gets a value of 4.00.
Second how can I update the sales price in my product page when I choose an option
function add_custom_fees_before_add_to_cart() {
global $product;
$myfee = floatval(get_post_meta(get_the_ID(), '_skrprom', TRUE)) + floatval(0.90);
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide' ),
'options' => array(
'' => 'Τηλεφωνική Παραγγελία',
$myfee => 'Έξοδα Συσκευασίας '.$myfee.'€',
),
'default' => ''
);
?>
<div class="custom-fees-wrap">
<label for="iconic-engraving"><?php _e( 'Customize Your Order!', 'textdomain' ); ?></label>
<?php woocommerce_form_field( 'custom_fees', $args, '' ); ?>
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_fees_before_add_to_cart', 99 );
function save_value_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
$custom_fees = filter_input( INPUT_POST, 'custom_fees' );
if ( empty( $custom_fees ) ) {
return $cart_item_data;
}
$cart_item_data['custom_fees'] = $custom_fees;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'save_value_add_cart_item_data', 99, 3 );
function calculate_add_cart_fee() {
global $woocommerce;
$cart_items = $woocommerce->cart->get_cart();
foreach( $cart_items as $key => $item ) {
if( !isset( $item['custom_fees'] ) && empty( $item['custom_fees'] ) ) continue;
$woocommerce->cart->add_fee( __('Έξοδα Συσκευασίας', 'textdomain'), $item['custom_fees'] );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'calculate_add_cart_fee', 99 );

WooCommerce checkout radio buttons that set a percentage fee based on specific items subtotal

I am trying to implement warranty option into woocommerce checkout. The below code works for static price values.
// 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(
'0' => '1 Year Repair or Replace Warranty - Included',
'75' => '2 Years Extended Warranty ($75.00)',
'112.5' => '3 Years Extended Warranty ($112.50)',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<h4>Choose your Warranty</h4>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div><br>';
}
// 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( 'Warranty Option', $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'] );
}
}
Is there a way to output the calculated warranty amount options based on the percentage of items Subtotal Amount? Like:
2 years warranty price is 10% of the Subtotal Price.
3 year warranty is 15%. Example:
For example if the Subtotal is $85, warranty options to display would be:
2 Years Extended Warranty ($8.50) /* 10% */
3 Years Extended Warranty ($12.75) /* 15% */
OPTIONAL: I'm also trying to use this feature to a particular product or a set of products and not to all products. If there's a way to set this.
To get a custom percentage fee based on specific cart items subtotal (for a defined set of product IDs) and on radio button (percentage warranty choice), use the following revisited code:
// Custom function to get related cart items subtotal for specific defined product Ids
function get_related_items_subtotal( $cart ) {
// HERE below define the related targeted products IDs in the array
$targeted_ids = array(29, 27, 28, 72, 84, 95);
$custom_subtotal = 0; // Initialize
// Loop through cart items
foreach ( $cart->get_cart() as $item ) {
if ( array_intersect($targeted_ids, array($item['product_id'], $item['variation_id']) ) ) {
$custom_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
}
}
return $custom_subtotal;
}
// 1 - Display custom checkout radio buttons fields
add_action( 'woocommerce_review_order_before_payment', 'display_custom_checkout_radio_buttons' );
function display_custom_checkout_radio_buttons() {
$custom_subtotal = get_related_items_subtotal( WC()->cart );
if ( $custom_subtotal > 0 ) {
$value = WC()->session->get( 'warranty' );
$value = empty( $value ) ? WC()->checkout->get_value( 'warranty' ) : $value;
$value = empty( $value ) ? '0' : $value;
echo '<div id="checkout-radio">
<h4>' . __("Choose your Warranty") .'</h4>';
woocommerce_form_field( 'warranty', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'0' => __( '1 Year Repair or Replace Warranty - Included', 'woocommerce' ),
'10' => __( '2 Years Extended Warranty', 'woocommerce' ) . ' (' . strip_tags( wc_price( 10 * $custom_subtotal / 100 ) ) . ')',
'15' => __( '3 Years Extended Warranty', 'woocommerce' ) . ' (' . strip_tags( wc_price( 15 * $custom_subtotal / 100 ) ) . ')',
),
), $value );
echo '</div>';
}
}
// 2 - Customizing Woocommerce checkout radio form field
add_filter( 'woocommerce_form_field_radio', 'custom_form_field_radio', 20, 4 );
function custom_form_field_radio( $field, $key, $args, $value ) {
if ( ! empty( $args['options'] ) && 'warranty' === $key && is_checkout() ) {
$field = str_replace( '</label><input ', '</label><br><input ', $field );
$field = str_replace( '<label ', '<label style="display:inline;margin-left:8px;" ', $field );
}
return $field;
}
// 3 - Add a percentage Fee based on radio buttons for specific defined product Ids
add_action( 'woocommerce_cart_calculate_fees', 'percentage_fee_based_on_radio_buttons', 20 );
function percentage_fee_based_on_radio_buttons( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = (float) WC()->session->get( 'warranty' );
if ( $percentage ) {
$custom_subtotal = get_related_items_subtotal( $cart );
if ( $custom_subtotal > 0 ) {
$label_text = sprintf( __('Extended Warranty %d years', 'woocommerce'), $percentage == 10 ? 2 : 3 );
$cart->add_fee( $label_text, $custom_subtotal * $percentage / 100 );
}
}
}
// 4 - Set chosen radio button value to a WC Session variable
add_action( 'woocommerce_checkout_update_order_review', 'chosen_input_radio_button_value_to_wc_session' );
function chosen_input_radio_button_value_to_wc_session( $posted_data ) {
parse_str( $posted_data, $fields );
if ( isset( $fields['warranty'] ) ){
WC()->session->set( 'warranty', $fields['warranty'] );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

I am unable to get the option value to add to cart at checkout

I am using plugins from Modern Tribe called Events Calendar and Event Tickets+ which is integrates with woo-commerce.
When booking an event, the user is asked to input the attendees details. I want to add to the end of the form the option for the user to have their ticket sent in the post. They will be presented with a radio option to include postage and packing.
I have added the file I am targeting to my child theme and added the code successfully to produce the radio button option. However, the code I have written to have the value added to the cart does not work.
I should add, that when the user selects an event, they then get taken to Cart. They are then asked for their attendee details,where I have my radio option. They then get taken to checkout, so it is here that the fee needs to be added. I have added this paragraph to my initial description, in case it is relevant.
Can anyone help please?
function ashweb_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' => 'No Option',
'option_1' => 'Option 1 ($1.65)',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<h3>Add Postage and Packing!</h3>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
}
ashweb_checkout_radio_choice();
add_action( 'woocommerce_cart_calculate_fees', 'ashweb_checkout_radio_choice_fee', 20, 1 );
function ashweb_checkout_radio_choice_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$radio = WC()->session->get( 'radio_chosen' );
if ( "option_1" == $radio ) {
$fee = 1.65;
}
$cart->add_fee( __('Option Fee', 'woocommerce'), $fee );
}

Categories