WooCommerce Add Insurance On Checkout

By February 2, 2020 February 17th, 2020 Web Development

Random little issue I had to solve. Create a way to add insurance to an order. Basically create a dummy product not in the store for a set amount. Change the [add-to_cart_url id=””?????] to the ID of your product that is the amount of insurance you want to add to the order. Then change the button text to match the price. Pretty simple and commented below.

add_action('woocommerce_cart_totals_after_shipping', 'wc_shipping_insurance_note_after_cart');
function wc_shipping_insurance_note_after_cart() {
global $woocommerce;
    $product_id = 1963;  // Change ID
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
    if ( $_product->id == $product_id )
        $found = true;
    }
    // if product not found, add it
if ( ! $found ):
?>
    <tr class="shipping">
        <th><?php _e( 'Shipping Insurance', 'woocommerce' ); ?></th>
        <td><a href="<?php echo do_shortcode('[add_to_cart_url id="1963"]'); ?>"><!-- Change ID --><?php _e( 'Add shipping insurance (+$25) ' ); ?><!-- Change Price --></a></td>
    </tr>
<?php else: ?>
    <tr class="shipping">
        <th><?php _e( 'Shipping Insurance', 'woocommerce' ); ?></th>
        <td>$25</td> <!-- Change Price -->
    </tr>
<?php endif;
}