This code is a WooCommerce customization that allows customers to add a tip to their order during checkout.
Advantages of the tip option:
Increases Revenue: Adding a tip option gives customers an opportunity to show appreciation for great service, and this can result in increased revenue for the business.
Improves Customer Satisfaction: Allowing customers to show their appreciation through a tip option can enhance their overall satisfaction with the business, leading to customer loyalty and repeat business.
Easy to Implement: Implementing a tip option on WooCommerce checkout is relatively easy, as this code snippet demonstrates.
Customizable: The code is customizable and can be edited to include different tip amounts or options.
If youre looking for a simpler option, take a look at this nice plugin
/**
* WooCommerce Tip option on Checkout
*/
// Add The tip
add_action( 'woocommerce_cart_calculate_fees', 'add_tip_fee', 20, 1 );
function add_tip_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$radio = WC()->session->get( 'tip_chosen' );
if ( $radio ) {
switch ( $radio ) {
case 'tip_1':
$tip_amount = 0.20;
break;
case 'tip_2':
$tip_amount = 0.50;
break;
default:
$tip_amount = 0;
break;
}
$cart->add_fee( 'Tip' , $tip_amount );
}
}
// Add tip to order
add_action( 'woocommerce_checkout_update_order_review', 'save_tip_choice' );
function save_tip_choice( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['tip_choice'] ) ){
WC()->session->set( 'tip_chosen', $output['tip_choice'] );
}
}
// Modify the tip options to include the tip amount in the value
add_action( 'woocommerce_review_order_before_payment', 'checkout_radio_buttons' );
function checkout_radio_buttons() {
echo '<div id="tips" class="tips">';
echo '<h4>Add Tip?</h4>';
woocommerce_form_field( 'tip_choice', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change', 'tip-options' ),
'options' => array(
'0' => 'No tip',
'tip_1' => '€0.2',
'tip_2' => '€0.5',
),
'default' => '0',
'value' => $tip_amount, // use the tip amount variable as the value
));
echo '</div>';
}
// css
add_action( 'wp_footer', 'checkout_tip_styling' );
function checkout_tip_styling() { ?>
<style>
label.radio {
display:inline!important;
margin-right:30px;
}
.tips-donation {
padding: 30px 0;
}
</style>
<?php }